]> git.localhorst.tv Git - l2e.git/blobdiff - src/battle/states/PerformAttacks.cpp
extracted battle logic into a class
[l2e.git] / src / battle / states / PerformAttacks.cpp
index f930de955b39a72da9c3255e17d5ec5c162c3c23..2274cfc10a390a15f3dca0f9f4c3fca936528b85 100644 (file)
@@ -1,10 +1,3 @@
-/*
- * PerformAttacks.cpp
- *
- *  Created on: Aug 10, 2012
- *      Author: holy
- */
-
 #include "PerformAttacks.h"
 
 #include "../BattleState.h"
@@ -30,10 +23,24 @@ using std::vector;
 
 namespace battle {
 
+PerformAttacks::PerformAttacks(Battle *battle, BattleState *parent)
+: battle(battle)
+, parent(parent)
+, moveAnimation(0)
+, targetAnimation(0)
+, titleBarText(0)
+, cursor(-1) {
+
+}
+
+
 void PerformAttacks::OnEnterState(SDL_Surface *screen) {
        battle->CalculateAttackOrder();
-       numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
+       numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1
+                       ? battle->MaxMonsters()
+                       : battle->NumHeroes() + 1);
        numberPosition.reserve(numberAnimation.size());
+       OnResize(screen->w, screen->h);
 }
 
 void PerformAttacks::OnExitState(SDL_Surface *screen) {
@@ -50,7 +57,11 @@ void PerformAttacks::OnPauseState(SDL_Surface *screen) {
 
 
 void PerformAttacks::OnResize(int width, int height) {
-
+       const Resources &res = parent->Res();
+       framePosition = parent->ScreenOffset();
+       frameSize = Vector<int>(
+                       parent->Width(),
+                       res.titleFrame->BorderHeight() * 2 + res.titleFont->CharHeight());
 }
 
 
@@ -83,7 +94,7 @@ void PerformAttacks::HandleEvents(const Input &input) {
                AddNumberAnimations(capsule.GetAttackChoice().Selection());
        } else {
                Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
-               const AttackChoice &ac(battle->HeroAt(battle->CurrentAttack().index).GetAttackChoice());
+               const AttackChoice &ac(battle->CurrentAttackAttackChoice());
 
                switch (ac.GetType()) {
                        case AttackChoice::SWORD:
@@ -126,7 +137,12 @@ void PerformAttacks::HandleEvents(const Input &input) {
                }
        }
 
-       if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850);
+       if (titleBarText) {
+               titleBarTimer = GraphicsTimers().StartCountdown(850);
+               textPosition = parent->ScreenOffset() + Vector<int>(
+                               (parent->Width() - std::strlen(titleBarText) * parent->Res().titleFont->CharWidth()) / 2,
+                               parent->Res().titleFrame->BorderHeight());
+       }
        if (moveAnimation.Valid()) {
                moveAnimation.Start(*this);
                if (battle->CurrentAttack().IsMonster()) {
@@ -148,25 +164,25 @@ void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) {
        if (ts.TargetsMonsters()) {
                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));
+                               numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), parent->Res().numberAnimationPrototype, parent->Res().bigNumberSprite));
                                numberPosition.push_back(
-                                               battle->MonsterAt(i).Position());
+                                               battle->MonsterAt(i).Position() + parent->ScreenOffset());
                        } else if (ts.IsGood(i)) {
-                               numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
+                               numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), parent->Res().numberAnimationPrototype, parent->Res().greenNumberSprite));
                                numberPosition.push_back(
-                                               battle->MonsterAt(i).Position());
+                                               battle->MonsterAt(i).Position() + parent->ScreenOffset());
                        }
                }
        } 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));
+                               numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), parent->Res().numberAnimationPrototype, parent->Res().bigNumberSprite));
                                numberPosition.push_back(
-                                               battle->HeroAt(i).Position());
+                                               battle->HeroAt(i).Position() + parent->ScreenOffset());
                        } else if (ts.IsGood(i)) {
-                               numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
+                               numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), parent->Res().numberAnimationPrototype, parent->Res().greenNumberSprite));
                                numberPosition.push_back(
-                                               battle->HeroAt(i).Position());
+                                               battle->HeroAt(i).Position() + parent->ScreenOffset());
                        }
                }
        }
@@ -217,56 +233,51 @@ void PerformAttacks::ResetAnimation() {
 }
 
 
-void PerformAttacks::UpdateWorld(float deltaT) {
+void PerformAttacks::UpdateWorld(Uint32 deltaT) {
 
 }
 
 void PerformAttacks::Render(SDL_Surface *screen) {
-       Vector<int> offset(battle->CalculateScreenOffset(screen));
-       battle->RenderBackground(screen, offset);
-       battle->RenderMonsters(screen, offset);
-       battle->RenderHeroes(screen, offset);
-       battle->RenderCapsule(screen, offset);
-       battle->RenderSmallHeroTags(screen, offset);
-       RenderTitleBar(screen, offset);
-       RenderNumbers(screen, offset);
-       RenderTargetAnimation(screen, offset);
+       parent->RenderBackground(screen);
+       parent->RenderMonsters(screen);
+       parent->RenderHeroes(screen);
+       parent->RenderCapsule(screen);
+       parent->RenderSmallHeroTags(screen);
+       RenderTitleBar(screen);
+       RenderNumbers(screen);
+       RenderTargetAnimation(screen);
 }
 
-void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
+void PerformAttacks::RenderTitleBar(SDL_Surface *screen) const {
        if (!titleBarText || !titleBarTimer.Running()) return;
 
-       int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
-       battle->Res().titleFrame->Draw(screen, offset, battle->Width(), height);
+       parent->Res().titleFrame->Draw(screen, framePosition, frameSize.X(), frameSize.Y());
 
-       Vector<int> textPosition(
-                       (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
-                       battle->Res().titleFrame->BorderHeight());
-       battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
+       parent->Res().titleFont->DrawString(titleBarText, screen, textPosition);
 }
 
-void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
+void PerformAttacks::RenderNumbers(SDL_Surface *screen) 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);
-                       numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
+                       numberAnimation[i].Draw(screen, numberPosition[i] + align);
                }
        }
 }
 
-void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const math::Vector<int> &offset) const {
+void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen) const {
        if (!targetAnimation.Valid() || !targetAnimation.Running()) return;
        const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection());
        if (ts.TargetsHeroes()) {
                for (vector<Vector<int> >::size_type i(0), end(battle->NumHeroes()); i < end; ++i) {
                        if (ts.IsSelected(i)) {
-                               targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + offset);
+                               targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + parent->ScreenOffset());
                        }
                }
        } else {
                for (vector<Vector<int> >::size_type i(0), end(battle->MaxMonsters()); i < end; ++i) {
                        if (ts.IsSelected(i)) {
-                               targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + offset);
+                               targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + parent->ScreenOffset());
                        }
                }
        }