]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/SelectItem.cpp
extracted battle logic into a class
[l2e.git] / src / battle / states / SelectItem.cpp
1 #include "SelectItem.h"
2
3 #include "SelectAttackType.h"
4 #include "SelectTarget.h"
5 #include "../BattleState.h"
6 #include "../../app/Application.h"
7 #include "../../app/Input.h"
8 #include "../../common/Item.h"
9 #include "../../graphics/Frame.h"
10 #include "../../math/Vector.h"
11
12 using app::Application;
13 using app::Input;
14 using common::Item;
15 using math::Vector;
16 using graphics::Frame;
17
18 namespace battle {
19
20 SelectItem::SelectItem(Battle *battle, SelectAttackType *parent)
21 : battle(battle)
22 , parent(parent) {
23
24 }
25
26
27 void SelectItem::OnEnterState(SDL_Surface *screen) {
28         OnResize(screen->w, screen->h);
29 }
30
31 void SelectItem::OnExitState(SDL_Surface *screen) {
32
33 }
34
35 void SelectItem::OnResumeState(SDL_Surface *screen) {
36         if (battle->ActiveHero().GetAttackChoice().Selection().HasSelected()) {
37                 battle->ActiveHero().GetAttackChoice().SetType(AttackChoice::ITEM);
38                 battle->ActiveHero().GetAttackChoice().SetItem(parent->ItemMenu().Selected());
39                 Ctrl().PopState();
40         }
41 }
42
43 void SelectItem::OnPauseState(SDL_Surface *screen) {
44
45 }
46
47
48 void SelectItem::OnResize(int width, int height) {
49         const Vector<int> offset = parent->ScreenOffset();
50
51         const Resources &res = parent->Res();
52         const Frame &frame = *res.selectFrame;
53
54         framePosition = offset + frame.BorderSize();
55         frameSize = Vector<int>(
56                         parent->Width() - 2 * frame.BorderWidth(),
57                         res.normalFont->CharHeight() * 13);
58
59         headlinePosition = offset + Vector<int>(
60                         2 * frame.BorderWidth() + res.normalFont->CharWidth(),
61                         2 * frame.BorderHeight());
62
63         menuPosition = offset + Vector<int>(
64                         2 * frame.BorderWidth() + res.normalFont->CharWidth(),
65                         2 * frame.BorderHeight() + 2 * res.normalFont->CharHeight());
66 }
67
68
69 void SelectItem::HandleEvents(const Input &input) {
70         if (input.JustPressed(Input::ACTION_A)) {
71                 if (parent->ItemMenu().SelectedIsEnabled()) {
72                         AttackChoice &ac(battle->ActiveHero().GetAttackChoice());
73                         const Item *item(parent->ItemMenu().Selected());
74                         ac.Selection().Reset();
75                         if (item->GetTargetingMode().TargetsAlly()) {
76                                 ac.Selection().SelectHeroes();
77                         } else {
78                                 ac.Selection().SelectMonsters();
79                         }
80                         if (item->GetTargetingMode().TargetsAll()) {
81                                 ac.SetType(AttackChoice::ITEM);
82                                 ac.SetItem(item);
83                                 battle->NextHero();
84                                 Ctrl().PopState();
85                         } else {
86                                 if (item->GetTargetingMode().TargetsSingle()) {
87                                         ac.Selection().SetSingle();
88                                 } else {
89                                         ac.Selection().SetMultiple();
90                                 }
91                                 Ctrl().PushState(new SelectTarget(battle, parent, &ac.Selection(), parent->Res().itemTargetCursor));
92                         }
93                 }
94         }
95         if (input.JustPressed(Input::ACTION_B)) {
96                 Ctrl().PopState(); // return control to parent
97         }
98         if (input.JustPressed(Input::PAD_UP)) {
99                 parent->ItemMenu().PreviousRow();
100         }
101         if (input.JustPressed(Input::PAD_RIGHT)) {
102                 parent->ItemMenu().NextItem();
103         }
104         if (input.JustPressed(Input::PAD_DOWN)) {
105                 parent->ItemMenu().NextRow();
106         }
107         if (input.JustPressed(Input::PAD_LEFT)) {
108                 parent->ItemMenu().PreviousItem();
109         }
110 }
111
112 void SelectItem::UpdateWorld(Uint32 deltaT) {
113
114 }
115
116 void SelectItem::Render(SDL_Surface *screen) {
117         parent->Render(screen);
118         RenderFrame(screen);
119         RenderHeadline(screen);
120         RenderMenu(screen);
121 }
122
123 void SelectItem::RenderFrame(SDL_Surface *screen) {
124         const Frame &frame = *parent->Res().selectFrame;
125         frame.Draw(screen, framePosition, frameSize.X(), frameSize.Y());
126 }
127
128 void SelectItem::RenderHeadline(SDL_Surface *screen) {
129         const Resources &res = parent->Res();
130         res.normalFont->DrawString(res.itemMenuHeadline, screen, headlinePosition);
131 }
132
133 void SelectItem::RenderMenu(SDL_Surface *screen) {
134         parent->ItemMenu().Draw(screen, menuPosition);
135 }
136
137 }