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 --- src/brice.cpp | 4 ++ src/common.cpp | 5 ++ src/components.cpp | 2 + src/engine.cpp | 1 + src/gametime.cpp | 1 + src/inventory.cpp | 129 ++++++++++++++++++++++++++++++++++++++++------- src/quest.cpp | 3 +- src/render.cpp | 7 +++ src/shader_utils.cpp | 5 +- src/texture.cpp | 7 ++- src/ui.cpp | 72 +++++++++++--------------- src/ui_menu.cpp | 139 +++++++++++++++++++++++++++------------------------ src/window.cpp | 1 + src/world.cpp | 9 +++- 14 files changed, 253 insertions(+), 132 deletions(-) (limited to 'src') diff --git a/src/brice.cpp b/src/brice.cpp index 53b1431..e1a5b3e 100644 --- a/src/brice.cpp +++ b/src/brice.cpp @@ -6,6 +6,10 @@ #include #include +#include +#include + +extern std::vector StringTokenizer(const std::string& str, char delim); static std::unordered_map brice; diff --git a/src/common.cpp b/src/common.cpp index 2ec80a7..37f5c78 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1,11 +1,16 @@ #include #include +#include #include #include #include +#include +#include #include +#include + #ifndef __WIN32__ #include diff --git a/src/components.cpp b/src/components.cpp index 2bd93fc..d8321f4 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include diff --git a/src/engine.cpp b/src/engine.cpp index d334aea..ebf5953 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/src/gametime.cpp b/src/gametime.cpp index cb736ff..a5272e5 100644 --- a/src/gametime.cpp +++ b/src/gametime.cpp @@ -1,6 +1,7 @@ #include #include // millis +#include static unsigned int tickCount = 0; static unsigned int deltaTime = 1; diff --git a/src/inventory.cpp b/src/inventory.cpp index 354db9c..f222ef1 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -1,23 +1,32 @@ #include #include -#include -#include +#include #include #include +#include #include #include using namespace tinyxml2; -constexpr const char* itemsPath = "config/items.xml"; +extern vec2 offset; static std::unordered_map itemList; +constexpr const char* itemsPath = "config/items.xml"; + +static bool fullInventory = false; + +constexpr int entrySize = 70; +constexpr int hotbarSize = 4; +constexpr float inventoryZ = -6.0f; +constexpr unsigned int rowSize = 8; void InventorySystem::configure(entityx::EventManager &ev) { ev.subscribe(*this); + ev.subscribe(*this); } void InventorySystem::loadItems(void) { @@ -43,24 +52,43 @@ void InventorySystem::update(entityx::EntityManager &en, entityx::EventManager & void InventorySystem::render(void) { + int size = items.size(); + // calculate positions - items.front().loc = vec2(offset.x - 35 * items.size(), offset.y - game::SCREEN_HEIGHT / 2); - for (unsigned int i = 1; i < items.size(); i++) - items[i].loc = vec2(items[i - 1].loc.x + 70, items[i - 1].loc.y); + items.front().loc = vec2(offset.x - entrySize / 2 * hotbarSize, offset.y + game::SCREEN_HEIGHT / 2 - entrySize); + for (unsigned int i = 1; i < hotbarSize; i++) + items[i].loc = vec2(items[i - 1].loc.x + entrySize, items[i - 1].loc.y); + + ui::drawNiceBox(items[0].loc - 10, items[hotbarSize - 1].loc + vec2(entrySize + 4, entrySize + 10), inventoryZ); + + if (fullInventory) { + vec2 start (offset.x - entrySize * rowSize / 2, offset.y + game::SCREEN_HEIGHT / 2 - 180); + for (unsigned int i = hotbarSize; i < items.size(); i++) { + items[i].loc = vec2(start.x + entrySize * ((i - hotbarSize) % rowSize), start.y); + if ((i - hotbarSize) % rowSize == rowSize - 1) + start.y -= entrySize; + } + + ui::drawNiceBox(items[items.size() - rowSize].loc - 10, items[hotbarSize + rowSize - 1].loc + (entrySize + 4), inventoryZ); + } else { + size = hotbarSize; + } // draw items - for (const auto& i : items) { + for (int n = 0; n < size; n++) { + auto &i = items[n]; + // draw the slot Render::textShader.use(); Render::textShader.enable(); Colors::black.use(); - glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, .8); + glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, .6); glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, Colors::texCoord); - vec2 end = i.loc + 64; + vec2 end = i.loc + entrySize - 6; GLfloat coords[18] = { - i.loc.x, i.loc.y, -7, end.x, i.loc.y, -7, end.x, end.y, -7, - end.x, end.y, -7, i.loc.x, end.y, -7, i.loc.x, i.loc.y, -7 + i.loc.x, i.loc.y, inventoryZ - 0.1, end.x, i.loc.y, inventoryZ - 0.1, end.x, end.y, inventoryZ - 0.1, + end.x, end.y, inventoryZ - 0.1, i.loc.x, end.y, inventoryZ - 0.1, i.loc.x, i.loc.y, inventoryZ - 0.1 }; glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); glDrawArrays(GL_TRIANGLES, 0, 6); @@ -73,12 +101,12 @@ void InventorySystem::render(void) glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); vec2 end = i.loc + i.item->sprite.getDim(); GLfloat coords[18] = { - i.loc.x, i.loc.y, -7.1, end.x, i.loc.y, -7.1, end.x, end.y, -7.1, - end.x, end.y, -7.1, i.loc.x, end.y, -7.1, i.loc.x, i.loc.y, -7.1 + 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 }; glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); glDrawArrays(GL_TRIANGLES, 0, 6); - ui::setFontZ(-7.2); + ui::setFontZ(-7.2); // TODO fix z's ui::putText(i.loc.x, i.loc.y, std::to_string(i.count).c_str()); ui::setFontZ(-6); } @@ -88,18 +116,81 @@ void InventorySystem::render(void) Render::textShader.unuse(); } +void InventorySystem::receive(const MouseClickEvent &mce) +{ + (void)mce; +} + void InventorySystem::receive(const KeyDownEvent &kde) { - (void)kde; + if (kde.keycode == SDLK_e) { + fullInventory ^= true; + } } void InventorySystem::add(const std::string& name, int count) { - for (auto& i : items) { - if (i.count == 0) { - i.item = &itemList[name]; - i.count = count; + auto i = std::find_if(items.begin(), items.end(), + [&name](const InventoryEntry& ie) { + // either matching item that isn't filled, or empty slow + return (ie.item != nullptr && ie.item->name == name && ie.count < ie.item->stackSize) || ie.count == 0; + }); + + if (i != items.end()) { + auto& ie = *i; + + // update the slot + if (ie.item == nullptr) { + ie.item = &itemList[name]; + ie.count = count; + } else { + ie.count += count; + } + + // handle overflow + if (ie.count > ie.item->stackSize) { + int diff = ie.count - ie.item->stackSize; + ie.count = ie.item->stackSize; + add(name, diff); + } + } +} + +bool InventorySystem::take(const std::string& name, int count) +{ + using InvIter = std::vector::iterator; + std::forward_list toDelete; + InvIter next = items.begin(); + int taken = 0; + + while (taken < count) { + auto i = std::find_if(next, items.end(), + [&name](const InventoryEntry& ie) { + return ie.item != nullptr && ie.item->name == name; + }); + + if (i == items.end()) + break; + + toDelete.push_front(i); + taken += i->count; + } + + if (taken < count) + return false; + + for (auto& ii : toDelete) { + if (count > ii->count) { + ii->item = nullptr; + count -= ii->count; + ii->count = 0; + } else { + ii->count -= count; + if (ii->count == 0) + ii->item = nullptr; break; } } + + return true; } diff --git a/src/quest.cpp b/src/quest.cpp index 1e343a6..8107eb6 100644 --- a/src/quest.cpp +++ b/src/quest.cpp @@ -1,8 +1,9 @@ #include -#include #include +extern std::vector StringTokenizer(const std::string& str, char delim); + void QuestSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) { (void)en; diff --git a/src/render.cpp b/src/render.cpp index 5cbd11e..303ff1c 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -1,7 +1,14 @@ #include +#include + +#include +#include +#include #include +extern vec2 offset; + static Shader *currentShader = nullptr; void preRender(void); diff --git a/src/shader_utils.cpp b/src/shader_utils.cpp index bc9375c..85060e2 100644 --- a/src/shader_utils.cpp +++ b/src/shader_utils.cpp @@ -1,10 +1,9 @@ +#include + #include #include using namespace std; -#include -//#include - /** * Store all the file's contents in memory, useful to pass shaders * source code to OpenGL. Using SDL_RWops for Android asset support. diff --git a/src/texture.cpp b/src/texture.cpp index 721e5cb..ad06967 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -1,7 +1,12 @@ +#include + #include #include -#include +#include + +#include +#include namespace Colors { diff --git a/src/ui.cpp b/src/ui.cpp index 552884f..dad9454 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -1,5 +1,14 @@ #include +#include +#include FT_FREETYPE_H + +#include +#include +#include +#include +#include + #include #include #include @@ -12,6 +21,7 @@ #include #include + using namespace std::literals::chrono_literals; extern Menu *currentMenu; @@ -40,24 +50,19 @@ SDL_Keycode getControl(int index) static FT_Library ftl; static FT_Face ftf; -typedef struct { +struct FT_Info { vec2 wh; vec2 bl; vec2 ad; -} FT_Info; + GLuint tex; -static std::vector ftdat16 (93, { { 0, 0 }, { 0, 0 }, { 0, 0 } }); -static std::vector ftex16 (93, 0); -static bool ft16loaded = false; - -static std::vector ftdat24 (93, { { 0, 0 }, { 0, 0 }, { 0, 0 } }); -static std::vector ftex24 (93, 0); -static bool ft24loaded = false; + FT_Info(void) + : tex(0) {} +}; -static auto *ftdat = &ftdat16; -static auto *ftex = &ftex16; +static std::vector ftData (93); -static Color fontColor (255, 255, 255, 255); +static Color fontColor (255, 255, 255); /* * Variables for dialog boxes / options. @@ -87,13 +92,15 @@ Mix_Chunk *sanic; static GLuint pageTex = 0; static bool pageTexReady = false; -void loadFontSize(int size, std::vector &tex, std::vector &dat) +void loadFontSize(int size, std::vector &data) { - FT_Set_Pixel_Sizes(ftf,0,size); + FT_Set_Pixel_Sizes(ftf, 0, size); // pre-render 'all' the characters - glDeleteTextures(93, tex.data()); - glGenTextures(93, tex.data()); // Generate new texture name/locations? + for (auto& d : data) { + glDeleteTextures(1, &d.tex); + glGenTextures(1, &d.tex); // Generate new texture name/locations? + } for (char i = 33; i < 126; i++) { // load the character from the font family file @@ -101,7 +108,7 @@ void loadFontSize(int size, std::vector &tex, std::vector &dat) UserError("Error! Unsupported character " + i); // transfer the character's bitmap (?) to a texture for rendering - glBindTexture(GL_TEXTURE_2D, tex[i - 33]); + glBindTexture(GL_TEXTURE_2D, data[i - 33].tex); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S , GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T , GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER , GL_LINEAR); @@ -118,7 +125,7 @@ void loadFontSize(int size, std::vector &tex, std::vector &dat) for (auto j = buf.size(); j--;) buf[j] ^= !g->bitmap.buffer[j] ? buf[j] : 0; - auto& d = dat[i - 33]; + auto& d = data[i - 33]; d.wh.x = g->bitmap.width; d.wh.y = g->bitmap.rows; d.bl.x = g->bitmap_left; @@ -220,9 +227,6 @@ namespace ui { #ifdef DEBUG DEBUG_printf("Using font %s\n",ttf); #endif // DEBUG - - ft16loaded = false; - ft24loaded = false; } /* @@ -230,17 +234,8 @@ namespace ui { */ void setFontSize(unsigned int size) { - auto& loaded = (size == 16) ? ft16loaded : ft24loaded; - auto& tex = (size == 16) ? ftex16 : ftex24; - auto& dat = (size == 16) ? ftdat16 : ftdat24; - - if (!loaded) { - loadFontSize(fontSize = size, tex, dat); - loaded = true; - } - ftex = &tex; - ftdat = &dat; fontSize = size; + loadFontSize(size, ftData); } /* @@ -261,7 +256,7 @@ namespace ui { * Draws a character at the specified coordinates, aborting if the character is unknown. */ vec2 putChar(float xx,float yy,char c){ - const auto& ch = (*ftdat)[c - 33]; + const auto& ch = ftData[c - 33]; int x = xx, y = yy; // get dimensions of the rendered character @@ -274,7 +269,7 @@ namespace ui { // draw the character glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, (*ftex)[c - 33]); + glBindTexture(GL_TEXTURE_2D, ch.tex); Render::textShader.use(); Render::textShader.enable(); @@ -381,7 +376,7 @@ namespace ui { width += fontSize / 2; break; default: - width += (*ftdat)[i].wh.x + fontSize * 0.1f; + width += ftData[i].wh.x + fontSize * 0.1f; break; } } while(s[++i]); @@ -974,8 +969,7 @@ namespace ui { setFontColor(255,255,255,255); } - if (currentMenu != nullptr) - menu::draw(); + menu::draw(); // draw the mouse static const Texture mouseTex ("assets/goodmouse.png"); @@ -1181,9 +1175,6 @@ using namespace ui; void InputSystem::receive(const MainSDLEvent& event) { - if (currentMenu != nullptr) - return; - const auto& e = event.event; auto& ev = game::events; @@ -1213,9 +1204,6 @@ void InputSystem::receive(const MainSDLEvent& event) //case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONDOWN: - if (currentMenu != nullptr) - break; - ev.emit(mouse, e.button.button); // run actions? diff --git a/src/ui_menu.cpp b/src/ui_menu.cpp index c0bd3fb..6ef06c1 100644 --- a/src/ui_menu.cpp +++ b/src/ui_menu.cpp @@ -1,12 +1,16 @@ #include +#include #include +#include #include #include #include -extern Menu *currentMenu; +extern vec2 offset; + +static Menu* currentMenu = nullptr; static Menu pauseMenu; static Menu optionsMenu; @@ -14,24 +18,10 @@ static Menu controlsMenu; void Menu::gotoParent(void) { - if (!parent) { - currentMenu = nullptr; + if (parent == nullptr) game::config::update(); - } else { + else currentMenu = parent; - } -} - -inline void segFault(void) -{ - ++*((int *)0); -} - -void quitGame(void) -{ - game::config::update(); - game::config::save(); - game::endGame(); } std::string& deleteWord(std::string& s) @@ -106,93 +96,109 @@ void setControlF(unsigned int index, menuItem &m) namespace ui { namespace menu { - menuItem createButton(vec2 l, dim2 d, Color c, const char* t, menuFunc f) { - menuItem temp; + menuItem createButton(vec2 l, dim2 d, Color c, const char* t, MenuAction f) { + menuItem temp (l, d, c, t); temp.member = 0; - temp.loc = l; - temp.dim = d; - temp.color = c; - temp.text = t; temp.button.func = f; - temp.child = nullptr; return temp; } - menuItem createChildButton(vec2 l, dim2 d, Color c, const char* t, Menu *_child) { - menuItem temp; + menuItem createChildButton(vec2 l, dim2 d, Color c, const char* t, Menu* _child) { + menuItem temp (l, d, c, t, _child); temp.member = -1; - temp.loc = l; - temp.dim = d; - temp.color = c; - temp.text = t; temp.button.func = nullptr; - temp.child = _child; return temp; } menuItem createParentButton(vec2 l, dim2 d, Color c, const char* t) { - menuItem temp; + menuItem temp (l, d, c, t); temp.member = -2; - temp.loc = l; - temp.dim = d; - temp.color = c; - temp.text = t; temp.button.func = nullptr; - temp.child = nullptr; return temp; } menuItem createSlider(vec2 l, dim2 d, Color c, float min, float max, const char* t, float* v) { - menuItem temp; + menuItem temp (l, d, c, t); temp.member = 1; - temp.loc = l; - temp.dim = d; - temp.color = c; temp.slider.minValue = min; temp.slider.maxValue = max; - temp.text = t; temp.slider.var = v; temp.slider.sliderLoc = *v; - temp.child = nullptr; return temp; } void init(void) { + dim2 obSize (256, 75); + Color black (0, 0, 0); // Create the main pause menu - pauseMenu.items.push_back(ui::menu::createParentButton({-128,100},{256,75},{0.0f,0.0f,0.0f}, "Resume")); - pauseMenu.items.push_back(ui::menu::createChildButton({-128, 0},{256,75},{0.0f,0.0f,0.0f}, "Options", &optionsMenu)); - pauseMenu.items.push_back(ui::menu::createChildButton({-128,-100},{256,75},{0.0f,0.0f,0.0f}, "Controls", &controlsMenu)); - pauseMenu.items.push_back(ui::menu::createButton({-128,-200},{256,75},{0.0f,0.0f,0.0f}, "Save and Quit", quitGame)); - pauseMenu.items.push_back(ui::menu::createButton({-128,-300},{256,75},{0.0f,0.0f,0.0f}, "Segfault", segFault)); + pauseMenu.items.push_back(ui::menu::createParentButton( + vec2(-128, 100), obSize, black, "Resume")); + + pauseMenu.items.push_back(ui::menu::createChildButton( + vec2(-128, 0), obSize, black, "Options", &optionsMenu)); + + pauseMenu.items.push_back(ui::menu::createChildButton( + vec2(-128, -100), obSize, black, "Controls", &controlsMenu)); + + pauseMenu.items.push_back(ui::menu::createButton( + vec2(-128, -200), obSize, black, "Save and Quit", + []() { + game::config::update(); // TODO necessary? + game::config::save(); + game::endGame(); + } )); + + pauseMenu.items.push_back(ui::menu::createButton( + vec2(-128, -300), obSize, black, "Segfault", + []() { ++*((int *)0); } )); // Create the options (sound) menu - optionsMenu.items.push_back(ui::menu::createSlider({0-static_cast(game::SCREEN_WIDTH)/4,0-(512/2)}, {50,512}, {0.0f, 0.0f, 0.0f}, 0, 100, "Master", &game::config::VOLUME_MASTER)); - optionsMenu.items.push_back(ui::menu::createSlider({-200,100}, {512,50}, {0.0f, 0.0f, 0.0f}, 0, 100, "Music", &game::config::VOLUME_MUSIC)); - optionsMenu.items.push_back(ui::menu::createSlider({-200,000}, {512,50}, {0.0f, 0.0f, 0.0f}, 0, 100, "SFX", &game::config::VOLUME_SFX)); + optionsMenu.items.push_back(ui::menu::createSlider( + vec2(-static_cast(game::SCREEN_WIDTH) / 4, -(512/2)), dim2(50, 512), + black, 0, 100, "Master", &game::config::VOLUME_MASTER)); + optionsMenu.items.push_back(ui::menu::createSlider( + vec2(-200, 100), dim2(512, 50), black, 0, 100, "Music", &game::config::VOLUME_MUSIC)); + optionsMenu.items.push_back(ui::menu::createSlider( + vec2(-200, 0), dim2(512, 50), black, 0, 100, "SFX", &game::config::VOLUME_SFX)); + optionsMenu.parent = &pauseMenu; // Create the controls menu - controlsMenu.items.push_back(ui::menu::createButton({-450,300}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Up: ", nullptr)); - controlsMenu.items.back().button.func = [](){ setControlF(0, controlsMenu.items[0]); }; - controlsMenu.items.push_back(ui::menu::createButton({-450,200}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Left: ", nullptr)); - controlsMenu.items.back().button.func = [](){ setControlF(1, controlsMenu.items[1]); }; - controlsMenu.items.push_back(ui::menu::createButton({-450,100}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Right: ", nullptr)); - controlsMenu.items.back().button.func = [](){ setControlF(2, controlsMenu.items[2]); }; - controlsMenu.items.push_back(ui::menu::createButton({-450,0}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Sprint: ", nullptr)); - controlsMenu.items.back().button.func = [](){ setControlF(3, controlsMenu.items[3]); }; - controlsMenu.items.push_back(ui::menu::createButton({-450,-100}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Creep: ", nullptr)); - controlsMenu.items.back().button.func = [](){ setControlF(4, controlsMenu.items[4]); }; - controlsMenu.items.push_back(ui::menu::createButton({-450,-200}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Inventory: ", nullptr)); - controlsMenu.items.back().button.func = [](){ setControlF(5, controlsMenu.items[5]); }; + dim2 cbSize (400, 75); + controlsMenu.items.push_back(ui::menu::createButton( + vec2(-450, 300), cbSize, black, "Up: ", + []() { setControlF(0, controlsMenu.items[0]); } )); + + controlsMenu.items.push_back(ui::menu::createButton( + vec2(-450, 200), cbSize, black, "Left: ", + []() { setControlF(1, controlsMenu.items[1]); } )); + + controlsMenu.items.push_back(ui::menu::createButton( + vec2(-450, 100), cbSize, black, "Right: ", + []() { setControlF(2, controlsMenu.items[2]); } )); + + controlsMenu.items.push_back(ui::menu::createButton( + vec2(-450, 0), cbSize, black, "Sprint: ", + []() { setControlF(3, controlsMenu.items[3]); } )); + + controlsMenu.items.push_back(ui::menu::createButton( + vec2(-450, -100), cbSize, black, "Creep: ", + []() { setControlF(4, controlsMenu.items[4]); } )); + + controlsMenu.items.push_back(ui::menu::createButton( + vec2(-450, -200), cbSize, black, "Inventory: ", + []() { setControlF(5, controlsMenu.items[5]); } )); + controlsMenu.parent = &pauseMenu; + initControls(&controlsMenu); } @@ -201,8 +207,11 @@ namespace ui { } void draw(void) { - auto SCREEN_WIDTH = game::SCREEN_WIDTH; - auto SCREEN_HEIGHT = game::SCREEN_HEIGHT; + if (currentMenu == nullptr) + return; + + auto& SCREEN_WIDTH = game::SCREEN_WIDTH; + auto& SCREEN_HEIGHT = game::SCREEN_HEIGHT; SDL_Event e; diff --git a/src/window.cpp b/src/window.cpp index 992e3c1..f2c250b 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include diff --git a/src/world.cpp b/src/world.cpp index 1d29bfe..4f2f99c 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -12,6 +12,12 @@ #include using namespace std::literals::chrono_literals; +#include +#include +#include +#include +#include + // local game headers #include #include @@ -369,7 +375,8 @@ void WorldSystem::load(const std::string& file) entity.assign(g); } else if (tname == "Name") { - entity.assign(coalesce(wxml->Attribute("name"), abcd->Attribute("value"))); + auto name = wxml->Attribute("name"); + entity.assign( name != nullptr ? name : abcd->Attribute("value")); } else if (tname == "Dialog") { entity.assign((wxml->BoolAttribute("hasDialog") ? 0 : 9999)); } else if (tname == "Grounded") { -- 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 'src') 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 'src') 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