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