]> git.localhorst.tv Git - l2e.git/blobdiff - src/battle/states/SwapHeroes.cpp
extracted battle logic into a class
[l2e.git] / src / battle / states / SwapHeroes.cpp
index f01e60da8603f862f97ad541a321d24c0bad750d..799cff8d07213cd56d8d4dee59f1e9cb59240feb 100644 (file)
@@ -1,46 +1,64 @@
-/*
- * SwapHeroes.cpp
- *
- *  Created on: Aug 10, 2012
- *      Author: holy
- */
-
 #include "SwapHeroes.h"
 
 #include "SelectMoveAction.h"
 #include "../BattleState.h"
 #include "../../app/Application.h"
 #include "../../app/Input.h"
-#include "../../geometry/operators.h"
-#include "../../geometry/Point.h"
+#include "../../math/Vector.h"
 
 using app::Application;
 using app::Input;
-using geometry::Point;
-using geometry::Vector;
+using math::Vector;
 using std::vector;
 
 namespace battle {
 
-void SwapHeroes::EnterState(Application &c, SDL_Surface *screen) {
-       ctrl = &c;
+SwapHeroes::SwapHeroes(Battle *battle, SelectMoveAction *parent)
+: battle(battle)
+, parent(parent)
+, cursor(0)
+, selected(-1)
+, flipFlop(true) {
+
 }
 
-void SwapHeroes::ExitState(Application &c, SDL_Surface *screen) {
-       ctrl = 0;
+
+void SwapHeroes::OnEnterState(SDL_Surface *screen) {
+       OnResize(screen->w, screen->h);
 }
 
-void SwapHeroes::ResumeState(Application &ctrl, SDL_Surface *screen) {
+void SwapHeroes::OnExitState(SDL_Surface *screen) {
 
 }
 
-void SwapHeroes::PauseState(Application &ctrl, SDL_Surface *screen) {
+void SwapHeroes::OnResumeState(SDL_Surface *screen) {
 
 }
 
+void SwapHeroes::OnPauseState(SDL_Surface *screen) {
 
-void SwapHeroes::Resize(int width, int height) {
+}
 
+
+void SwapHeroes::OnResize(int width, int height) {
+       Vector<int> offset(parent->ScreenOffset());
+       // offset the cursor by 1/8th to the left and bottom
+       cursorOffset = Vector<int>(parent->Res().swapCursor->Width() / -8, parent->Res().swapCursor->Height() / 8);
+       indicatorOffset = Vector<int>(0, 0);
+
+       positions.clear();
+       positions.reserve(battle->NumHeroes());
+       for (int i(0), end(battle->NumHeroes()); i < end; ++i) {
+               Vector<int> positionCorrection(
+                               parent->Res().swapCursor->Width() / 2,
+                               parent->HeroTagAt(i).HeroSprite()->Height() - parent->Res().swapCursor->Height() / 2);
+               // indicator offsets are inverted for heroes
+               positionCorrection -= cursorOffset;
+               positions.push_back(parent->HeroTagPositionAt(i)
+                               + parent->HeroTagAt(i).HeroOffset()
+                               + positionCorrection
+                               + offset);
+       }
 }
 
 
@@ -57,7 +75,7 @@ void SwapHeroes::HandleEvents(const Input &input) {
                if (cursor == selected) {
                        selected = -1;
                } else {
-                       ctrl->PopState();
+                       Ctrl().PopState();
                }
        }
 
@@ -108,41 +126,31 @@ void SwapHeroes::MoveLeft() {
        if (cursor > 0) {
                --cursor;
        } else {
-               cursor = battle->Heroes().size();
+               cursor = battle->NumHeroes();
        }
 }
 
 
-void SwapHeroes::UpdateWorld(float deltaT) {
+void SwapHeroes::UpdateWorld(Uint32 deltaT) {
 
 }
 
 void SwapHeroes::Render(SDL_Surface *screen) {
-       Vector<int> offset(battle->CalculateScreenOffset(screen));
        parent->Render(screen);
-       RenderCursors(screen, offset);
+       RenderCursors(screen);
 }
 
-void SwapHeroes::RenderCursors(SDL_Surface *screen, const geometry::Vector<int> &offset) {
-       // offset the cursor by 1/8th to the left and bottom
-       Vector<int> cursorOffset(battle->Res().swapCursor->Width() / -8, battle->Res().swapCursor->Height() / 8);
-       Vector<int> indicatorOffset(0, 0);
-       vector<Point<int> > positions;
-       for (vector<Hero>::size_type i(0), end(battle->Heroes().size()); i < end; ++i) {
-               Vector<int> positionCorrection(battle->Res().swapCursor->Width() / 2, battle->HeroTagAt(i).HeroSprite()->Height() - battle->Res().swapCursor->Height() / 2);
-               // indicator offsets are inverted for heroes
-               positionCorrection -= cursorOffset;
-               positions.push_back(battle->HeroTagPositionAt(i) + battle->HeroTagAt(i).HeroOffset() + positionCorrection);
-       }
+void SwapHeroes::RenderCursors(SDL_Surface *screen) {
        if (flipFlop) {
-               for (vector<Point<int> >::size_type i(0); i < positions.size(); ++i) {
+               for (vector<Vector<int> >::size_type i(0), end(positions.size());
+                               i != end; ++i) {
                        if (selected == int(i)) {
-                               battle->Res().swapCursor->DrawTopRight(screen, positions[i] + offset);
+                               parent->Res().swapCursor->DrawTopRight(screen, positions[i]);
                        }
                }
        }
        flipFlop = !flipFlop;
-       battle->Res().swapCursor->DrawTopRight(screen, positions[cursor] + offset + cursorOffset);
+       parent->Res().swapCursor->DrawTopRight(screen, positions[cursor] + cursorOffset);
 }
 
 }