]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/SwapHeroes.cpp
removed lazy fwd headers
[l2e.git] / src / battle / states / SwapHeroes.cpp
1 /*
2  * SwapHeroes.cpp
3  *
4  *  Created on: Aug 10, 2012
5  *      Author: holy
6  */
7
8 #include "SwapHeroes.h"
9
10 #include "SelectMoveAction.h"
11 #include "../BattleState.h"
12 #include "../../app/Application.h"
13 #include "../../app/Input.h"
14 #include "../../math/Vector.h"
15
16 using app::Application;
17 using app::Input;
18 using math::Vector;
19 using std::vector;
20
21 namespace battle {
22
23 void SwapHeroes::OnEnterState(SDL_Surface *screen) {
24
25 }
26
27 void SwapHeroes::OnExitState(SDL_Surface *screen) {
28
29 }
30
31 void SwapHeroes::OnResumeState(SDL_Surface *screen) {
32
33 }
34
35 void SwapHeroes::OnPauseState(SDL_Surface *screen) {
36
37 }
38
39
40 void SwapHeroes::OnResize(int width, int height) {
41
42 }
43
44
45 void SwapHeroes::HandleEvents(const Input &input) {
46         if (input.JustPressed(Input::ACTION_A)) {
47                 if (selected != -1 && cursor != selected) {
48                         battle->SwapHeroes(cursor, selected);
49                         selected = -1;
50                 } else {
51                         selected = cursor;
52                 }
53         }
54         if (input.JustPressed(Input::ACTION_B)) {
55                 if (cursor == selected) {
56                         selected = -1;
57                 } else {
58                         Ctrl().PopState();
59                 }
60         }
61
62         if (input.JustPressed(Input::PAD_UP)) {
63                 MoveUp();
64         }
65         if (input.JustPressed(Input::PAD_RIGHT)) {
66                 MoveRight();
67         }
68         if (input.JustPressed(Input::PAD_DOWN)) {
69                 MoveDown();
70         }
71         if (input.JustPressed(Input::PAD_LEFT)) {
72                 MoveLeft();
73         }
74 }
75
76
77 void SwapHeroes::MoveUp() {
78         if (cursor < 2) {
79                 if (battle->HeroPositionOccupied(cursor + 2)) {
80                         cursor += 2;
81                 }
82         } else {
83                 cursor -= 2;
84         }
85 }
86
87 void SwapHeroes::MoveRight() {
88         if (cursor < 3 && battle->HeroPositionOccupied(cursor + 1)) {
89                 ++cursor;
90         } else {
91                 cursor = 0;
92         }
93 }
94
95 void SwapHeroes::MoveDown() {
96         if (cursor < 2) {
97                 if (battle->HeroPositionOccupied(cursor + 2)) {
98                         cursor += 2;
99                 }
100         } else {
101                 cursor -= 2;
102         }
103 }
104
105 void SwapHeroes::MoveLeft() {
106         if (cursor > 0) {
107                 --cursor;
108         } else {
109                 cursor = battle->NumHeroes();
110         }
111 }
112
113
114 void SwapHeroes::UpdateWorld(Uint32 deltaT) {
115
116 }
117
118 void SwapHeroes::Render(SDL_Surface *screen) {
119         Vector<int> offset(battle->CalculateScreenOffset(screen));
120         parent->Render(screen);
121         RenderCursors(screen, offset);
122 }
123
124 void SwapHeroes::RenderCursors(SDL_Surface *screen, const math::Vector<int> &offset) {
125         // offset the cursor by 1/8th to the left and bottom
126         Vector<int> cursorOffset(battle->Res().swapCursor->Width() / -8, battle->Res().swapCursor->Height() / 8);
127         Vector<int> indicatorOffset(0, 0);
128         vector<Vector<int> > positions;
129         for (int i(0), end(battle->NumHeroes()); i < end; ++i) {
130                 Vector<int> positionCorrection(battle->Res().swapCursor->Width() / 2, battle->HeroTagAt(i).HeroSprite()->Height() - battle->Res().swapCursor->Height() / 2);
131                 // indicator offsets are inverted for heroes
132                 positionCorrection -= cursorOffset;
133                 positions.push_back(battle->HeroTagPositionAt(i) + battle->HeroTagAt(i).HeroOffset() + positionCorrection);
134         }
135         if (flipFlop) {
136                 for (vector<Vector<int> >::size_type i(0); i < positions.size(); ++i) {
137                         if (selected == int(i)) {
138                                 battle->Res().swapCursor->DrawTopRight(screen, positions[i] + offset);
139                         }
140                 }
141         }
142         flipFlop = !flipFlop;
143         battle->Res().swapCursor->DrawTopRight(screen, positions[cursor] + offset + cursorOffset);
144 }
145
146 }