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