]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/SelectSpell.cpp
moved popping of battle state into battle state
[l2e.git] / src / battle / states / SelectSpell.cpp
1 /*
2  * SelectSpell.cpp
3  *
4  *  Created on: Aug 8, 2012
5  *      Author: holy
6  */
7
8 #include "SelectSpell.h"
9
10 #include "SelectAttackType.h"
11 #include "SelectMoveAction.h"
12 #include "SelectTarget.h"
13 #include "../BattleState.h"
14 #include "../../app/Application.h"
15 #include "../../app/Input.h"
16 #include "../../common/Spell.h"
17 #include "../../geometry/operators.h"
18 #include "../../geometry/Point.h"
19 #include "../../graphics/Frame.h"
20
21 using app::Application;
22 using app::Input;
23 using common::Spell;
24 using geometry::Point;
25 using geometry::Vector;
26 using graphics::Frame;
27
28 namespace battle {
29
30 void SelectSpell::EnterState(Application &c, SDL_Surface *screen) {
31         ctrl = &c;
32 }
33
34 void SelectSpell::ExitState(Application &c, SDL_Surface *screen) {
35         ctrl = 0;
36 }
37
38 void SelectSpell::ResumeState(Application &ctrl, SDL_Surface *screen) {
39         if (battle->ActiveHeroTargets().HasSelected()) {
40                 ctrl.PopState();
41         }
42 }
43
44 void SelectSpell::PauseState(Application &ctrl, SDL_Surface *screen) {
45
46 }
47
48
49 void SelectSpell::Resize(int width, int height) {
50
51 }
52
53
54 void SelectSpell::HandleEvents(const Input &input) {
55         if (input.JustPressed(Input::ACTION_A)) {
56                 if (battle->GetSpellMenu().SelectedIsEnabled()) {
57                         const Spell *spell(battle->GetSpellMenu().Selected());
58                         battle->ActiveHeroTargets().Reset();
59                         if (spell->GetTargetingMode().TargetsAlly()) {
60                                 battle->ActiveHeroTargets().SelectHeroes();
61                         } else {
62                                 battle->ActiveHeroTargets().SelectEnemies();
63                         }
64                         if (spell->GetTargetingMode().TargetsAll()) {
65                                 battle->SetAttackType(AttackChoice::MAGIC);
66                                 // TODO: remove item from inventory
67                                 battle->ActiveHeroAttackChoice().SetSpell(spell);
68                                 battle->NextHero();
69                                 ctrl->PopState();
70                         } else {
71                                 if (spell->GetTargetingMode().TargetsSingle()) {
72                                         battle->ActiveHeroTargets().SetSingle();
73                                 } else {
74                                         battle->ActiveHeroTargets().SetMultiple();
75                                 }
76                                 ctrl->PushState(new SelectTarget(battle, parent, &battle->ActiveHeroTargets(), battle->Res().magicTargetCursor));
77                         }
78                 }
79         }
80         if (input.JustPressed(Input::ACTION_B)) {
81                 ctrl->PopState(); // return control to parent
82         }
83         if (input.JustPressed(Input::PAD_UP)) {
84                 battle->GetSpellMenu().PreviousRow();
85         }
86         if (input.JustPressed(Input::PAD_RIGHT)) {
87                 battle->GetSpellMenu().NextItem();
88         }
89         if (input.JustPressed(Input::PAD_DOWN)) {
90                 battle->GetSpellMenu().NextRow();
91         }
92         if (input.JustPressed(Input::PAD_LEFT)) {
93                 battle->GetSpellMenu().PreviousItem();
94         }
95 }
96
97 void SelectSpell::UpdateWorld(float deltaT) {
98
99 }
100
101 void SelectSpell::Render(SDL_Surface *screen) {
102         parent->Render(screen);
103         Vector<int> offset(battle->CalculateScreenOffset(screen));
104         RenderFrame(screen, offset);
105         RenderHeadline(screen, offset);
106         RenderMenu(screen, offset);
107 }
108
109 void SelectSpell::RenderFrame(SDL_Surface *screen, const Vector<int> &offset) {
110         const Frame *frame(battle->Res().selectFrame);
111         Point<int> position(frame->BorderWidth(), frame->BorderHeight());
112         int width(battle->BackgroundWidth() - 2 * frame->BorderWidth());
113         int height(battle->Res().normalFont->CharHeight() * 13);
114         frame->Draw(screen, position + offset, width, height);
115 }
116
117 void SelectSpell::RenderHeadline(SDL_Surface *screen, const Vector<int> &offset) {
118         const Resources &res(battle->Res());
119         Point<int> position(
120                         2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
121                         2 * res.selectFrame->BorderHeight());
122         res.normalFont->DrawString(res.spellMenuHeadline, screen, position + offset);
123 }
124
125 void SelectSpell::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) {
126         const Resources &res(battle->Res());
127         Point<int> position(
128                         2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
129                         2 * res.selectFrame->BorderHeight() + 2 * res.normalFont->CharHeight());
130         battle->GetSpellMenu().Draw(screen, position + offset);
131 }
132
133 }