]> git.localhorst.tv Git - l2e.git/blobdiff - src/battle/states/PerformAttacks.cpp
reduced title bar time
[l2e.git] / src / battle / states / PerformAttacks.cpp
index 6c318aaf04138e86c627a94429dcdec23e260911..36b7b1630d41477fa23ba7055ed629ee39078a8c 100644 (file)
@@ -7,7 +7,6 @@
 
 #include "PerformAttacks.h"
 
-#include "../AttackAnimation.h"
 #include "../BattleState.h"
 #include "../Hero.h"
 #include "../Monster.h"
@@ -34,12 +33,13 @@ namespace battle {
 
 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
        ctrl = &c;
-       battle->WriteOrder(order);
+       battle->CalculateAttackOrder();
        numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
        numberPosition.reserve(numberAnimation.size());
 }
 
 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
+       battle->ClearAllAttacks();
        ctrl = 0;
 }
 
@@ -58,48 +58,39 @@ void PerformAttacks::Resize(int width, int height) {
 
 
 void PerformAttacks::HandleEvents(const Input &input) {
-       if (attackAnimation) attackAnimation->Update();
-       CheckNumberAnimation();
+       CheckAnimations();
        if (HasAnimationsRunning()) return;
        ResetAnimation();
-       AdvanceCursor();
-       if (Finished()) {
-               battle->ClearAllAttacks();
+       battle->ApplyDamage();
+       battle->NextAttack();
+       if (battle->AttacksFinished()) {
                ctrl->PopState();
                return;
        }
 
-       if (order[cursor].isMonster) {
-               const Monster &monster(battle->MonsterAt(order[cursor].index));
+       battle->CalculateDamage();
+
+       if (battle->CurrentAttack().isMonster) {
+               Monster &monster(battle->MonsterAt(battle->CurrentAttack().index));
                titleBarText = monster.Name();
-               moveAnimation = 0;
+               moveAnimation = monster.AttackAnimation();
+               targetAnimation = monster.MeleeAnimation();
+               AddNumberAnimations(battle->MonsterAttackChoiceAt(battle->CurrentAttack().index).Selection());
        } else {
-               Hero &hero(battle->HeroAt(order[cursor].index));
-               const AttackChoice &ac(battle->AttackChoiceAt(order[cursor].index));
+               Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
+               const AttackChoice &ac(battle->AttackChoiceAt(battle->CurrentAttack().index));
 
                switch (ac.GetType()) {
                        case AttackChoice::SWORD:
                                if (hero.HasWeapon()) {
                                        titleBarText = hero.Weapon()->Name();
-                                       moveAnimation = hero.AttackAnimation();
+                                       targetAnimation = hero.Weapon()->AttackAnimation();
                                } else {
                                        titleBarText = "Melee attack!";
-                                       if (hero.MeleeAnimation()) {
-                                               moveAnimation = 0;
-                                               attackAnimation = hero.MeleeAnimation();
-                                       } else {
-                                               moveAnimation = hero.AttackAnimation();
-                                               attackAnimation = 0;
-                                       }
-                               }
-                               numberAnimation.push_back(NumberAnimation(15, battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
-                               if (ac.Selection().TargetsEnemies()) {
-                                       numberPosition.push_back(
-                                                       battle->MonsterPositions()[ac.Selection().SingleSelection()]);
-                               } else {
-                                       numberPosition.push_back(
-                                                       battle->HeroesPositions()[ac.Selection().SingleSelection()]);
+                                       targetAnimation = hero.MeleeAnimation();
                                }
+                               moveAnimation = hero.AttackAnimation();
+                               AddNumberAnimations(ac.Selection());
                                break;
                        case AttackChoice::MAGIC:
                                titleBarText = ac.GetSpell()->Name();
@@ -130,17 +121,52 @@ void PerformAttacks::HandleEvents(const Input &input) {
                }
        }
 
-       if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(1500);
+       if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850);
        if (moveAnimation) moveAnimation->Start(*this);
-       if (attackAnimation) attackAnimation->Start(battle, this);
+       if (targetAnimation) {
+               targetAnimationTimer = GraphicsTimers().StartCountdown(150);
+       } else {
+               targetAnimationTimer.Clear();
+       }
 }
 
-void PerformAttacks::CheckNumberAnimation() {
-       if (moveAnimation && moveAnimation->Running()) return;
-       if (attackAnimation && !attackAnimation->Finished()) return;
-       if (moveAnimation || attackAnimation) {
+void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) {
+       if (ts.TargetsEnemies()) {
+               for (int i(0); i < battle->MaxMonsters(); ++i) {
+                       if (ts.IsBad(i)) {
+                               numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
+                               numberPosition.push_back(
+                                               battle->MonsterPositions()[i]);
+                       } else if (ts.IsGood(i)) {
+                               numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
+                               numberPosition.push_back(
+                                               battle->MonsterPositions()[i]);
+                       }
+               }
+       } else {
+               for (int i(0); i < battle->NumHeroes(); ++i) {
+                       if (ts.IsBad(i)) {
+                               numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
+                               numberPosition.push_back(
+                                               battle->HeroesPositions()[i]);
+                       } else if (ts.IsGood(i)) {
+                               numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
+                               numberPosition.push_back(
+                                               battle->HeroesPositions()[i]);
+                       }
+               }
+       }
+}
+
+void PerformAttacks::CheckAnimations() {
+       if (targetAnimation && targetAnimationTimer.JustHit()) {
+               targetAnimation->Start(*this);
+       }
+       if (moveAnimation && !moveAnimation->Finished()) return;
+       if (targetAnimation && !targetAnimation->Finished()) return;
+       if (moveAnimation || targetAnimation) {
                moveAnimation = 0;
-               attackAnimation = 0;
+               targetAnimation = 0;
                for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
                        i->Start(*this);
                }
@@ -154,6 +180,7 @@ void PerformAttacks::CheckNumberAnimation() {
 bool PerformAttacks::HasAnimationsRunning() const {
        if (titleBarTimer.Running()) return true;
        if (moveAnimation && moveAnimation->Running()) return true;
+       if (targetAnimation && targetAnimation->Running()) return true;
        for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
                if (i->Running()) return true;
        }
@@ -161,25 +188,19 @@ bool PerformAttacks::HasAnimationsRunning() const {
 }
 
 void PerformAttacks::ResetAnimation() {
-       if (moveAnimation) moveAnimation->Stop();
-       if (attackAnimation) attackAnimation = 0;
+       if (moveAnimation) {
+               moveAnimation->Stop();
+               moveAnimation = 0;
+       }
+       if (targetAnimation) {
+               targetAnimation->Stop();
+               targetAnimation = 0;
+       }
        titleBarTimer.Clear();
        numberAnimation.clear();
        numberPosition.clear();
 }
 
-void PerformAttacks::AdvanceCursor() {
-       ++cursor;
-       while (cursor < int(order.size())) {
-               if (order[cursor].isMonster) {
-                       if (battle->MonsterAt(order[cursor].index).Health() > 0) break;
-               } else {
-                       if (battle->HeroAt(order[cursor].index).Health() > 0) break;
-               }
-               ++cursor;
-       }
-}
-
 
 void PerformAttacks::UpdateWorld(float deltaT) {
 
@@ -193,10 +214,10 @@ void PerformAttacks::Render(SDL_Surface *screen) {
        battle->RenderSmallHeroTags(screen, offset);
        RenderTitleBar(screen, offset);
        RenderNumbers(screen, offset);
-       if (attackAnimation) attackAnimation->Render(screen, offset);
+       RenderTargetAnimation(screen, offset);
 }
 
-void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) {
+void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
        if (!titleBarText || !titleBarTimer.Running()) return;
 
        int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
@@ -208,7 +229,7 @@ void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offs
        battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
 }
 
-void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) {
+void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
        for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
                if (numberAnimation[i].Running()) {
                        Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
@@ -217,4 +238,15 @@ void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offse
        }
 }
 
+void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector<int> &offset) const {
+       if (!targetAnimation || !targetAnimation->Running()) return;
+       const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection());
+       const vector<Point<int> > &positions(ts.TargetsHeroes() ? battle->HeroesPositions() : battle->MonsterPositions());
+       for (vector<Point<int> >::size_type i(0), end(positions.size()); i < end; ++i) {
+               if (ts.IsSelected(i)) {
+                       targetAnimation->DrawCenter(screen, positions[i] + offset);
+               }
+       }
+}
+
 }