diff options
-rw-r--r-- | include/components.hpp | 18 | ||||
-rw-r--r-- | main.cpp | 3 | ||||
-rw-r--r-- | src/inventory.cpp | 48 |
3 files changed, 59 insertions, 10 deletions
diff --git a/include/components.hpp b/include/components.hpp index 449f7c3..5292fb2 100644 --- a/include/components.hpp +++ b/include/components.hpp @@ -12,6 +12,7 @@ #include <vector> #include <events.hpp> +#include <inventory.hpp> #include <random.hpp> #include <texture.hpp> #include <vector2.hpp> @@ -64,7 +65,7 @@ struct Physics { * Constructor that sets the gravity constant, if not specified it becomes 0. * @param g The non default gravity constant. */ - Physics(float g = 0.0f): g(g) {} + Physics(float g = 1.0f): g(g) {} float g; /**< The gravity constant, how fast the object falls */ }; @@ -105,6 +106,13 @@ struct Name { std::string name; }; +struct ItemDrop { + ItemDrop(InventoryEntry& ie) + : item(ie) {} + + InventoryEntry item; +}; + /** * @struct Solid * @brief Allows an entity to collide with other objects. @@ -154,6 +162,14 @@ struct SpriteData { offset_tex.y = offset.y/tmpsize.y; } + SpriteData(Texture t) + : tex(t) { + size_tex = 1; + offset_tex = 0; + size = tex.getDim(); + offset = 0; + } + Texture tex; vec2 size; vec2 offset; @@ -131,8 +131,7 @@ int main(int argc, char *argv[]) ///////////////////////////// - game::engine.getSystem<InventorySystem>()->add("Debug", 3); - game::engine.getSystem<InventorySystem>()->take("Debug", 2); + game::engine.getSystem<InventorySystem>()->add("Debug", 9); std::list<SDL_Event> eventQueue; diff --git a/src/inventory.cpp b/src/inventory.cpp index 83bdeb2..fd844f5 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -1,6 +1,8 @@ #include <inventory.hpp> #include <common.hpp> +#include <components.hpp> +#include <engine.hpp> #include <error.hpp> #include <render.hpp> #include <ui.hpp> @@ -126,12 +128,26 @@ void InventorySystem::render(void) void InventorySystem::receive(const MouseClickEvent &mce) { - int end = fullInventory ? items.size() : hotbarSize; - movingItem = -1; - for (int i = 0; i < end; i++) { - if (mce.position > items[i].loc && mce.position < items[i].loc + entrySize) { - movingItem = i; - break; + if (mce.button == SDL_BUTTON_RIGHT) { + game::entities.each<ItemDrop>([&](entityx::Entity e, ItemDrop& id) { + auto& posComp = *e.component<Position>(); + auto& dimComp = *e.component<Solid>(); + vec2 sta (posComp.x, posComp.y); + vec2 end (sta.x + dimComp.width, sta.y + dimComp.height); + + if (mce.position > sta && mce.position < end) { + add(id.item.item->name, id.item.count); + e.destroy(); + } + }); + } else { + int end = fullInventory ? items.size() : hotbarSize; + movingItem = -1; + for (int i = 0; i < end; i++) { + if (mce.position > items[i].loc && mce.position < items[i].loc + entrySize) { + movingItem = i; + break; + } } } } @@ -149,9 +165,27 @@ void InventorySystem::receive(const MouseReleaseEvent &mre) items[movingItem].item = nullptr; items[movingItem].count = 0; } - break; + + movingItem = -1; + return; } } + + auto e = game::entities.create(); + e.assign<Position>(mre.position.x, mre.position.y); + e.assign<Direction>(0, 1); + e.assign<ItemDrop>(items[movingItem]); + e.assign<Sprite>(); + e.component<Sprite>()->addSpriteSegment( + SpriteData(items[movingItem].item->sprite), vec2(0, 0)); + auto dim = items[movingItem].item->sprite.getDim(); + e.assign<Solid>(HLINES(dim.x), HLINES(dim.y)); + e.assign<Visible>(); + e.assign<Physics>(); + + items[movingItem].item = nullptr; + items[movingItem].count = 0; + movingItem = -1; } } |