From 85ac93ffe31bfeee54aa6167111f1c15f14bc405 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 12 Aug 2012 16:14:05 +0200 Subject: [PATCH] reworked PerformAttacks --- src/battle/BattleState.cpp | 24 +++++ src/battle/BattleState.h | 9 ++ src/battle/states/PerformAttacks.cpp | 156 +++++++++++---------------- src/battle/states/PerformAttacks.h | 14 ++- 4 files changed, 105 insertions(+), 98 deletions(-) diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index 03cbb21..a4e8158 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -233,6 +233,30 @@ void BattleState::ClearAllAttacks() { } +class OrderCompare { + public: + OrderCompare(BattleState *battle) : battle(battle) { } + bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) { + int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).Agility() : battle->HeroAt(lhs.index).Agility()); + int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).Agility() : battle->HeroAt(rhs.index).Agility()); + return lagl > ragl; + } + private: + BattleState *battle; +}; + +void BattleState::WriteOrder(std::vector &order) { + order.reserve(monsters.size() + NumHeroes()); + for (int i(0); i < numHeroes; ++i) { + order.push_back(Order(i, false)); + } + for (vector::size_type i(0), end(monsters.size()); i < end; ++i) { + order.push_back(Order(i, true)); + } + std::sort(order.begin(), order.end(), OrderCompare(this)); +} + + void BattleState::HandleEvents(const Input &input) { } diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 40dd51d..d0a0027 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -118,6 +118,15 @@ public: void SetRunaway() { ranAway = true; } void ClearAllAttacks(); + struct Order { + Order(int index, bool isMonster) + : index(index), isMonster(isMonster) { } + int index; + bool isMonster; + }; + + void WriteOrder(std::vector &); + public: geometry::Vector CalculateScreenOffset(SDL_Surface *screen) const { return geometry::Vector( diff --git a/src/battle/states/PerformAttacks.cpp b/src/battle/states/PerformAttacks.cpp index 93a09ef..4f7a427 100644 --- a/src/battle/states/PerformAttacks.cpp +++ b/src/battle/states/PerformAttacks.cpp @@ -32,6 +32,7 @@ namespace battle { void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) { ctrl = &c; + battle->WriteOrder(order); } void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) { @@ -39,7 +40,7 @@ void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) { } void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) { - fakeMoveTimer = GraphicsTimers().StartCountdown(850); + } void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) { @@ -53,103 +54,72 @@ void PerformAttacks::Resize(int width, int height) { void PerformAttacks::HandleEvents(const Input &input) { - if (fakeMoveTimer.JustHit()) { - if (monsters) { - if (titleBarText) { - titleBarText = 0; - ++cursor; - while (cursor < battle->MaxMonsters() && !battle->MonsterPositionOccupied(cursor)) { - ++cursor; - } - if (cursor >= battle->MaxMonsters()) { - battle->ClearAllAttacks(); - ctrl->PopState(); - } - } else { - if (cursor == 0) { - if (battle->HeroAt(battle->NumHeroes() - 1).AttackAnimation()) { - battle->HeroAt(battle->NumHeroes() - 1).AttackAnimation()->Stop(); - } - if (battle->HeroAt(battle->NumHeroes() - 1).SpellAnimation()) { - battle->HeroAt(battle->NumHeroes() - 1).SpellAnimation()->Stop(); - } - } - titleBarText = battle->MonsterAt(cursor).Name(); - } + if (titleBarTimer.Running() || (moveAnimation && moveAnimation->Running())) return; + + if (moveAnimation) moveAnimation->Stop(); + titleBarTimer.Clear(); + + ++cursor; + + while (cursor < int(order.size())) { + if (order[cursor].isMonster) { + if (battle->MonsterAt(order[cursor].index).Health() > 0) break; } else { - const AttackChoice &ac(battle->AttackChoiceAt(cursor)); - if (titleBarText) { - titleBarText = 0; - - switch (ac.GetType()) { - case AttackChoice::SWORD: - if (battle->HeroAt(cursor).AttackAnimation()) { - battle->HeroAt(cursor).AttackAnimation()->Start(*this); - } - break; - case AttackChoice::MAGIC: - if (battle->HeroAt(cursor).SpellAnimation()) { - battle->HeroAt(cursor).SpellAnimation()->Start(*this); - } - break; - case AttackChoice::DEFEND: - break; - case AttackChoice::IKARI: - if (ac.GetItem()->HasIkari()) { - if (ac.GetItem()->GetIkari()->IsMagical()) { - if (battle->HeroAt(cursor).SpellAnimation()) { - battle->HeroAt(cursor).SpellAnimation()->Start(*this); - } - } else { - if (battle->HeroAt(cursor).AttackAnimation()) { - battle->HeroAt(cursor).AttackAnimation()->Start(*this); - } - } - } - break; - case AttackChoice::ITEM: - break; - case AttackChoice::UNDECIDED: - break; - } + if (battle->HeroAt(order[cursor].index).Health() > 0) break; + } + ++cursor; + } - ++cursor; - if (cursor == battle->NumHeroes()) { - cursor = 0; - monsters = true; - } - } else { - if (cursor > 0) { - if (battle->HeroAt(cursor - 1).AttackAnimation()) { - battle->HeroAt(cursor - 1).AttackAnimation()->Stop(); - } - if (battle->HeroAt(cursor - 1).SpellAnimation()) { - battle->HeroAt(cursor - 1).SpellAnimation()->Stop(); + if (cursor >= int(order.size())) { + battle->ClearAllAttacks(); + ctrl->PopState(); + return; + } + + if (order[cursor].isMonster) { + const Monster &monster(battle->MonsterAt(order[cursor].index)); + titleBarText = monster.Name(); + moveAnimation = 0; + } else { + Hero &hero(battle->HeroAt(order[cursor].index)); + const AttackChoice &ac(battle->AttackChoiceAt(order[cursor].index)); + + switch (ac.GetType()) { + case AttackChoice::SWORD: + titleBarText = hero.HasWeapon() ? hero.Weapon()->Name() : "Melee attack!"; + moveAnimation = hero.AttackAnimation(); + break; + case AttackChoice::MAGIC: + titleBarText = ac.GetSpell()->Name(); + moveAnimation = hero.SpellAnimation(); + break; + case AttackChoice::DEFEND: + titleBarText = "Defends."; + moveAnimation = 0; + break; + case AttackChoice::IKARI: + if (ac.GetItem()->HasIkari()) { + titleBarText = ac.GetItem()->GetIkari()->Name(); + if (ac.GetItem()->GetIkari()->IsMagical()) { + moveAnimation = hero.SpellAnimation(); + } else { + moveAnimation = hero.AttackAnimation(); } } - switch (ac.GetType()) { - case AttackChoice::SWORD: - titleBarText = battle->HeroAt(cursor).HasWeapon() ? battle->HeroAt(cursor).Weapon()->Name() : "Gauntlet"; - break; - case AttackChoice::MAGIC: - titleBarText = ac.GetSpell()->Name(); - break; - case AttackChoice::DEFEND: - titleBarText = "Defends."; - break; - case AttackChoice::IKARI: - titleBarText = ac.GetItem()->HasIkari() ? ac.GetItem()->GetIkari()->Name() : "No Ikari?"; - break; - case AttackChoice::ITEM: - titleBarText = ac.GetItem()->Name(); - break; - case AttackChoice::UNDECIDED: - titleBarText = "WTF???"; - break; - } - } + break; + case AttackChoice::ITEM: + titleBarText = ac.GetItem()->Name(); + moveAnimation = 0; + break; + case AttackChoice::UNDECIDED: + titleBarText = "UNDECIDED"; + moveAnimation = 0; + break; } } + + titleBarTimer = GraphicsTimers().StartCountdown(1500); + if (moveAnimation) moveAnimation->Start(*this); } @@ -167,7 +137,7 @@ void PerformAttacks::Render(SDL_Surface *screen) { } void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector &offset) { - if (!titleBarText) return; + if (!titleBarText || !titleBarTimer.Running()) return; int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight()); battle->Res().titleFrame->Draw(screen, Point(offset.X(), offset.Y()), battle->BackgroundWidth(), height); diff --git a/src/battle/states/PerformAttacks.h b/src/battle/states/PerformAttacks.h index f6157e1..e0d3df8 100644 --- a/src/battle/states/PerformAttacks.h +++ b/src/battle/states/PerformAttacks.h @@ -10,18 +10,21 @@ #include "../../app/State.h" +#include "../BattleState.h" #include "../../geometry/Vector.h" -namespace battle { +#include + +namespace graphics { class Animation; } -class BattleState; +namespace battle { class PerformAttacks : public app::State { public: explicit PerformAttacks(BattleState *battle) - : ctrl(0), battle(battle), titleBarText(0), cursor(0), monsters(false) { } + : ctrl(0), battle(battle), moveAnimation(0), titleBarText(0), cursor(-1) { } public: virtual void EnterState(app::Application &ctrl, SDL_Surface *screen); @@ -41,10 +44,11 @@ private: private: app::Application *ctrl; BattleState *battle; + graphics::Animation *moveAnimation; const char *titleBarText; - app::Timer fakeMoveTimer; + app::Timer titleBarTimer; + std::vector order; int cursor; - bool monsters; }; -- 2.39.2