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