]> git.localhorst.tv Git - l2e.git/blobdiff - src/battle/states/SelectTarget.cpp
extracted battle logic into a class
[l2e.git] / src / battle / states / SelectTarget.cpp
index 1961738623a30a1c9f3ec2f90c340c3a651f4dbe..f9507a17024966158efca526d50193961bd2c8b4 100644 (file)
@@ -1,26 +1,34 @@
-/*
- * SelectTarget.cpp
- *
- *  Created on: Aug 9, 2012
- *      Author: holy
- */
-
 #include "SelectTarget.h"
 
 #include "SelectAttackType.h"
 #include "../BattleState.h"
 #include "../../app/Application.h"
 #include "../../app/Input.h"
+#include "../../math/Vector.h"
 
 using app::Application;
 using app::Input;
-using geometry::Vector;
+using math::Vector;
 using std::vector;
 
 namespace battle {
 
-void SelectTarget::OnEnterState(SDL_Surface *screen) {
+SelectTarget::SelectTarget(
+               Battle *battle,
+               SelectAttackType *parent,
+               TargetSelection *selection,
+               const graphics::Sprite *cursorIcon)
+: battle(battle)
+, parent(parent)
+, selection(selection)
+, cursorIcon(cursorIcon)
+, flipFlop(true) {
+
+}
 
+
+void SelectTarget::OnEnterState(SDL_Surface *screen) {
+       OnResize(screen->w, screen->h);
 }
 
 void SelectTarget::OnExitState(SDL_Surface *screen) {
@@ -37,7 +45,22 @@ void SelectTarget::OnPauseState(SDL_Surface *screen) {
 
 
 void SelectTarget::OnResize(int width, int height) {
+       Vector<int> offset(parent->ScreenOffset());
+       cursorOffset = Vector<int>(cursorIcon->Width() / -2, cursorIcon->Height()) + offset;
+       // offset the indicator by 1/8th to the right and top
+       indicatorOffset = cursorOffset + Vector<int>(cursorIcon->Width() / 8, cursorIcon->Height() / -8);
 
+       monsterPositions.clear();
+       monsterPositions.reserve(battle->NumMonsters());
+       for (int i(0), end(battle->NumMonsters()); i < end; ++i) {
+               monsterPositions.push_back(battle->MonsterAt(i).Position());
+       }
+
+       heroPositions.clear();
+       heroPositions.reserve(battle->NumHeroes());
+       for (int i(0), end(battle->NumHeroes()); i < end; ++i) {
+               heroPositions.push_back(parent->HeroTagPositionAt(i) + parent->HeroTagAt(i).HeroOffset());
+       }
 }
 
 
@@ -75,39 +98,30 @@ void SelectTarget::HandleEvents(const Input &input) {
        }
 }
 
-void SelectTarget::UpdateWorld(float deltaT) {
+void SelectTarget::UpdateWorld(Uint32 deltaT) {
 
 }
 
 void SelectTarget::Render(SDL_Surface *screen) {
-       Vector<int> offset(battle->CalculateScreenOffset(screen));
        parent->Render(screen);
-       RenderCursors(screen, offset);
+       RenderCursors(screen);
 }
 
-void SelectTarget::RenderCursors(SDL_Surface *screen, const geometry::Vector<int> &offset) {
-       Vector<int> cursorOffset(cursorIcon->Width() / -2, cursorIcon->Height());
-       // offset the indicator by 1/8th to the right and top
-       Vector<int> indicatorOffset(cursorOffset + Vector<int>(cursorIcon->Width() / 8, cursorIcon->Height() / -8));
-       vector<Vector<int> > positions;
-       if (selection->TargetsMonsters()) {
-               for (int i(0), end(battle->MaxMonsters()); i < end; ++i) {
-                       positions.push_back(battle->MonsterAt(i).Position());
-               }
-       } else {
-               for (int i(0), end(battle->NumHeroes()); i < end; ++i) {
-                       positions.push_back(battle->HeroTagPositionAt(i) + battle->HeroTagAt(i).HeroOffset());
-               }
-       }
+void SelectTarget::RenderCursors(SDL_Surface *screen) {
+       vector<Vector<int> > &positions = selection->TargetsMonsters()
+                       ? monsterPositions
+                       : heroPositions;
+
        if (flipFlop) {
-               for (vector<Vector<int> >::size_type i(0); i < positions.size(); ++i) {
+               for (vector<Vector<int> >::size_type i(0), end(positions.size());
+                               i != end; ++i) {
                        if (selection->IsSelected(i)) {
-                               cursorIcon->DrawTopRight(screen, positions[i] + indicatorOffset + offset);
+                               cursorIcon->DrawTopRight(screen, positions[i] + indicatorOffset);
                        }
                }
        }
        flipFlop = !flipFlop;
-       cursorIcon->DrawTopRight(screen, positions[selection->Current()] + offset + cursorOffset);
+       cursorIcon->DrawTopRight(screen, positions[selection->Current()] + cursorOffset);
 }
 
 }