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