From: Daniel Karbach Date: Tue, 27 Nov 2012 12:50:36 +0000 (+0100) Subject: implemented item equipping X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=ec18252fd6199492c40b98abab289c375a5a41b7;p=l2e.git implemented item equipping --- diff --git a/src/battle/Hero.h b/src/battle/Hero.h index 1ec4140..5e86f88 100644 --- a/src/battle/Hero.h +++ b/src/battle/Hero.h @@ -54,13 +54,6 @@ public: common::Stats &GetStats() { return stats; } const common::Stats &GetStats() const { return stats; } - common::Item *Weapon() { return master->Equipment(common::Hero::EQUIP_WEAPON); } - common::Item *Armor() { return master->Equipment(common::Hero::EQUIP_ARMOR); } - common::Item *Shield() { return master->Equipment(common::Hero::EQUIP_SHIELD); } - common::Item *Helmet() { return master->Equipment(common::Hero::EQUIP_HELMET); } - common::Item *Ring() { return master->Equipment(common::Hero::EQUIP_RING); } - common::Item *Jewel() { return master->Equipment(common::Hero::EQUIP_JEWEL); } - const common::Item *Weapon() const { return master->Equipment(common::Hero::EQUIP_WEAPON); } const common::Item *Armor() const { return master->Equipment(common::Hero::EQUIP_ARMOR); } const common::Item *Shield() const { return master->Equipment(common::Hero::EQUIP_SHIELD); } diff --git a/src/common/Hero.h b/src/common/Hero.h index f61d0ee..00924d4 100644 --- a/src/common/Hero.h +++ b/src/common/Hero.h @@ -63,11 +63,10 @@ public: bool CanEquip(const Item &) const; bool CanInvoke(const Spell &) const; - Item *Equipment(EquipSlot i) { return equipment[i]; } const Item *Equipment(EquipSlot i) const { return equipment[i]; } bool Equipped(EquipSlot i) const { return equipment[i]; } void RemoveEquipment(EquipSlot i) { equipment[i] = 0; } - void SetEquipment(EquipSlot i, Item *item) { equipment[i] = item; } + void SetEquipment(EquipSlot i, const Item *item) { equipment[i] = item; } std::vector &Spells() { return spells; } const std::vector &Spells() const { return spells; } @@ -105,7 +104,7 @@ private: int useMask; - Item *equipment[EQUIP_COUNT]; + const Item *equipment[EQUIP_COUNT]; // TODO: vector does not seem to be a good choice std::vector spells; diff --git a/src/menu/EquipMenu.cpp b/src/menu/EquipMenu.cpp index c60ea30..199b8e1 100644 --- a/src/menu/EquipMenu.cpp +++ b/src/menu/EquipMenu.cpp @@ -148,7 +148,9 @@ void EquipMenu::HandleEvents(const Input &input) { inventoryMenu.NextRow(); } if (input.JustPressed(Input::ACTION_A)) { - // TODO: equip selected item + EquipSelected(); + inventoryMenu.SetInactive(); + equipmentMenu.SetActive(); } else if (input.JustPressed(Input::ACTION_B)) { inventoryMenu.SetInactive(); equipmentMenu.SetActive(); @@ -337,4 +339,30 @@ void EquipMenu::LoadInventory() { } } +void EquipMenu::EquipSelected() { + Inventory &inv = parent->Game().state->inventory; + Hero &hero = GetHero(); + const Hero::EquipSlot slot = Hero::EquipSlot(equipmentMenu.SelectedIndex()); + + const Item *selected = inventoryMenu.Selected(); + const Item *equipped = equipmentMenu.Selected(); + + if (!hero.CanEquip(*selected)) { + // TODO: error noise and blur + return; + } + + inv.Remove(selected, 1); + if (!inv.Add(equipped, 1)) { + // roll back + inv.Add(selected, 1); + // TODO: error noise, blur, message? + return; + } + + hero.SetEquipment(slot, selected); + LoadEquipment(); + LoadInventory(); +} + } diff --git a/src/menu/EquipMenu.h b/src/menu/EquipMenu.h index 24e9789..f8e9e78 100644 --- a/src/menu/EquipMenu.h +++ b/src/menu/EquipMenu.h @@ -52,6 +52,7 @@ private: bool InventoryVisible() const; void LoadInventory(); + void EquipSelected(); void RenderStatus(SDL_Surface *screen, const geometry::Vector &offset) const; void RenderStats(SDL_Surface *screen, const geometry::Vector &offset) const;