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