]> git.localhorst.tv Git - l2e.git/blobdiff - src/battle/states/PerformAttacks.cpp
added number animation
[l2e.git] / src / battle / states / PerformAttacks.cpp
index 4f7a427c6a404a5fbac1ecc3f29de5c1bba936b7..b5cd9bebec80754ee019001b972616b557829e34 100644 (file)
@@ -27,12 +27,15 @@ using app::Application;
 using app::Input;
 using geometry::Point;
 using geometry::Vector;
+using std::vector;
 
 namespace battle {
 
 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
        ctrl = &c;
        battle->WriteOrder(order);
+       numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
+       numberPosition.reserve(numberAnimation.size());
 }
 
 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
@@ -54,23 +57,11 @@ void PerformAttacks::Resize(int width, int height) {
 
 
 void PerformAttacks::HandleEvents(const Input &input) {
-       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 {
-                       if (battle->HeroAt(order[cursor].index).Health() > 0) break;
-               }
-               ++cursor;
-       }
-
-       if (cursor >= int(order.size())) {
+       CheckNumberAnimation();
+       if (HasAnimationsRunning()) return;
+       ResetAnimation();
+       AdvanceCursor();
+       if (Finished()) {
                battle->ClearAllAttacks();
                ctrl->PopState();
                return;
@@ -88,6 +79,14 @@ void PerformAttacks::HandleEvents(const Input &input) {
                        case AttackChoice::SWORD:
                                titleBarText = hero.HasWeapon() ? hero.Weapon()->Name() : "Melee attack!";
                                moveAnimation = hero.AttackAnimation();
+                               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()]);
+                               }
                                break;
                        case AttackChoice::MAGIC:
                                titleBarText = ac.GetSpell()->Name();
@@ -118,10 +117,51 @@ void PerformAttacks::HandleEvents(const Input &input) {
                }
        }
 
-       titleBarTimer = GraphicsTimers().StartCountdown(1500);
+       if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(1500);
        if (moveAnimation) moveAnimation->Start(*this);
 }
 
+void PerformAttacks::CheckNumberAnimation() {
+       if (moveAnimation && moveAnimation->Running()) return;
+       if (!moveAnimation || moveAnimation->JustFinished()) {
+               for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
+                       i->Start(*this);
+               }
+       } else {
+               for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
+                       i->CheckTimers(*this);
+               }
+       }
+}
+
+bool PerformAttacks::HasAnimationsRunning() const {
+       if (titleBarTimer.Running()) return true;
+       if (moveAnimation && moveAnimation->Running()) return true;
+       for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
+               if (i->Running()) return true;
+       }
+       return false;
+}
+
+void PerformAttacks::ResetAnimation() {
+       if (moveAnimation) moveAnimation->Stop();
+       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) {
 
@@ -134,6 +174,7 @@ void PerformAttacks::Render(SDL_Surface *screen) {
        battle->RenderHeroes(screen, offset);
        battle->RenderSmallHeroTags(screen, offset);
        RenderTitleBar(screen, offset);
+       RenderNumbers(screen, offset);
 }
 
 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) {
@@ -148,4 +189,13 @@ 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) {
+       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);
+                       numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
+               }
+       }
+}
+
 }