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