From 59edd60ebec61bf24dd27063f85bcd049fd0af13 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan <tullivan99@gmail.com> Date: Thu, 19 Jan 2017 16:20:13 -0500 Subject: killed common, more inventory, other random stuff --- include/inventory.hpp | 75 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 14 deletions(-) (limited to 'include/inventory.hpp') diff --git a/include/inventory.hpp b/include/inventory.hpp index 44ed148..2e261ae 100644 --- a/include/inventory.hpp +++ b/include/inventory.hpp @@ -1,21 +1,41 @@ +/** + * @file inventory.hpp + * @brief Provides an inventory for the player. + */ #ifndef INVENTORY_HPP_ #define INVENTORY_HPP_ +#include <GL/glew.h> +#include <SDL2/SDL_opengl.h> + +#include <string> +#include <vector> + #include <entityx/entityx.h> +#include <tinyxml2.h> +using namespace tinyxml2; -#include <components.hpp> #include <events.hpp> +#include <texture.hpp> +/** + * @struct Item + * Contains the information neccessary for an item. + */ struct Item { - std::string name; - std::string type; - int value; - int stackSize; - Texture sprite; + std::string name; /**< The name of the item */ + std::string type; /**< The type of the item */ + int value; /**< The value/worth of the item */ + int stackSize; /**< The stack size of the item */ + Texture sprite; /**< The texture for the item (in inventory) */ Item(void) : value(0), stackSize(1) {} + /** + * Constructs an item from XML. + * @param the xml element (<item />) + */ Item(XMLElement *e) { name = e->StrAttribute("name"); type = e->StrAttribute("type"); @@ -29,35 +49,62 @@ struct Item { } }; +/** + * @struct InventoryEntry + * Describes a slot in the player's inventory. + */ struct InventoryEntry { - Item* item; - int count; - vec2 loc; + Item* item; /**< Pointer to info on what item this slot contains */ + int count; /**< The quantity of this item stored here */ + + vec2 loc; /**< Used by render, to determine slot location on screen */ InventoryEntry(void) : item(nullptr), count(0) {} }; +/** + * @class InventorySystem + * Handles the player's inventory system. + */ class InventorySystem : public entityx::System<InventorySystem>, public entityx::Receiver<InventorySystem> { private: + /** + * A vector for the player's inventory slots. + */ std::vector<InventoryEntry> items; + void loadItems(void); + public: - InventorySystem(int size = 4) { + InventorySystem(int size = 20) { items.resize(size); loadItems(); } void configure(entityx::EventManager &ev); - - void loadItems(void); - void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override; - void receive(const KeyDownEvent &kde); + void receive(const MouseClickEvent &mce); + void render(void); + /** + * Adds 'count' 'name's to the inventory. + * @param name the name of the item + * @param count the quantity of the item to give + */ void add(const std::string& name, int count); + + /** + * Takes 'count' 'name's from the inventory. + * If the inventory does not contain enough of the item, no items are taken + * and false is returned. + * @param name the name of the item + * @param count the quantity of the item to take + * @return true if the operation could be completed + */ + bool take(const std::string& name, int count); }; #endif // INVENTORY_HPP_ -- cgit v1.2.3 From df22618e71ceeb73d3f4c6b8f74d9c07fce3ccea Mon Sep 17 00:00:00 2001 From: Clyne Sullivan <tullivan99@gmail.com> Date: Thu, 19 Jan 2017 20:55:39 -0500 Subject: inventory item dragging --- Changelog | 17 +++++++++++++++++ include/events.hpp | 8 ++++++++ include/inventory.hpp | 1 + include/vector2.hpp | 8 ++++++++ src/inventory.cpp | 45 ++++++++++++++++++++++++++++++++++++++++----- src/ui.cpp | 5 ++++- 6 files changed, 78 insertions(+), 6 deletions(-) (limited to 'include/inventory.hpp') diff --git a/Changelog b/Changelog index b13a786..94f0a4d 100644 --- 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 + diff --git a/include/events.hpp b/include/events.hpp index 5939c3d..6251fac 100644 --- a/include/events.hpp +++ b/include/events.hpp @@ -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) {} diff --git a/include/inventory.hpp b/include/inventory.hpp index 2e261ae..084a2d6 100644 --- a/include/inventory.hpp +++ b/include/inventory.hpp @@ -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); diff --git a/include/vector2.hpp b/include/vector2.hpp index 17e47d1..d335a4b 100644 --- a/include/vector2.hpp +++ b/include/vector2.hpp @@ -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) + ")"; diff --git a/src/inventory.cpp b/src/inventory.cpp index f222ef1..83bdeb2 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -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) diff --git a/src/ui.cpp b/src/ui.cpp index dad9454..7a81bd5 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -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); -- cgit v1.2.3