]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/SelectAttackType.cpp
removed lazy fwd headers
[l2e.git] / src / battle / states / SelectAttackType.cpp
1 /*
2  * SelectAttackType.cpp
3  *
4  *  Created on: Aug 7, 2012
5  *      Author: holy
6  */
7
8 #include "SelectAttackType.h"
9
10 #include "SelectIkari.h"
11 #include "SelectItem.h"
12 #include "SelectMoveAction.h"
13 #include "SelectSpell.h"
14 #include "SelectTarget.h"
15 #include "../AttackChoice.h"
16 #include "../BattleState.h"
17 #include "../../app/Application.h"
18 #include "../../app/Input.h"
19 #include "../../common/Item.h"
20 #include "../../math/Vector.h"
21
22 #include <stdexcept>
23
24 using app::Application;
25 using app::Input;
26 using common::Item;
27 using math::Vector;
28
29 namespace battle {
30
31 void SelectAttackType::OnEnterState(SDL_Surface *screen) {
32
33 }
34
35 void SelectAttackType::OnExitState(SDL_Surface *screen) {
36
37 }
38
39 void SelectAttackType::OnResumeState(SDL_Surface *screen) {
40         if (battle->ActiveHero().GetAttackChoice().Selection().HasSelected()) {
41                 battle->ActiveHero().GetAttackChoice().SetType(battle->GetAttackTypeMenu().Selected());
42                 battle->NextHero();
43         }
44         if (battle->AttackSelectionDone()) {
45                 // pass through
46                 Ctrl().PopState();
47         }
48 }
49
50 void SelectAttackType::OnPauseState(SDL_Surface *screen) {
51
52 }
53
54
55 void SelectAttackType::OnResize(int width, int height) {
56
57 }
58
59
60 void SelectAttackType::HandleEvents(const Input &input) {
61         if (input.IsDown(Input::PAD_UP)) {
62                 battle->GetAttackTypeMenu().Select(AttackChoice::MAGIC);
63         } else if (input.IsDown(Input::PAD_RIGHT)) {
64                 battle->GetAttackTypeMenu().Select(AttackChoice::DEFEND);
65         } else if (input.IsDown(Input::PAD_DOWN)) {
66                 battle->GetAttackTypeMenu().Select(AttackChoice::IKARI);
67         } else if (input.IsDown(Input::PAD_LEFT)) {
68                 battle->GetAttackTypeMenu().Select(AttackChoice::ITEM);
69         } else {
70                 battle->GetAttackTypeMenu().Select(AttackChoice::SWORD);
71         }
72
73         Hero &hero(battle->ActiveHero());
74         AttackChoice &ac(hero.GetAttackChoice());
75         if (input.JustPressed(Input::ACTION_A)) {
76                 switch (battle->GetAttackTypeMenu().Selected()) {
77                         case AttackChoice::SWORD:
78                                 if (hero.HasWeapon()) {
79                                         if (hero.Weapon()->GetTargetingMode().TargetsAll()) {
80                                                 ac.SetType(AttackChoice::SWORD);
81                                                 battle->NextHero();
82                                                 break;
83                                         } else {
84                                                 ac.Selection().ReadMode(hero.Weapon()->GetTargetingMode());
85                                         }
86                                 } else {
87                                         ac.Selection().SetSingle();
88                                 }
89                                 ac.Selection().Reset();
90                                 Ctrl().PushState(new SelectTarget(battle, this, &ac.Selection(), battle->Res().weaponTargetCursor));
91                                 break;
92                         case AttackChoice::MAGIC:
93                                 if (battle->ActiveHero().CanUseMagic()) {
94                                         Ctrl().PushState(new SelectSpell(battle, this));
95                                 }
96                                 break;
97                         case AttackChoice::DEFEND:
98                                 ac.SetType(AttackChoice::DEFEND);
99                                 battle->NextHero();
100                                 break;
101                         case AttackChoice::IKARI:
102                                 Ctrl().PushState(new SelectIkari(battle, this));
103                                 break;
104                         case AttackChoice::ITEM:
105                                 Ctrl().PushState(new SelectItem(battle, this));
106                                 break;
107                         default:
108                                 throw std::logic_error("selected invalid attack type");
109                 }
110         } else if (input.JustPressed(Input::ACTION_B)) {
111                 ac.Reset();
112                 battle->PreviousHero();
113                 if (battle->BeforeFirstHero()) {
114                         Ctrl().ChangeState(new SelectMoveAction(battle));
115                 } else {
116                         battle->ActiveHero().GetAttackChoice().Reset();
117                 }
118         }
119
120         if (battle->AttackSelectionDone()) {
121                 Ctrl().PopState();
122         }
123 }
124
125 void SelectAttackType::UpdateWorld(Uint32 deltaT) {
126
127 }
128
129 void SelectAttackType::Render(SDL_Surface *screen) {
130         Vector<int> offset(battle->CalculateScreenOffset(screen));
131         battle->RenderBackground(screen, offset);
132         battle->RenderMonsters(screen, offset);
133         battle->RenderHeroTags(screen, offset);
134         RenderMenu(screen, offset);
135 }
136
137 void SelectAttackType::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) {
138         Vector<int> position(
139                         (battle->Width() - battle->GetAttackTypeMenu().Width()) / 2,
140                         battle->Height() - battle->GetAttackTypeMenu().Height() - battle->GetAttackTypeMenu().Height() / 2);
141         battle->GetAttackTypeMenu().Render(screen, position + offset);
142 }
143
144 }