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