From 5121f4215d725f492bea084fb94900d7e5972743 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 21 Aug 2012 20:58:48 +0200 Subject: [PATCH] moved spell and ikari menu initialization to Hero --- src/battle/BattleState.cpp | 92 +----------------------------------- src/battle/BattleState.h | 2 - src/battle/Hero.cpp | 97 ++++++++++++++++++++++++++++++++++++++ src/battle/Hero.h | 6 +++ 4 files changed, 105 insertions(+), 92 deletions(-) diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index b7017c9..65bdb91 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -75,9 +75,9 @@ void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) { heroesLayout->CalculatePositions(background->w, background->h, heroesPositions); for (int i(0); i < 4; ++i) { heroes[i].SpellMenu() = res->spellMenuPrototype; - LoadSpellMenu(i); + heroes[i].UpdateSpellMenu(); heroes[i].IkariMenu() = res->ikariMenuPrototype; - LoadIkariMenu(i); + heroes[i].UpdateIkariMenu(res); heroTags[i] = HeroTag(this, i); smallHeroTags[i] = SmallHeroTag(this, i); } @@ -103,94 +103,6 @@ void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) { LoadInventory(); } -void BattleState::LoadSpellMenu(vector::size_type index) { - assert(index >= 0 && index < 4); - heroes[index].SpellMenu().Clear(); - heroes[index].SpellMenu().Reserve(HeroAt(index).Spells().size()); - for (vector::const_iterator i(HeroAt(index).Spells().begin()), end(HeroAt(index).Spells().end()); i != end; ++i) { - bool enabled((*i)->CanUseInBattle() && (*i)->Cost() <= HeroAt(index).Mana()); - heroes[index].SpellMenu().Add((*i)->Name(), *i, enabled, 0, (*i)->Cost()); - } -} - -void BattleState::LoadIkariMenu(vector::size_type index) { - assert(index >= 0 && index < 4); - heroes[index].IkariMenu().Clear(); - heroes[index].IkariMenu().Reserve(6); - - if (HeroAt(index).HasWeapon()) { - heroes[index].IkariMenu().Add( - HeroAt(index).Weapon()->Name(), - HeroAt(index).Weapon(), - HeroAt(index).Weapon()->HasIkari() && HeroAt(index).Weapon()->GetIkari()->Cost() <= HeroAt(index).IP(), - res->weaponMenuIcon, - 0, - HeroAt(index).Weapon()->HasIkari() ? HeroAt(index).Weapon()->GetIkari()->Name() : ""); - } else { - heroes[index].IkariMenu().Add(res->noEquipmentText, 0, false, res->weaponMenuIcon); - } - - if (HeroAt(index).HasArmor()) { - heroes[index].IkariMenu().Add( - HeroAt(index).Armor()->Name(), - HeroAt(index).Armor(), - HeroAt(index).Armor()->HasIkari() && HeroAt(index).Armor()->GetIkari()->Cost() <= HeroAt(index).IP(), - res->armorMenuIcon, - 0, - HeroAt(index).Armor()->HasIkari() ? HeroAt(index).Armor()->GetIkari()->Name() : ""); - } else { - heroes[index].IkariMenu().Add(res->noEquipmentText, 0, false, res->armorMenuIcon); - } - - if (HeroAt(index).HasShield()) { - heroes[index].IkariMenu().Add( - HeroAt(index).Shield()->Name(), - HeroAt(index).Shield(), - HeroAt(index).Shield()->HasIkari() && HeroAt(index).Shield()->GetIkari()->Cost() <= HeroAt(index).IP(), - res->shieldMenuIcon, - 0, - HeroAt(index).Shield()->HasIkari() ? HeroAt(index).Shield()->GetIkari()->Name() : ""); - } else { - heroes[index].IkariMenu().Add(res->noEquipmentText, 0, false, res->shieldMenuIcon); - } - - if (HeroAt(index).HasHelmet()) { - heroes[index].IkariMenu().Add( - HeroAt(index).Helmet()->Name(), - HeroAt(index).Helmet(), - HeroAt(index).Helmet()->HasIkari() && HeroAt(index).Helmet()->GetIkari()->Cost() <= HeroAt(index).IP(), - res->helmetMenuIcon, - 0, - HeroAt(index).Helmet()->HasIkari() ? HeroAt(index).Helmet()->GetIkari()->Name() : ""); - } else { - heroes[index].IkariMenu().Add(res->noEquipmentText, 0, false, res->helmetMenuIcon); - } - - if (HeroAt(index).HasRing()) { - heroes[index].IkariMenu().Add( - HeroAt(index).Ring()->Name(), - HeroAt(index).Ring(), - HeroAt(index).Ring()->HasIkari() && HeroAt(index).Ring()->GetIkari()->Cost() <= HeroAt(index).IP(), - res->ringMenuIcon, - 0, - HeroAt(index).Ring()->HasIkari() ? HeroAt(index).Ring()->GetIkari()->Name() : ""); - } else { - heroes[index].IkariMenu().Add(res->noEquipmentText, 0, false, res->ringMenuIcon); - } - - if (HeroAt(index).HasJewel()) { - heroes[index].IkariMenu().Add( - HeroAt(index).Jewel()->Name(), - HeroAt(index).Jewel(), - HeroAt(index).Jewel()->HasIkari() && HeroAt(index).Jewel()->GetIkari()->Cost() <= HeroAt(index).IP(), - res->jewelMenuIcon, - 0, - HeroAt(index).Jewel()->HasIkari() ? HeroAt(index).Jewel()->GetIkari()->Name() : ""); - } else { - heroes[index].IkariMenu().Add(res->noEquipmentText, 0, false, res->jewelMenuIcon); - } -} - void BattleState::LoadInventory() { const Inventory &inv(*res->inventory); itemMenu.Clear(); diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index a4f4037..3561609 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -161,8 +161,6 @@ public: void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector &offset); private: - void LoadSpellMenu(std::vector::size_type heroIndex); - void LoadIkariMenu(std::vector::size_type heroIndex); void LoadInventory(); Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const; diff --git a/src/battle/Hero.cpp b/src/battle/Hero.cpp index a3361c6..17ec08a 100644 --- a/src/battle/Hero.cpp +++ b/src/battle/Hero.cpp @@ -7,6 +7,16 @@ #include "Hero.h" +#include "AttackChoice.h" +#include "Resources.h" +#include "../common/Ikari.h" +#include "../common/Item.h" +#include "../common/Spell.h" + +using common::Ikari; +using common::Spell; +using std::vector; + namespace battle { Hero::Hero() @@ -53,4 +63,91 @@ void Hero::SubtractHealth(int amount) { } } + +void Hero::UpdateSpellMenu() { + SpellMenu().Clear(); + SpellMenu().Reserve(Spells().size()); + for (vector::const_iterator i(Spells().begin()), end(Spells().end()); i != end; ++i) { + bool enabled((*i)->CanUseInBattle() && (*i)->Cost() <= Mana()); + SpellMenu().Add((*i)->Name(), *i, enabled, 0, (*i)->Cost()); + } +} + +void Hero::UpdateIkariMenu(const Resources *res) { + IkariMenu().Clear(); + IkariMenu().Reserve(6); + + if (HasWeapon()) { + IkariMenu().Add( + Weapon()->Name(), + Weapon(), + Weapon()->HasIkari() && Weapon()->GetIkari()->Cost() <= IP(), + res->weaponMenuIcon, + 0, + Weapon()->HasIkari() ? Weapon()->GetIkari()->Name() : ""); + } else { + IkariMenu().Add(res->noEquipmentText, 0, false, res->weaponMenuIcon); + } + + if (HasArmor()) { + IkariMenu().Add( + Armor()->Name(), + Armor(), + Armor()->HasIkari() && Armor()->GetIkari()->Cost() <= IP(), + res->armorMenuIcon, + 0, + Armor()->HasIkari() ? Armor()->GetIkari()->Name() : ""); + } else { + IkariMenu().Add(res->noEquipmentText, 0, false, res->armorMenuIcon); + } + + if (HasShield()) { + IkariMenu().Add( + Shield()->Name(), + Shield(), + Shield()->HasIkari() && Shield()->GetIkari()->Cost() <= IP(), + res->shieldMenuIcon, + 0, + Shield()->HasIkari() ? Shield()->GetIkari()->Name() : ""); + } else { + IkariMenu().Add(res->noEquipmentText, 0, false, res->shieldMenuIcon); + } + + if (HasHelmet()) { + IkariMenu().Add( + Helmet()->Name(), + Helmet(), + Helmet()->HasIkari() && Helmet()->GetIkari()->Cost() <= IP(), + res->helmetMenuIcon, + 0, + Helmet()->HasIkari() ? Helmet()->GetIkari()->Name() : ""); + } else { + IkariMenu().Add(res->noEquipmentText, 0, false, res->helmetMenuIcon); + } + + if (HasRing()) { + IkariMenu().Add( + Ring()->Name(), + Ring(), + Ring()->HasIkari() && Ring()->GetIkari()->Cost() <= IP(), + res->ringMenuIcon, + 0, + Ring()->HasIkari() ? Ring()->GetIkari()->Name() : ""); + } else { + IkariMenu().Add(res->noEquipmentText, 0, false, res->ringMenuIcon); + } + + if (HasJewel()) { + IkariMenu().Add( + Jewel()->Name(), + Jewel(), + Jewel()->HasIkari() && Jewel()->GetIkari()->Cost() <= IP(), + res->jewelMenuIcon, + 0, + Jewel()->HasIkari() ? Jewel()->GetIkari()->Name() : ""); + } else { + IkariMenu().Add(res->noEquipmentText, 0, false, res->jewelMenuIcon); + } +} + } diff --git a/src/battle/Hero.h b/src/battle/Hero.h index 275db6d..44d0b4d 100644 --- a/src/battle/Hero.h +++ b/src/battle/Hero.h @@ -26,6 +26,8 @@ namespace graphics { namespace battle { +class Resources; + class Hero { public: @@ -93,6 +95,10 @@ public: AttackChoice &GetAttackChoice() { return attackChoice; } const AttackChoice &GetAttackChoice() const { return attackChoice; } +public: + void UpdateSpellMenu(); + void UpdateIkariMenu(const Resources *); + // temporary setters until loader is implemented public: void SetName(const char *n) { name = n; } -- 2.39.2