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