]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/PerformAttacks.cpp
renamed BattleState's background functions
[l2e.git] / src / battle / states / PerformAttacks.cpp
1 /*
2  * PerformAttacks.cpp
3  *
4  *  Created on: Aug 10, 2012
5  *      Author: holy
6  */
7
8 #include "PerformAttacks.h"
9
10 #include "../BattleState.h"
11 #include "../Hero.h"
12 #include "../Monster.h"
13 #include "../../app/Application.h"
14 #include "../../app/Input.h"
15 #include "../../common/Ikari.h"
16 #include "../../common/Item.h"
17 #include "../../common/Spell.h"
18 #include "../../geometry/operators.h"
19 #include "../../geometry/Point.h"
20 #include "../../graphics/Animation.h"
21 #include "../../graphics/Font.h"
22 #include "../../graphics/Frame.h"
23
24 #include <cstring>
25
26 using app::Application;
27 using app::Input;
28 using geometry::Point;
29 using geometry::Vector;
30 using std::vector;
31
32 namespace battle {
33
34 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
35         ctrl = &c;
36         battle->WriteOrder(order);
37         numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
38         numberPosition.reserve(numberAnimation.size());
39 }
40
41 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
42         ctrl = 0;
43 }
44
45 void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) {
46
47 }
48
49 void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) {
50
51 }
52
53
54 void PerformAttacks::Resize(int width, int height) {
55
56 }
57
58
59 void PerformAttacks::HandleEvents(const Input &input) {
60         CheckNumberAnimation();
61         if (HasAnimationsRunning()) return;
62         ResetAnimation();
63         AdvanceCursor();
64         if (Finished()) {
65                 battle->ClearAllAttacks();
66                 ctrl->PopState();
67                 return;
68         }
69
70         if (order[cursor].isMonster) {
71                 const Monster &monster(battle->MonsterAt(order[cursor].index));
72                 titleBarText = monster.Name();
73                 moveAnimation = 0;
74         } else {
75                 Hero &hero(battle->HeroAt(order[cursor].index));
76                 const AttackChoice &ac(battle->AttackChoiceAt(order[cursor].index));
77
78                 switch (ac.GetType()) {
79                         case AttackChoice::SWORD:
80                                 titleBarText = hero.HasWeapon() ? hero.Weapon()->Name() : "Melee attack!";
81                                 moveAnimation = hero.AttackAnimation();
82                                 numberAnimation.push_back(NumberAnimation(15, battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
83                                 if (ac.Selection().TargetsEnemies()) {
84                                         numberPosition.push_back(
85                                                         battle->MonsterPositions()[ac.Selection().SingleSelection()]);
86                                 } else {
87                                         numberPosition.push_back(
88                                                         battle->HeroesPositions()[ac.Selection().SingleSelection()]);
89                                 }
90                                 break;
91                         case AttackChoice::MAGIC:
92                                 titleBarText = ac.GetSpell()->Name();
93                                 moveAnimation = hero.SpellAnimation();
94                                 break;
95                         case AttackChoice::DEFEND:
96                                 titleBarText = "Defends.";
97                                 moveAnimation = 0;
98                                 break;
99                         case AttackChoice::IKARI:
100                                 if (ac.GetItem()->HasIkari()) {
101                                         titleBarText = ac.GetItem()->GetIkari()->Name();
102                                         if (ac.GetItem()->GetIkari()->IsMagical()) {
103                                                 moveAnimation = hero.SpellAnimation();
104                                         } else {
105                                                 moveAnimation = hero.AttackAnimation();
106                                         }
107                                 }
108                                 break;
109                         case AttackChoice::ITEM:
110                                 titleBarText = ac.GetItem()->Name();
111                                 moveAnimation = 0;
112                                 break;
113                         case AttackChoice::UNDECIDED:
114                                 titleBarText = "UNDECIDED";
115                                 moveAnimation = 0;
116                                 break;
117                 }
118         }
119
120         if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(1500);
121         if (moveAnimation) moveAnimation->Start(*this);
122 }
123
124 void PerformAttacks::CheckNumberAnimation() {
125         if (moveAnimation && moveAnimation->Running()) return;
126         if (!moveAnimation || moveAnimation->JustFinished()) {
127                 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
128                         i->Start(*this);
129                 }
130         } else {
131                 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
132                         i->CheckTimers(*this);
133                 }
134         }
135 }
136
137 bool PerformAttacks::HasAnimationsRunning() const {
138         if (titleBarTimer.Running()) return true;
139         if (moveAnimation && moveAnimation->Running()) return true;
140         for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
141                 if (i->Running()) return true;
142         }
143         return false;
144 }
145
146 void PerformAttacks::ResetAnimation() {
147         if (moveAnimation) moveAnimation->Stop();
148         titleBarTimer.Clear();
149         numberAnimation.clear();
150         numberPosition.clear();
151 }
152
153 void PerformAttacks::AdvanceCursor() {
154         ++cursor;
155         while (cursor < int(order.size())) {
156                 if (order[cursor].isMonster) {
157                         if (battle->MonsterAt(order[cursor].index).Health() > 0) break;
158                 } else {
159                         if (battle->HeroAt(order[cursor].index).Health() > 0) break;
160                 }
161                 ++cursor;
162         }
163 }
164
165
166 void PerformAttacks::UpdateWorld(float deltaT) {
167
168 }
169
170 void PerformAttacks::Render(SDL_Surface *screen) {
171         Vector<int> offset(battle->CalculateScreenOffset(screen));
172         battle->RenderBackground(screen, offset);
173         battle->RenderMonsters(screen, offset);
174         battle->RenderHeroes(screen, offset);
175         battle->RenderSmallHeroTags(screen, offset);
176         RenderTitleBar(screen, offset);
177         RenderNumbers(screen, offset);
178 }
179
180 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) {
181         if (!titleBarText || !titleBarTimer.Running()) return;
182
183         int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
184         battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->Width(), height);
185
186         Point<int> textPosition(
187                         (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
188                         battle->Res().titleFrame->BorderHeight());
189         battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
190 }
191
192 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) {
193         for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
194                 if (numberAnimation[i].Running()) {
195                         Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
196                         numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
197                 }
198         }
199 }
200
201 }