From 59edd60ebec61bf24dd27063f85bcd049fd0af13 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 19 Jan 2017 16:20:13 -0500 Subject: killed common, more inventory, other random stuff --- include/brice.hpp | 1 - include/color.hpp | 26 ++++++++ include/common.hpp | 164 ++--------------------------------------------- include/components.hpp | 15 +++-- include/config.hpp | 22 +++++++ include/debug.hpp | 19 ++++++ include/engine.hpp | 9 ++- include/error.hpp | 13 ++++ include/events.hpp | 4 +- include/fileio.hpp | 15 +++++ include/gametime.hpp | 1 - include/glm.hpp | 14 ++++ include/inventory.hpp | 75 ++++++++++++++++++---- include/particle.hpp | 4 +- include/player.hpp | 5 +- include/quest.hpp | 1 - include/random.hpp | 20 ++++++ include/render.hpp | 2 +- include/shader_utils.hpp | 1 + include/texture.hpp | 9 ++- include/ui.hpp | 52 +++++---------- include/ui_menu.hpp | 57 ++++++++-------- include/ui_quest.hpp | 5 +- include/vector2.hpp | 3 + include/vector3.hpp | 19 ++++++ include/weather.hpp | 6 +- include/world.hpp | 23 +++---- 27 files changed, 311 insertions(+), 274 deletions(-) create mode 100644 include/color.hpp create mode 100644 include/debug.hpp create mode 100644 include/error.hpp create mode 100644 include/fileio.hpp create mode 100644 include/glm.hpp create mode 100644 include/random.hpp create mode 100644 include/vector3.hpp (limited to 'include') diff --git a/include/brice.hpp b/include/brice.hpp index 1c4eccf..96ec25e 100644 --- a/include/brice.hpp +++ b/include/brice.hpp @@ -2,7 +2,6 @@ * @file brice.hpp * @brief A system for saving player information. */ - #ifndef BRICE_H_ #define BRICE_H_ diff --git a/include/color.hpp b/include/color.hpp new file mode 100644 index 0000000..2c89a1c --- /dev/null +++ b/include/color.hpp @@ -0,0 +1,26 @@ +#ifndef COLOR_HPP_ +#define COLOR_HPP_ + +/** + * Keeps track of an RGBA color. + */ +class Color{ +public: + float red; /**< The amount of red, 0-255 or 0.0-1.0 depending on usage */ + float green; /**< The amount of green */ + float blue; /**< The amount of blue */ + float alpha; /**< Transparency */ + + Color(float r = 0, float g = 0, float b = 0, float a = 255) + : red(r), green(g), blue(b), alpha(a) {} + + Color operator-(const float& a) { + return Color(red - a, green - a, blue - a, alpha); + } + + Color operator+(const float& a) { + return Color(red + a, green + a, blue + a, alpha); + } +}; + +#endif // COLOR_HPP_ diff --git a/include/common.hpp b/include/common.hpp index 1244464..b9be831 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -1,46 +1,9 @@ -#ifndef COMMON_H -#define COMMON_H - /** - * @file common.h - * @brief Common items needed by most other files. + * @file common.hpp + * @brief Common things needed by all files, in theory. */ - -// standard library includes -#include -#include -#include -#include -#include -#include -#include - -// alternative windows thread library -#ifndef __WIN32__ -#include -#else -#include -#endif // __WIN32__ - -// local library includes -#define GLEW_STATIC -#include - -#include -#include -#include -#include - -#include - -#define GLM_FORCE_RADIANS -#include -#include -#include -#include - -// game library includes -#include +#ifndef COMMON_HPP_ +#define COMMON_HPP_ // windows stuff #ifdef __WIN32__ @@ -48,128 +11,15 @@ using uint = unsigned int; #undef near #endif -/** - * Prints a formatted string to the terminal with file and line number, for debugging - */ -#define DEBUG_printf(message, ...) DEBUG_prints(__FILE__, __LINE__, message, __VA_ARGS__) - -#define coalesce(v1, v2) ((v1 != nullptr) ? v1 : v2) - -#include - -using vec2 = vector2; -using dim2 = vector2; - -/** - * A structure for three-dimensional points. - */ -struct vec3 { - float x; /**< The x coordinate */ - float y; /**< The y coordinate */ - float z; /**< The z coordinate */ - - vec3(float _x = 0.0f, float _y = 0.0f, float _z = 1.0f) - : x(_x), y(_y), z(_z) {} -}; - -/** - * This structure contains two sets of coordinates for ray drawing. - */ - -typedef struct { - vec2 start; /**< The start coordinate of the ray */ - vec2 end; /**< The end coordinate of the ray */ -} Ray; - -/** - * Keeps track of an RGBA color. - */ -class Color{ -public: - float red; /**< The amount of red, 0-255 or 0.0-1.0 depending on usage */ - float green; /**< The amount of green */ - float blue; /**< The amount of blue */ - float alpha; /**< Transparency */ - - Color(float r = 0, float g = 0, float b = 0, float a = 255) - : red(r), green(g), blue(b), alpha(a) {} - - Color operator-(const float& a) { - return Color(red - a, green - a, blue - a, alpha); - } - - Color operator+(const float& a) { - return Color(red + a, green + a, blue + a, alpha); - } -}; - -/** - * The amount of game ticks that should occur each second. - */ -constexpr unsigned int TICKS_PER_SEC = 20; - -/** - * The amount of milliseconds it takes for a game tick to fire. - */ -constexpr float MSEC_PER_TICK = 1000.0f / TICKS_PER_SEC; - -/** - * Separates a string into tokens using the given delimiter. - * - * @param the string to parse - * @param the delimiting character - * @return a vector of the tokens - */ -std::vector StringTokenizer(const std::string& str, char delim); - -/** - * Returns a measurement in HLINEs - * - * @param the number of HLINEs, integer or decimal - * @return the number in HLINEs - */ -template -inline T HLINES(const T &n) -{ - return (static_cast(game::HLINE) * n); -} - -/** - * A generically-named function to start the random number generator. - * This currently redirects to the library's default, but allows for - * a custom generator to be easily implemented. - */ -#define randInit srand - -/** - * Gets a random number (is a function). - */ -#define randGet rand - // defines pi for calculations that need it. constexpr float PI = 3.1415926535f; -// references the variable in main.cpp, used for drawing with the player -extern vec2 offset; - /** - * Prints a formatted debug message to the console, along with the callee's file and line - * number. + * Gets millisecond count since epoch. + * @return number of milliseconds */ -void DEBUG_prints(const char* file, int line, const char *s,...); - unsigned int millis(void); -// reads the names of files in a directory into the given string vector -int getdir(std::string dir, std::list& files); - -// reads the given file into a buffer and returns a pointer to the buffer -std::string readFile(const std::string& path); -std::vector readFileA(const std::string& path); - -// aborts the program, printing the given error -void UserError(std::string reason); - namespace std { template constexpr const T& clamp(const T& v, const T& lo, const T& hi) { @@ -177,4 +27,4 @@ namespace std { } } -#endif // COMMON_H +#endif // COMMON_HPP_ diff --git a/include/components.hpp b/include/components.hpp index e0147b0..449f7c3 100644 --- a/include/components.hpp +++ b/include/components.hpp @@ -5,15 +5,18 @@ * this allows the entity to change stats or skills on the go. This also allows every "object" * the be an entity, and it gives the game a much better customizability over xml. */ - #ifndef COMPONENTS_HPP #define COMPONENTS_HPP -#include -#include -#include +#include +#include + #include -#include +#include +#include +#include + +#include #include using namespace tinyxml2; @@ -158,7 +161,7 @@ struct SpriteData { vec2 offset_tex; vec2 size_tex; - uint limb; + unsigned int limb; }; using Frame = std::vector>; diff --git a/include/config.hpp b/include/config.hpp index e17df24..13596ff 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -71,4 +71,26 @@ namespace game { } } +/** + * The amount of game ticks that should occur each second. + */ +constexpr unsigned int TICKS_PER_SEC = 20; + +/** + * The amount of milliseconds it takes for a game tick to fire. + */ +constexpr float MSEC_PER_TICK = 1000.0f / TICKS_PER_SEC; + +/** + * Returns a measurement in HLINEs + * + * @param the number of HLINEs, integer or decimal + * @return the number in HLINEs + */ +template +inline T HLINES(const T &n) +{ + return (static_cast(game::HLINE) * n); +} + #endif //CONFIG_H diff --git a/include/debug.hpp b/include/debug.hpp new file mode 100644 index 0000000..e773a51 --- /dev/null +++ b/include/debug.hpp @@ -0,0 +1,19 @@ +/** + * @file debug.hpp + * @brief Debugging utilities + */ +#ifndef DEBUG_HPP_ +#define DEBUG_HPP_ + +/** + * Prints a formatted string to the terminal with file and line number, for debugging + */ +#define DEBUG_printf(message, ...) DEBUG_prints(__FILE__, __LINE__, message, __VA_ARGS__) + +/** + * Prints a formatted debug message to the console, along with the callee's file and line + * number. + */ +void DEBUG_prints(const char* file, int line, const char *s,...); + +#endif // DEBUG_HPP_ diff --git a/include/engine.hpp b/include/engine.hpp index 417522d..fda9980 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -6,10 +6,12 @@ #ifndef ENGINE_HPP_ #define ENGINE_HPP_ +#include +#include +#include + #include -#include -#include #include #include @@ -63,9 +65,6 @@ public: } }; -#include -#include - class LockableEntityManager : public entityx::EntityManager { private: std::atomic_bool locked; diff --git a/include/error.hpp b/include/error.hpp new file mode 100644 index 0000000..ef61c91 --- /dev/null +++ b/include/error.hpp @@ -0,0 +1,13 @@ +#ifndef ERROR_HPP_ +#define ERROR_HPP_ + +#include +#include + +inline void UserError(const std::string& why) +{ + std::cout << "User error: " << why << "!\n"; + abort(); +} + +#endif // ERROR_HPP_ diff --git a/include/events.hpp b/include/events.hpp index 1f06544..5939c3d 100644 --- a/include/events.hpp +++ b/include/events.hpp @@ -8,7 +8,9 @@ #include #include -#include + +#include +#include class World; diff --git a/include/fileio.hpp b/include/fileio.hpp new file mode 100644 index 0000000..c5e33ec --- /dev/null +++ b/include/fileio.hpp @@ -0,0 +1,15 @@ +#ifndef FILEIO_HPP_ +#define FILEIO_HPP_ + +#include +#include +#include + +// reads the names of files in a directory into the given string vector +int getdir(std::string dir, std::list& files); + +// reads the given file into a buffer and returns a pointer to the buffer +std::string readFile(const std::string& path); +std::vector readFileA(const std::string& path); + +#endif // FILEIO_HPP_ diff --git a/include/gametime.hpp b/include/gametime.hpp index 988533a..e06a0d3 100644 --- a/include/gametime.hpp +++ b/include/gametime.hpp @@ -2,7 +2,6 @@ * @file gametime.hpp * @brief Handles time related operations */ - #ifndef GAMETIME_H_ #define GAMETIME_H_ diff --git a/include/glm.hpp b/include/glm.hpp new file mode 100644 index 0000000..9fd533f --- /dev/null +++ b/include/glm.hpp @@ -0,0 +1,14 @@ +/** + * @file glm.hpp + * @brief Includes needed GLM files. + */ +#ifndef GLM_HPP_ +#define GLM_HPP_ + +#define GLM_FORCE_RADIANS +#include +#include +#include +#include + +#endif // GLM_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 +#include + +#include +#include + #include +#include +using namespace tinyxml2; -#include #include +#include +/** + * @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(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, public entityx::Receiver { private: + /** + * A vector for the player's inventory slots. + */ std::vector 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_ diff --git a/include/particle.hpp b/include/particle.hpp index 48a8938..f49e055 100644 --- a/include/particle.hpp +++ b/include/particle.hpp @@ -1,10 +1,10 @@ #ifndef PARTICLE_HPP_ #define PARTICLE_HPP_ -#include #include +#include -#include +#include #include diff --git a/include/player.hpp b/include/player.hpp index 85dc821..56886e3 100644 --- a/include/player.hpp +++ b/include/player.hpp @@ -2,16 +2,15 @@ * @file player.hpp * @brief The player system */ - #ifndef PLAYER_HPP_ #define PLAYER_HPP_ #include +#include #include #include -#include -#include +#include /** * The constant velocity the player is given when moved with the arrow keys. diff --git a/include/quest.hpp b/include/quest.hpp index cb43eca..27b533d 100644 --- a/include/quest.hpp +++ b/include/quest.hpp @@ -2,7 +2,6 @@ * @file quest.hpp * Quest handling. */ - #ifndef QUEST_HPP_ #define QUEST_HPP_ diff --git a/include/random.hpp b/include/random.hpp new file mode 100644 index 0000000..198861d --- /dev/null +++ b/include/random.hpp @@ -0,0 +1,20 @@ +/** + * @file random.hpp + * @brief Facilities to generate random numbers. + */ +#ifndef RANDOM_HPP_ +#define RANDOM_HPP_ + +/** + * A generically-named function to start the random number generator. + * This currently redirects to the library's default, but allows for + * a custom generator to be easily implemented. + */ +#define randInit srand + +/** + * Gets a random number (is a function). + */ +#define randGet rand + +#endif // RANDOM_HPP_ diff --git a/include/render.hpp b/include/render.hpp index 997d7d0..a21fcdd 100644 --- a/include/render.hpp +++ b/include/render.hpp @@ -6,8 +6,8 @@ #include #include -#include #include +#include /** * @class Shader diff --git a/include/shader_utils.hpp b/include/shader_utils.hpp index 06ac318..011b265 100644 --- a/include/shader_utils.hpp +++ b/include/shader_utils.hpp @@ -13,6 +13,7 @@ #include #include +#include char* file_read(const char* filename); void print_log(GLuint object); diff --git a/include/texture.hpp b/include/texture.hpp index 43db1b6..fb4e588 100644 --- a/include/texture.hpp +++ b/include/texture.hpp @@ -6,7 +6,14 @@ #ifndef TEXTURE_HPP_ #define TEXTURE_HPP_ -#include +#include +#include + +#include // must +#include + +#include +#include /** * When defined, DEBUG allows extra messages to be printed to the terminal for diff --git a/include/ui.hpp b/include/ui.hpp index 67c1010..ac415f5 100644 --- a/include/ui.hpp +++ b/include/ui.hpp @@ -1,49 +1,27 @@ -/* ---------------------------------------------------------------------------- -** The user interface system. -** -** This file contains everything user-interface related. -** --------------------------------------------------------------------------*/ -#ifndef UI_H -#define UI_H +/** + * @file ui.hpp + * @brief the user interface system. + */ +#ifndef UI_HPP_ +#define UI_HPP_ -#define DEBUG -#define SDL_KEY e.key.keysym.sym - -/* ---------------------------------------------------------------------------- -** Includes section -** --------------------------------------------------------------------------*/ - -// standard library headers #include -#include -#include - -// local game headers -#include -#include -//#include -#include -#include +#include -// local library headers +#include +#include #include -#include -#include FT_FREETYPE_H - -#ifndef __WIN32__ -# include -#endif // __WIN32__ +#include +#include +#include -/* ---------------------------------------------------------------------------- -** The UI namespace -** --------------------------------------------------------------------------*/ +#define DEBUG +#define SDL_KEY e.key.keysym.sym void setControl(int index, SDL_Keycode key); SDL_Keycode getControl(int index); -#include - class InputSystem : public entityx::System, public entityx::Receiver { public: inline void configure(entityx::EventManager &ev) { @@ -168,4 +146,4 @@ namespace ui { void takeScreenshot(GLubyte *pixels); } -#endif // UI_H +#endif // UI_HPP_ diff --git a/include/ui_menu.hpp b/include/ui_menu.hpp index b45a76d..5288161 100644 --- a/include/ui_menu.hpp +++ b/include/ui_menu.hpp @@ -1,56 +1,53 @@ #ifndef UI_MENU_H_ #define UI_MENU_H_ -#include +#include +#include + +#include #include #include +#include -typedef void (*menuFunc)(void); +using MenuAction = std::function; class Menu; class menuItem { public: int member; - Menu *child; + Menu* child; vec2 loc; dim2 dim; Color color; std::string text; - //union { - struct { - menuFunc func; - } button; - - struct { - float minValue; - float maxValue; - float sliderLoc; - float *var; - } slider; - //}; - - menuItem(){} - ~menuItem(){ - //button.text = NULL; - //slider.text = NULL; - - //delete[] button.text; - //delete[] slider.text; - //delete slider.var; - } + + struct { + MenuAction func; + } button; + + struct { + float minValue; + float maxValue; + float sliderLoc; + float* var; + } slider; + + menuItem(void) {} + + menuItem(vec2 l, dim2 d, Color c, std::string t, Menu* ch = nullptr) + : child(ch), loc(l), dim(d), color(c), text(t) {} }; class Menu { public: std::vector items; - Menu *parent; + Menu* parent; - ~Menu() - { + ~Menu(void) { items.clear(); - parent = NULL; + parent = nullptr; } void gotoParent(void); @@ -58,7 +55,7 @@ public: namespace ui { namespace menu { - menuItem createButton(vec2 l, dim2 d, Color c, const char* t, menuFunc f); + menuItem createButton(vec2 l, dim2 d, Color c, const char* t, MenuAction f); menuItem createChildButton(vec2 l, dim2 d, Color c, const char* ti, Menu *_child); menuItem createParentButton(vec2 l, dim2 d, Color c, const char* t); menuItem createSlider(vec2 l, dim2 d, Color c, float min, float max, const char* t, float* v); diff --git a/include/ui_quest.hpp b/include/ui_quest.hpp index d770011..a46483a 100644 --- a/include/ui_quest.hpp +++ b/include/ui_quest.hpp @@ -2,12 +2,13 @@ * @file ui_quest.hpp * @brief Handles UI elements related to quests. */ - #ifndef UI_QUEST_HPP_ #define UI_QUEST_HPP_ #include -#include +#include + +extern vec2 offset; namespace ui { namespace quest { diff --git a/include/vector2.hpp b/include/vector2.hpp index debaee9..17e47d1 100644 --- a/include/vector2.hpp +++ b/include/vector2.hpp @@ -77,4 +77,7 @@ struct vector2 { } }; +using vec2 = vector2; +using dim2 = vector2; + #endif // VECTOR2_HPP_ diff --git a/include/vector3.hpp b/include/vector3.hpp new file mode 100644 index 0000000..ee09eae --- /dev/null +++ b/include/vector3.hpp @@ -0,0 +1,19 @@ +#ifndef VECTOR3_HPP_ +#define VECTOR3_HPP_ + +/** + * A structure for three-dimensional points. + */ +template +struct vector3 { + T x; /**< The x coordinate */ + T y; /**< The y coordinate */ + T z; /**< The z coordinate */ + + vector3(T _x = 0, T _y = 0, T _z = 1) + : x(_x), y(_y), z(_z) {} +}; + +using vec3 = vector3; + +#endif // VECTOR3_HPP_ diff --git a/include/weather.hpp b/include/weather.hpp index 8e148dd..58a0cfa 100644 --- a/include/weather.hpp +++ b/include/weather.hpp @@ -1,11 +1,15 @@ #ifndef WEATHER_HPP_ #define WEATHER_HPP_ +#include + #include -#include +#include #include +extern vec2 offset; + /** * The weather type enum. * This enum contains every type of weather currently implemented in the game. diff --git a/include/world.hpp b/include/world.hpp index 9b68314..fe7d819 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -2,23 +2,24 @@ * @file world.hpp * @brief The world system */ +#ifndef WORLD_HPP_ +#define WORLD_HPP_ -#ifndef WORLD_H -#define WORLD_H +#include +#include +#include -// library includes +#include #include -// local game includes -#include -#include -#include -#include -#include #include - using namespace tinyxml2; +#include +#include +#include +#include + /** * The background type enum. * Used to choose which set of background images should be used. @@ -178,4 +179,4 @@ public: void load(const std::string& file); }; -#endif // WORLD_H +#endif // WORLD_HPP_ -- cgit v1.2.3 From df22618e71ceeb73d3f4c6b8f74d9c07fce3ccea Mon Sep 17 00:00:00 2001 From: Clyne Sullivan 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') 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& v) const { + return (x > v.x) && (y > v.y); + } + + bool operator<(const vector2& 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(*this); ev.subscribe(*this); + ev.subscribe(*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(mouse, e.button.button); + break; + case SDL_MOUSEBUTTONDOWN: ev.emit(mouse, e.button.button); -- cgit v1.2.3 From 1ac412a5496fb6c63c47f199dfc7facd5f4c080a Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 20 Jan 2017 10:32:08 -0500 Subject: item drop, pick up --- include/components.hpp | 18 +++++++++++++++++- main.cpp | 3 +-- src/inventory.cpp | 48 +++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 59 insertions(+), 10 deletions(-) (limited to 'include') 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 #include +#include #include #include #include @@ -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; diff --git a/main.cpp b/main.cpp index 4a41a3a..30e19f9 100644 --- a/main.cpp +++ b/main.cpp @@ -131,8 +131,7 @@ int main(int argc, char *argv[]) ///////////////////////////// - game::engine.getSystem()->add("Debug", 3); - game::engine.getSystem()->take("Debug", 2); + game::engine.getSystem()->add("Debug", 9); std::list 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 #include +#include +#include #include #include #include @@ -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([&](entityx::Entity e, ItemDrop& id) { + auto& posComp = *e.component(); + auto& dimComp = *e.component(); + 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(mre.position.x, mre.position.y); + e.assign(0, 1); + e.assign(items[movingItem]); + e.assign(); + e.component()->addSpriteSegment( + SpriteData(items[movingItem].item->sprite), vec2(0, 0)); + auto dim = items[movingItem].item->sprite.getDim(); + e.assign(HLINES(dim.x), HLINES(dim.y)); + e.assign(); + e.assign(); + + items[movingItem].item = nullptr; + items[movingItem].count = 0; + movingItem = -1; } } -- cgit v1.2.3