]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/SelectAttackType.cpp
removed redundant BattleState::ActiveHeroAttackChoice()
[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 "../../geometry/operators.h"
20
21 #include <stdexcept>
22
23 using app::Application;
24 using app::Input;
25 using geometry::Point;
26 using geometry::Vector;
27
28 namespace battle {
29
30 void SelectAttackType::EnterState(Application &c, SDL_Surface *screen) {
31         ctrl = &c;
32 }
33
34 void SelectAttackType::ExitState(Application &c, SDL_Surface *screen) {
35         ctrl = 0;
36 }
37
38 void SelectAttackType::ResumeState(Application &ctrl, SDL_Surface *screen) {
39         if (battle->ActiveHero().GetAttackChoice().Selection().HasSelected()) {
40                 battle->ActiveHero().GetAttackChoice().SetType(battle->GetAttackTypeMenu().Selected());
41                 battle->NextHero();
42         }
43         if (battle->AttackSelectionDone()) {
44                 // pass through
45                 ctrl.PopState();
46         }
47 }
48
49 void SelectAttackType::PauseState(Application &ctrl, SDL_Surface *screen) {
50
51 }
52
53
54 void SelectAttackType::Resize(int width, int height) {
55
56 }
57
58
59 void SelectAttackType::HandleEvents(const Input &input) {
60         if (input.IsDown(Input::PAD_UP)) {
61                 battle->GetAttackTypeMenu().Select(AttackChoice::MAGIC);
62         } else if (input.IsDown(Input::PAD_RIGHT)) {
63                 battle->GetAttackTypeMenu().Select(AttackChoice::DEFEND);
64         } else if (input.IsDown(Input::PAD_DOWN)) {
65                 battle->GetAttackTypeMenu().Select(AttackChoice::IKARI);
66         } else if (input.IsDown(Input::PAD_LEFT)) {
67                 battle->GetAttackTypeMenu().Select(AttackChoice::ITEM);
68         } else {
69                 battle->GetAttackTypeMenu().Select(AttackChoice::SWORD);
70         }
71
72         if (input.JustPressed(Input::ACTION_A)) {
73                 switch (battle->GetAttackTypeMenu().Selected()) {
74                         case AttackChoice::SWORD:
75                                 // TODO: detect single/multiple/all attack mode
76                                 battle->ActiveHero().GetAttackChoice().Selection().SetSingle();
77                                 battle->ActiveHero().GetAttackChoice().Selection().Reset();
78                                 ctrl->PushState(new SelectTarget(battle, this, &battle->ActiveHero().GetAttackChoice().Selection(), battle->Res().weaponTargetCursor));
79                                 break;
80                         case AttackChoice::MAGIC:
81                                 if (battle->ActiveHero().CanUseMagic()) {
82                                         ctrl->PushState(new SelectSpell(battle, this));
83                                 }
84                                 break;
85                         case AttackChoice::DEFEND:
86                                 battle->ActiveHero().GetAttackChoice().SetType(AttackChoice::DEFEND);
87                                 battle->NextHero();
88                                 break;
89                         case AttackChoice::IKARI:
90                                 ctrl->PushState(new SelectIkari(battle, this));
91                                 break;
92                         case AttackChoice::ITEM:
93                                 ctrl->PushState(new SelectItem(battle, this));
94                                 break;
95                         default:
96                                 throw std::logic_error("selected invalid attack type");
97                 }
98         } else if (input.JustPressed(Input::ACTION_B)) {
99                 battle->ActiveHero().GetAttackChoice().Reset();
100                 battle->PreviousHero();
101                 if (battle->BeforeFirstHero()) {
102                         ctrl->ChangeState(new SelectMoveAction(battle));
103                 } else {
104                         battle->ActiveHero().GetAttackChoice().Reset();
105                 }
106         }
107
108         if (battle->AttackSelectionDone()) {
109                 ctrl->PopState();
110         }
111 }
112
113 void SelectAttackType::UpdateWorld(float deltaT) {
114
115 }
116
117 void SelectAttackType::Render(SDL_Surface *screen) {
118         Vector<int> offset(battle->CalculateScreenOffset(screen));
119         battle->RenderBackground(screen, offset);
120         battle->RenderMonsters(screen, offset);
121         battle->RenderHeroTags(screen, offset);
122         RenderMenu(screen, offset);
123 }
124
125 void SelectAttackType::RenderMenu(SDL_Surface *screen, const Vector<int> &offset) {
126         Point<int> position(
127                         (battle->Width() - battle->GetAttackTypeMenu().Width()) / 2,
128                         battle->Height() - battle->GetAttackTypeMenu().Height() - battle->GetAttackTypeMenu().Height() / 2);
129         battle->GetAttackTypeMenu().Render(screen, position + offset);
130 }
131
132 }