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