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