]> git.localhorst.tv Git - l2e.git/commitdiff
reworked PerformAttacks
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 12 Aug 2012 14:14:05 +0000 (16:14 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 12 Aug 2012 14:14:05 +0000 (16:14 +0200)
src/battle/BattleState.cpp
src/battle/BattleState.h
src/battle/states/PerformAttacks.cpp
src/battle/states/PerformAttacks.h

index 03cbb213623b26bf9cd0383dcfd2a26f7c95b9ad..a4e8158a4339af806392d9e0b7489a9adc3d46b4 100644 (file)
@@ -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) {
+       order.reserve(monsters.size() + NumHeroes());
+       for (int i(0); i < numHeroes; ++i) {
+               order.push_back(Order(i, false));
+       }
+       for (vector<Monster>::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) {
 
 }
index 40dd51d9f671d8b79fb8a9c63d72d2586ad62c14..d0a002716268f1d9209988556e0e56f77a8c3ab1 100644 (file)
@@ -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<Order> &);
+
 public:
        geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
                return geometry::Vector<int>(
index 93a09ef77d8baa79726a59149880a1518364451f..4f7a427c6a404a5fbac1ecc3f29de5c1bba936b7 100644 (file)
@@ -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<int> &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<int>(offset.X(), offset.Y()), battle->BackgroundWidth(), height);
index f6157e1bc9d298468f3cfd73ed3950d74c16591e..e0d3df821a0a66f944884048eef1f6c2fbd0d43c 100644 (file)
 
 #include "../../app/State.h"
 
+#include "../BattleState.h"
 #include "../../geometry/Vector.h"
 
-namespace battle {
+#include <vector>
+
+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<Uint32> fakeMoveTimer;
+       app::Timer<Uint32> titleBarTimer;
+       std::vector<BattleState::Order> order;
        int cursor;
-       bool monsters;
 
 };