]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
inventory item dragging
authorClyne Sullivan <tullivan99@gmail.com>
Fri, 20 Jan 2017 01:55:39 +0000 (20:55 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Fri, 20 Jan 2017 01:55:39 +0000 (20:55 -0500)
Changelog
include/events.hpp
include/inventory.hpp
include/vector2.hpp
src/inventory.cpp
src/ui.cpp

index b13a786574ad19090f07b55c98bca428833977bf..94f0a4d70284e4b9d62658bd17bf4e7a386b3874 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -1093,3 +1093,20 @@ Late June (6/16/2016 - 6/27/2016)
        - created render.cpp, moved shader management to there
        - this game will be kinda decent now???
        - input bugs, because we're awful
+
+
+
+
+1/19/2017:
+==========
+
+       What's been happening:
+
+       - major rewrite, full on EntityX
+       - everything is actually decent now
+       - keeping an eye on optimization, perf, ...
+       - basically no segfaults? like what the heck
+       - new inventory system
+       - new entity animation system
+       - almost ready to really get some story work done
+
index 5939c3daf8cfa68f77f1b56f343fee0721f2fa72..6251fac58561de115b136b2a927930754d822478 100644 (file)
@@ -40,6 +40,14 @@ struct MouseClickEvent {
        int button;
 };
 
+struct MouseReleaseEvent {
+       MouseReleaseEvent(vec2 pos, int b)
+               : position(pos), button(b) {}
+
+       vec2 position;
+       int button;
+};
+
 struct KeyDownEvent {
     KeyDownEvent(SDL_Keycode kc = 0)
         : keycode(kc) {}
index 2e261ae0cfb642c22abfe4382d039df48a29ef7b..084a2d6162a9ddd5c1c151132173e6f26aa9b525 100644 (file)
@@ -86,6 +86,7 @@ public:
     void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override;
     void receive(const KeyDownEvent &kde);
        void receive(const MouseClickEvent &mce);
+       void receive(const MouseReleaseEvent &mce);
 
        void render(void);
 
index 17e47d13db61fad953114646f9fb9b05d9087962..d335a4bcdb8aa31c32436ea90caf9c955e4bcd47 100644 (file)
@@ -71,6 +71,14 @@ struct vector2 {
                return (x == v.x) && (y == v.y);
        }
 
+       bool operator>(const vector2<T>& v) const {
+               return (x > v.x) && (y > v.y);
+       }
+
+       bool operator<(const vector2<T>& v) const {
+               return (x < v.x) && (y < v.y);
+       }
+
        // other functions
        std::string toString(void) const {
                return "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
index f222ef14b88cc6a76fdcce2959a15507cc694078..83bdeb204f38bfac2a2bcba61781a8ca4bbbf688 100644 (file)
@@ -23,10 +23,13 @@ constexpr int          hotbarSize = 4;
 constexpr float        inventoryZ = -6.0f;
 constexpr unsigned int rowSize = 8;
 
+static int movingItem = -1;
+
 void InventorySystem::configure(entityx::EventManager &ev)
 {
     ev.subscribe<KeyDownEvent>(*this);
        ev.subscribe<MouseClickEvent>(*this);
+       ev.subscribe<MouseReleaseEvent>(*this);
 }
 
 void InventorySystem::loadItems(void) {
@@ -99,16 +102,21 @@ void InventorySystem::render(void)
                        i.item->sprite.use();
                        static const GLfloat tex[12] = {0,1,1,1,1,0,1,0,0,0,0,1};
                        glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex);
-                       vec2 end = i.loc + i.item->sprite.getDim();
+
+                       vec2 sta ((n == movingItem) ? ui::mouse - i.item->sprite.getDim() / 2 : i.loc);
+                       vec2 end = (n == movingItem) ? ui::mouse + i.item->sprite.getDim() / 2 : i.loc + i.item->sprite.getDim();
                        GLfloat coords[18] = {
-                               i.loc.x, i.loc.y, inventoryZ - 0.2, end.x, i.loc.y, inventoryZ - 0.2, end.x, end.y, inventoryZ - 0.2,
-                               end.x, end.y, inventoryZ - 0.2, i.loc.x, end.y, inventoryZ - 0.2, i.loc.x, i.loc.y, inventoryZ - 0.2
+                               sta.x, sta.y, inventoryZ - 0.2, end.x, sta.y, inventoryZ - 0.2, end.x, end.y, inventoryZ - 0.2,
+                               end.x, end.y, inventoryZ - 0.2, sta.x, end.y, inventoryZ - 0.2, sta.x, sta.y, inventoryZ - 0.2
                        };
                        glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords);
+                       if (n == movingItem)
+                               glUniform4f(Render::textShader.uniform[WU_tex_color], .8, .8, 1, .8);
                        glDrawArrays(GL_TRIANGLES, 0, 6);
                        ui::setFontZ(-7.2); // TODO fix z's
-                       ui::putText(i.loc.x, i.loc.y, std::to_string(i.count).c_str());
+                       ui::putText(sta.x, sta.y, std::to_string(i.count).c_str());
                        ui::setFontZ(-6);
+                       glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, 1);
                }
        }
 
@@ -118,7 +126,34 @@ void InventorySystem::render(void)
 
 void InventorySystem::receive(const MouseClickEvent &mce)
 {
-       (void)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;
+               }
+       }
+}
+
+void InventorySystem::receive(const MouseReleaseEvent &mre)
+{
+       if (movingItem != -1) {
+               int end = fullInventory ? items.size() : hotbarSize;
+               for (int i = 0; i < end; i++) {
+                       if (mre.position > items[i].loc && mre.position < items[i].loc + entrySize) {
+                               if (items[i].count > 0) {
+                                       std::swap(items[movingItem], items[i]);
+                               } else {
+                                       items[i] = items[movingItem];
+                                       items[movingItem].item = nullptr;
+                                       items[movingItem].count = 0;
+                               }
+                               break;
+                       }
+               }
+               movingItem = -1;
+       }
 }
 
 void InventorySystem::receive(const KeyDownEvent &kde)
index dad9454641d5b4ae1c839a6448e5bc5491265968..7a81bd525305347680e8c0e43900a8798f51ce3d 100644 (file)
@@ -1202,7 +1202,10 @@ void InputSystem::receive(const MainSDLEvent& event)
                premouse.y=e.motion.y;
                break;
 
-       //case SDL_MOUSEBUTTONUP:
+       case SDL_MOUSEBUTTONUP:
+               ev.emit<MouseReleaseEvent>(mouse, e.button.button);
+               break;
+
        case SDL_MOUSEBUTTONDOWN:
                ev.emit<MouseClickEvent>(mouse, e.button.button);