]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/SelectTarget.cpp
cached some of the battle coordinates
[l2e.git] / src / battle / states / SelectTarget.cpp
1 #include "SelectTarget.h"
2
3 #include "SelectAttackType.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 SelectTarget::OnEnterState(SDL_Surface *screen) {
17         OnResize(screen->w, screen->h);
18 }
19
20 void SelectTarget::OnExitState(SDL_Surface *screen) {
21
22 }
23
24 void SelectTarget::OnResumeState(SDL_Surface *screen) {
25
26 }
27
28 void SelectTarget::OnPauseState(SDL_Surface *screen) {
29
30 }
31
32
33 void SelectTarget::OnResize(int width, int height) {
34         Vector<int> offset(battle->ScreenOffset());
35         cursorOffset = Vector<int>(cursorIcon->Width() / -2, cursorIcon->Height()) + offset;
36         // offset the indicator by 1/8th to the right and top
37         indicatorOffset = cursorOffset + Vector<int>(cursorIcon->Width() / 8, cursorIcon->Height() / -8);
38
39         monsterPositions.clear();
40         monsterPositions.reserve(battle->MaxMonsters());
41         for (int i(0), end(battle->MaxMonsters()); i < end; ++i) {
42                 monsterPositions.push_back(battle->MonsterAt(i).Position());
43         }
44
45         heroPositions.clear();
46         heroPositions.reserve(battle->NumHeroes());
47         for (int i(0), end(battle->NumHeroes()); i < end; ++i) {
48                 heroPositions.push_back(battle->HeroTagPositionAt(i) + battle->HeroTagAt(i).HeroOffset());
49         }
50 }
51
52
53 void SelectTarget::HandleEvents(const Input &input) {
54         if (input.JustPressed(Input::ACTION_A)) {
55                 if (selection->CurrentIsSelected()) {
56                         Ctrl().PopState(); // return control to parent
57                 } else {
58                         selection->Select();
59                         if (selection->SelectSingle()) {
60                                 Ctrl().PopState(); // return control to parent
61                         }
62                 }
63         }
64         if (input.JustPressed(Input::ACTION_B)) {
65                 if (selection->CurrentIsSelected()) {
66                         selection->Unselect();
67                 } else {
68                         selection->UnselectAll();
69                         Ctrl().PopState(); // return control to parent
70                 }
71         }
72
73         if (input.JustPressed(Input::PAD_UP)) {
74                 selection->MoveUp();
75         }
76         if (input.JustPressed(Input::PAD_RIGHT)) {
77                 selection->MoveRight();
78         }
79         if (input.JustPressed(Input::PAD_DOWN)) {
80                 selection->MoveDown();
81         }
82         if (input.JustPressed(Input::PAD_LEFT)) {
83                 selection->MoveLeft();
84         }
85 }
86
87 void SelectTarget::UpdateWorld(Uint32 deltaT) {
88
89 }
90
91 void SelectTarget::Render(SDL_Surface *screen) {
92         parent->Render(screen);
93         RenderCursors(screen);
94 }
95
96 void SelectTarget::RenderCursors(SDL_Surface *screen) {
97         vector<Vector<int> > &positions = selection->TargetsMonsters()
98                         ? monsterPositions
99                         : heroPositions;
100
101         if (flipFlop) {
102                 for (vector<Vector<int> >::size_type i(0), end(positions.size());
103                                 i != end; ++i) {
104                         if (selection->IsSelected(i)) {
105                                 cursorIcon->DrawTopRight(screen, positions[i] + indicatorOffset);
106                         }
107                 }
108         }
109         flipFlop = !flipFlop;
110         cursorIcon->DrawTopRight(screen, positions[selection->Current()] + cursorOffset);
111 }
112
113 }