diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-03-14 21:50:17 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-03-14 21:50:17 -0400 |
commit | bf01660ab468f49d63a133c28131ab21bba3a1a1 (patch) | |
tree | d1b6ef1920ae755ccd0c2b2562e2b988458182ec /src | |
parent | ba651a82d585c181e9632fadba5bb4d683842d44 (diff) |
good item using
Diffstat (limited to 'src')
-rw-r--r-- | src/attack.cpp | 11 | ||||
-rw-r--r-- | src/inventory.cpp | 61 | ||||
-rw-r--r-- | src/player.cpp | 13 |
3 files changed, 53 insertions, 32 deletions
diff --git a/src/attack.cpp b/src/attack.cpp index 525b0c3..fd171a0 100644 --- a/src/attack.cpp +++ b/src/attack.cpp @@ -13,6 +13,12 @@ inline T abs(const T& n) { return n >= 0 ? n : -n; } +bool inrange(float point, float left, float right, float range) +{ + return (left < point + range && left > point - range) || + (right < point + range && right > point - range) || + (point > left && point < right); +} void AttackSystem::receive(const AttackEvent& ae) { @@ -34,10 +40,9 @@ void AttackSystem::update(entityx::EntityManager& en, entityx::EventManager& ev, if (e.has_component<Player>()) return; - if ((pos.x > a.pos.x && pos.x < a.pos.x + shortSlashLength) || - (pos.x + dim.width < a.pos.x && pos.x + dim.width > a.pos.x - shortSlashLength)) { + if (inrange(a.pos.x, pos.x, pos.x + dim.width, shortSlashLength)) { h.health -= a.power; - game::engine.getSystem<ParticleSystem>()->addMultiple(10, ParticleType::DownSlash, + game::engine.getSystem<ParticleSystem>()->addMultiple(15, ParticleType::DownSlash, [&](){ return vec2(pos.x + dim.width / 2, pos.y + dim.height / 2); }, 300, 7); } } diff --git a/src/inventory.cpp b/src/inventory.cpp index d669c00..3e7104e 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -16,17 +16,15 @@ using namespace tinyxml2; extern vec2 offset; -static std::unordered_map<std::string, Item> itemList; -constexpr const char* itemsPath = "config/items.xml"; - -static bool fullInventory = false; - -constexpr int entrySize = 70; -constexpr int hotbarSize = 4; -constexpr float inventoryZ = -5.0f; -constexpr unsigned int rowSize = 8; +inline bool isGoodEntry(const InventoryEntry& ie) { + return (ie.item != nullptr) && (ie.count > 0); +} -static int movingItem = -1; +InventorySystem::InventorySystem(int size) +{ + items.resize(size); + loadItems(); +} void InventorySystem::configure(entityx::EventManager &ev) { @@ -64,7 +62,9 @@ void InventorySystem::render(void) 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); + hotStart = items[0].loc - 10; + hotEnd = items[hotbarSize - 1].loc + vec2(entrySize + 4, entrySize + 10); + ui::drawNiceBox(hotStart, hotEnd, inventoryZ); if (fullInventory) { vec2 start (offset.x - entrySize * rowSize / 2, offset.y + game::SCREEN_HEIGHT / 2 - 180); @@ -74,7 +74,9 @@ void InventorySystem::render(void) start.y -= entrySize; } - ui::drawNiceBox(items[items.size() - rowSize].loc - 10, items[hotbarSize + rowSize - 1].loc + (entrySize + 4), inventoryZ); + fullStart = items[items.size() - rowSize].loc - 10; + fullEnd = items[hotbarSize + rowSize - 1].loc + (entrySize + 4); + ui::drawNiceBox(fullStart, fullEnd, inventoryZ); } else { size = hotbarSize; } @@ -102,7 +104,7 @@ void InventorySystem::render(void) glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, 1); // draw the item - if (i.item != nullptr && i.count > 0) { + if (isGoodEntry(i)) { i.item->sprite.use(); glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); @@ -131,7 +133,7 @@ void InventorySystem::render(void) } } - if (items[0].item != nullptr && items[0].count > 0) { + if (isGoodEntry(items[0])) { Render::textShader.use(); Render::textShader.enable(); @@ -168,17 +170,22 @@ void InventorySystem::receive(const MouseClickEvent &mce) e.destroy(); } }); - } else { - int end = fullInventory ? items.size() : hotbarSize; - movingItem = -1; - for (int i = 0; i < end; i++) { - if (items[i].item == nullptr || items[i].count == 0) - continue; - - if (mce.position > items[i].loc && mce.position < items[i].loc + entrySize) { - movingItem = i; - break; + } else if (mce.button == SDL_BUTTON_LEFT) { + if ((mce.position > hotStart && mce.position < hotEnd) || + (fullInventory && mce.position > fullStart && mce.position < fullEnd)) { + int end = fullInventory ? items.size() : hotbarSize; + movingItem = -1; + for (int i = 0; i < end; i++) { + if (!isGoodEntry(items[i])) + continue; + + if (mce.position > items[i].loc && mce.position < items[i].loc + entrySize) { + movingItem = i; + break; + } } + } else if (isGoodEntry(items[0])) { + game::events.emit<UseItemEvent>(items[0].item, mce.position); } } } @@ -189,7 +196,7 @@ void InventorySystem::receive(const MouseReleaseEvent &mre) 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) { + if (isGoodEntry(items[i])) { std::swap(items[movingItem], items[i]); } else { items[i] = items[movingItem]; @@ -240,7 +247,7 @@ void InventorySystem::add(const std::string& name, int count) auto& ie = *i; // update the slot - if (ie.item == nullptr) { + if (!isGoodEntry(ie)) { ie.item = &itemList[name]; ie.count = count; } else { @@ -266,7 +273,7 @@ bool InventorySystem::take(const std::string& name, int count) while (taken < count) { auto i = std::find_if(next, items.end(), [&name](const InventoryEntry& ie) { - return ie.item != nullptr && ie.item->name == name; + return isGoodEntry(ie) && ie.item->name == name; }); if (i >= items.end()) diff --git a/src/player.cpp b/src/player.cpp index 0a85459..3d959ea 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -97,6 +97,7 @@ void PlayerSystem::configure(entityx::EventManager &ev) { ev.subscribe<KeyUpEvent>(*this); ev.subscribe<KeyDownEvent>(*this); + ev.subscribe<UseItemEvent>(*this); } void PlayerSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) { @@ -200,8 +201,6 @@ void PlayerSystem::receive(const KeyDownEvent &kde) } else if (kc == SDLK_t) { game::time::tick(50); } - if (kc == SDLK_j) - game::events.emit<AttackEvent>(vec2(loc.x, loc.y), AttackType::ShortSlash); } vec2 PlayerSystem::getPosition(void) const @@ -209,3 +208,13 @@ vec2 PlayerSystem::getPosition(void) const auto& loc = *game::entities.component<Position>(player.id()).get(); return vec2(loc.x, loc.y); } + +void PlayerSystem::receive(const UseItemEvent& uie) +{ + if (uie.item->type == "Sword") { + auto loc = getPosition(); + auto &solid = *player.component<Solid>().get(); + loc.x += solid.width / 2, loc.y += solid.height / 2; + game::events.emit<AttackEvent>(loc, AttackType::ShortSlash); + } +} |