From adb1e3d5eafba2ef3de1499be286be330afaefef Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sat, 11 Aug 2012 16:15:05 +0200 Subject: [PATCH] made battle's heroes into an array --- src/battle/BattleState.cpp | 17 +++++++++-------- src/battle/BattleState.h | 25 +++++++++++++------------ src/battle/TargetSelection.cpp | 8 ++++---- src/battle/states/PerformAttacks.cpp | 2 +- src/battle/states/SelectTarget.cpp | 2 +- src/battle/states/SwapHeroes.cpp | 4 ++-- 6 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index 9e84206..0f3fad9 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -43,14 +43,15 @@ void BattleState::AddMonster(const Monster &m) { } void BattleState::AddHero(const Hero &h) { - if (heroes.size() >= heroesLayout->NumPositions()) { + if (numHeroes >= 4 || numHeroes >= (int)heroesLayout->NumPositions()) { throw std::overflow_error("too many heroes for layout"); } - heroes.push_back(h); + heroes[numHeroes] = h; + ++numHeroes; } -void BattleState::SwapHeroes(std::vector::size_type lhs, std::vector::size_type rhs) { - if (lhs < 0 || lhs >= heroes.size() || rhs < 0 || rhs >= heroes.size() || lhs == rhs) return; +void BattleState::SwapHeroes(int lhs, int rhs) { + if (lhs < 0 || lhs >= numHeroes || rhs < 0 || rhs >= numHeroes || lhs == rhs) return; std::swap(heroes[lhs], heroes[rhs]); } @@ -63,7 +64,7 @@ void BattleState::Resize(int w, int h) { void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) { monstersLayout->CalculatePositions(background->w, background->h, monsterPositions); heroesLayout->CalculatePositions(background->w, background->h, heroesPositions); - for (vector::size_type i(0), end(heroes.size()); i < end; ++i) { + for (int i(0); i < 4; ++i) { spellMenus[i] = res->spellMenuPrototype; LoadSpellMenu(i); ikariMenus[i] = res->ikariMenuPrototype; @@ -208,7 +209,7 @@ void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) { void BattleState::ClearAllAttacks() { activeHero = -1; - for (vector::size_type i(0), end(heroes.size()); i < end; ++i) { + for (int i(0); i < numHeroes; ++i) { attackChoices[i] = AttackChoice(this); } } @@ -249,7 +250,7 @@ void BattleState::RenderMonsters(SDL_Surface *screen, const Vector &offset) } void BattleState::RenderHeroes(SDL_Surface *screen, const Vector &offset) { - for (vector::size_type i(0), end(heroes.size()); i < end; ++i) { + for (int i(0); i < numHeroes; ++i) { heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset, 0, 1); } } @@ -258,7 +259,7 @@ void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector &offset) int tagHeight(attackTypeMenu.Height()); int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2); - for (vector::size_type i(0), end(heroes.size()); i < end; ++i) { + for (int i(0); i < numHeroes; ++i) { heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero); } } diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 7dbd016..978581f 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -51,6 +51,7 @@ public: , res(res) , attackTypeMenu(res->attackIcons) , moveMenu(res->moveIcons) + , numHeroes(0) , activeHero(-1) , ranAway(false) { } @@ -75,28 +76,28 @@ public: AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; } MoveMenu &GetMoveMenu() { return moveMenu; } - bool HasMoreHeroes() const { return activeHero < (int) heroes.size(); } + bool HasMoreHeroes() const { return activeHero < numHeroes; } void NextHero() { ++activeHero; } bool BeforeFirstHero() const { return activeHero < 0; } void PreviousHero() { --activeHero; } Hero &ActiveHero() { return heroes[activeHero]; } const Hero &ActiveHero() const { return heroes[activeHero]; } - Hero &HeroAt(std::vector::size_type index) { return heroes[index]; } - const Hero &HeroAt(std::vector::size_type index) const { return heroes[index]; } + Hero &HeroAt(int index) { return heroes[index]; } + const Hero &HeroAt(int index) const { return heroes[index]; } Monster &MonsterAt(std::vector::size_type index) { return monsters[index]; } const Monster &MonsterAt(std::vector::size_type index) const { return monsters[index]; } - void SwapHeroes(std::vector::size_type lhs, std::vector::size_type rhs); + void SwapHeroes(int lhs, int rhs); const HeroTag &ActiveHeroTag() const { return heroTags[activeHero]; } - const HeroTag &HeroTagAt(std::vector::size_type index) const { return heroTags[index]; } - const geometry::Point &HeroTagPositionAt(std::vector::size_type index) const { return heroTagPositions[index]; } + const HeroTag &HeroTagAt(int index) const { return heroTags[index]; } + const geometry::Point &HeroTagPositionAt(int index) const { return heroTagPositions[index]; } bool HasChosenAttackType() const { return attackChoices[activeHero].GetType() != AttackChoice::UNDECIDED; } void SetAttackType(AttackChoice::Type t) { attackChoices[activeHero].SetType(t); } AttackChoice &ActiveHeroAttackChoice() { return attackChoices[activeHero]; } const AttackChoice &ActiveHeroAttackChoice() const { return attackChoices[activeHero]; } - const AttackChoice &AttackChoiceAt(std::vector::size_type index) const { return attackChoices[index]; } + const AttackChoice &AttackChoiceAt(int index) const { return attackChoices[index]; } TargetSelection &ActiveHeroTargets() { return attackChoices[activeHero].Selection(); } const TargetSelection &ActiveHeroTargets() const { return attackChoices[activeHero].Selection(); } - bool AttackSelectionDone() const { return activeHero >= (int) heroes.size(); } + bool AttackSelectionDone() const { return activeHero >= numHeroes; } graphics::Menu &GetSpellMenu() { return spellMenus[activeHero]; } const graphics::Menu &GetSpellMenu() const { return spellMenus[activeHero]; } @@ -108,9 +109,8 @@ public: const std::vector > &MonsterPositions() const { return monsterPositions; } bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; } const std::vector > &HeroesPositions() const { return heroesPositions; } - bool HeroPositionOccupied(int index) { return index >= 0 && index < int(heroes.size()); } - std::vector &Heroes() { return heroes; } - const std::vector &Heroes() const { return heroes; } + bool HeroPositionOccupied(int index) { return index >= 0 && index < numHeroes; } + int NumHeroes() const { return numHeroes; } std::vector &Monsters() { return monsters; } const std::vector &Monsters() const { return monsters; } @@ -147,13 +147,14 @@ private: std::vector > monsterPositions; std::vector > heroesPositions; std::vector monsters; - std::vector heroes; + Hero heroes[4]; graphics::Menu spellMenus[4]; graphics::Menu itemMenu; graphics::Menu ikariMenus[4]; HeroTag heroTags[4]; geometry::Point heroTagPositions[4]; AttackChoice attackChoices[4]; + int numHeroes; int activeHero; bool ranAway; diff --git a/src/battle/TargetSelection.cpp b/src/battle/TargetSelection.cpp index 8563907..c557386 100644 --- a/src/battle/TargetSelection.cpp +++ b/src/battle/TargetSelection.cpp @@ -13,7 +13,7 @@ namespace battle { TargetSelection::TargetSelection(BattleState *battle, bool multiple, bool atEnemy) : battle(battle) -, selected(battle ? (battle->MonsterPositions().size() > battle->Heroes().size() ? battle->MonsterPositions().size() : battle->Heroes().size()) : 0, false) +, selected(battle ? ((int)battle->MonsterPositions().size() > battle->NumHeroes() ? (int)battle->MonsterPositions().size() : battle->NumHeroes()) : 0, false) , selection(-1) , cursor(0) , multiple(multiple) @@ -64,7 +64,7 @@ void TargetSelection::MoveRight() { cursor = (cursor + 1) % battle->MonsterPositions().size(); } } else { - cursor = (cursor + 1) % battle->Heroes().size(); + cursor = (cursor + 1) % battle->NumHeroes(); } } @@ -74,7 +74,7 @@ void TargetSelection::MoveDown() { return; } int newCursor(cursor + 2 % 4); - if (newCursor < int(battle->Heroes().size())) { + if (newCursor < battle->NumHeroes()) { cursor = newCursor; } } @@ -84,7 +84,7 @@ void TargetSelection::MoveLeft() { cursor = (cursor + battle->MonsterPositions().size() - 1) % battle->MonsterPositions().size(); FindNextEnemy(); } else { - cursor = (cursor + battle->Heroes().size() - 1) % battle->Heroes().size(); + cursor = (cursor + battle->NumHeroes() - 1) % battle->NumHeroes(); } } diff --git a/src/battle/states/PerformAttacks.cpp b/src/battle/states/PerformAttacks.cpp index 35253ce..e5bd7c9 100644 --- a/src/battle/states/PerformAttacks.cpp +++ b/src/battle/states/PerformAttacks.cpp @@ -72,7 +72,7 @@ void PerformAttacks::HandleEvents(const Input &input) { if (titleBarText) { titleBarText = 0; ++cursor; - if (cursor == (int)battle->Heroes().size()) { + if (cursor == battle->NumHeroes()) { cursor = 0; monsters = true; } diff --git a/src/battle/states/SelectTarget.cpp b/src/battle/states/SelectTarget.cpp index c0ff6cc..8fe31cd 100644 --- a/src/battle/states/SelectTarget.cpp +++ b/src/battle/states/SelectTarget.cpp @@ -99,7 +99,7 @@ void SelectTarget::RenderCursors(SDL_Surface *screen, const geometry::Vector::size_type i(0), end(battle->Heroes().size()); i < end; ++i) { + for (int i(0), end(battle->NumHeroes()); i < end; ++i) { Vector positionCorrection(cursorIcon->Width() / 2, battle->HeroTagAt(i).HeroSprite()->Height() - cursorIcon->Height() / 2); // indicator offsets are inverted for heroes positionCorrection -= cursorOffset; diff --git a/src/battle/states/SwapHeroes.cpp b/src/battle/states/SwapHeroes.cpp index f01e60d..568fe88 100644 --- a/src/battle/states/SwapHeroes.cpp +++ b/src/battle/states/SwapHeroes.cpp @@ -108,7 +108,7 @@ void SwapHeroes::MoveLeft() { if (cursor > 0) { --cursor; } else { - cursor = battle->Heroes().size(); + cursor = battle->NumHeroes(); } } @@ -128,7 +128,7 @@ void SwapHeroes::RenderCursors(SDL_Surface *screen, const geometry::Vector Vector cursorOffset(battle->Res().swapCursor->Width() / -8, battle->Res().swapCursor->Height() / 8); Vector indicatorOffset(0, 0); vector > positions; - for (vector::size_type i(0), end(battle->Heroes().size()); i < end; ++i) { + for (int i(0), end(battle->NumHeroes()); i < end; ++i) { Vector positionCorrection(battle->Res().swapCursor->Width() / 2, battle->HeroTagAt(i).HeroSprite()->Height() - battle->Res().swapCursor->Height() / 2); // indicator offsets are inverted for heroes positionCorrection -= cursorOffset; -- 2.39.2