]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
item drop, pick up
authorClyne Sullivan <tullivan99@gmail.com>
Fri, 20 Jan 2017 15:32:08 +0000 (10:32 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Fri, 20 Jan 2017 15:32:08 +0000 (10:32 -0500)
include/components.hpp
main.cpp
src/inventory.cpp

index 449f7c3712221dd101265716490f5e43cc2ad66a..5292fb286ebf9176d93f05b25d3aac7a346252b2 100644 (file)
@@ -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;
index 4a41a3a89870059b2095060723d39f8c805edb84..30e19f90f30b209aa745cb4919d93ce23201457a 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -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;
 
index 83bdeb204f38bfac2a2bcba61781a8ca4bbbf688..fd844f5db03272971bc3e28242a31d142ea3e578 100644 (file)
@@ -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;
        }
 }