]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/SelectSpell.cpp
moved hero position to Hero
[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->ActiveHero().GetAttackChoice().Selection().HasSelected()) {
40                 battle->ActiveHero().GetAttackChoice().SetType(AttackChoice::MAGIC);
41                 battle->ActiveHero().GetAttackChoice().SetSpell(battle->ActiveHero().SpellMenu().Selected());
42                 ctrl.PopState();
43         }
44 }
45
46 void SelectSpell::PauseState(Application &ctrl, SDL_Surface *screen) {
47
48 }
49
50
51 void SelectSpell::Resize(int width, int height) {
52
53 }
54
55
56 void SelectSpell::HandleEvents(const Input &input) {
57         if (input.JustPressed(Input::ACTION_A)) {
58                 if (battle->ActiveHero().SpellMenu().SelectedIsEnabled()) {
59                         AttackChoice &ac(battle->ActiveHero().GetAttackChoice());
60                         const Spell *spell(battle->ActiveHero().SpellMenu().Selected());
61                         ac.Selection().Reset();
62                         if (spell->GetTargetingMode().TargetsAlly()) {
63                                 ac.Selection().SelectHeroes();
64                         } else {
65                                 ac.Selection().SelectEnemies();
66                         }
67                         if (spell->GetTargetingMode().TargetsAll()) {
68                                 ac.SetType(AttackChoice::MAGIC);
69                                 // TODO: remove item from inventory
70                                 ac.SetSpell(spell);
71                                 battle->NextHero();
72                                 ctrl->PopState();
73                         } else {
74                                 if (spell->GetTargetingMode().TargetsSingle()) {
75                                         ac.Selection().SetSingle();
76                                 } else {
77                                         ac.Selection().SetMultiple();
78                                 }
79                                 ctrl->PushState(new SelectTarget(battle, parent, &ac.Selection(), battle->Res().magicTargetCursor));
80                         }
81                 }
82         }
83         if (input.JustPressed(Input::ACTION_B)) {
84                 ctrl->PopState(); // return control to parent
85         }
86         if (input.JustPressed(Input::PAD_UP)) {
87                 battle->ActiveHero().SpellMenu().PreviousRow();
88         }
89         if (input.JustPressed(Input::PAD_RIGHT)) {
90                 battle->ActiveHero().SpellMenu().NextItem();
91         }
92         if (input.JustPressed(Input::PAD_DOWN)) {
93                 battle->ActiveHero().SpellMenu().NextRow();
94         }
95         if (input.JustPressed(Input::PAD_LEFT)) {
96                 battle->ActiveHero().SpellMenu().PreviousItem();
97         }
98 }
99
100 void SelectSpell::UpdateWorld(float deltaT) {
101
102 }
103
104 void SelectSpell::Render(SDL_Surface *screen) {
105         parent->Render(screen);
106         Vector<int> offset(battle->CalculateScreenOffset(screen));
107         RenderFrame(screen, offset);
108         RenderHeadline(screen, offset);
109         RenderMenu(screen, offset);
110 }
111
112 void SelectSpell::RenderFrame(SDL_Surface *screen, const Vector<int> &offset) {
113         const Frame *frame(battle->Res().selectFrame);
114         Point<int> position(frame->BorderWidth(), frame->BorderHeight());
115         int width(battle->Width() - 2 * frame->BorderWidth());
116         int height(battle->Res().normalFont->CharHeight() * 13);
117         frame->Draw(screen, position + offset, width, height);
118 }
119
120 void SelectSpell::RenderHeadline(SDL_Surface *screen, const Vector<int> &offset) {
121         const Resources &res(battle->Res());
122         Point<int> position(
123                         2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
124                         2 * res.selectFrame->BorderHeight());
125         res.normalFont->DrawString(res.spellMenuHeadline, screen, position + offset);
126 }
127
128 void SelectSpell::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) {
129         const Resources &res(battle->Res());
130         Point<int> position(
131                         2 * res.selectFrame->BorderWidth() + res.normalFont->CharWidth(),
132                         2 * res.selectFrame->BorderHeight() + 2 * res.normalFont->CharHeight());
133         battle->ActiveHero().SpellMenu().Draw(screen, position + offset);
134 }
135
136 }