]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/PerformAttacks.cpp
renamed namespace geometry -> math
[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 "../TargetSelection.h"
14 #include "../../app/Application.h"
15 #include "../../app/Input.h"
16 #include "../../common/Ikari.h"
17 #include "../../common/Item.h"
18 #include "../../common/Spell.h"
19 #include "../../graphics/Animation.h"
20 #include "../../graphics/Font.h"
21 #include "../../graphics/Frame.h"
22
23 #include <cstring>
24
25 using app::Application;
26 using app::Input;
27 using math::Vector;
28 using graphics::AnimationRunner;
29 using std::vector;
30
31 namespace battle {
32
33 void PerformAttacks::OnEnterState(SDL_Surface *screen) {
34         battle->CalculateAttackOrder();
35         numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
36         numberPosition.reserve(numberAnimation.size());
37 }
38
39 void PerformAttacks::OnExitState(SDL_Surface *screen) {
40         battle->ClearAllAttacks();
41 }
42
43 void PerformAttacks::OnResumeState(SDL_Surface *screen) {
44
45 }
46
47 void PerformAttacks::OnPauseState(SDL_Surface *screen) {
48
49 }
50
51
52 void PerformAttacks::OnResize(int width, int height) {
53
54 }
55
56
57 void PerformAttacks::HandleEvents(const Input &input) {
58         CheckAnimations();
59         if (HasAnimationsRunning()) return;
60         ResetAnimation();
61         battle->ApplyDamage();
62         battle->NextAttack();
63         if (battle->AttacksFinished()) {
64                 Ctrl().PopState();
65                 return;
66         }
67
68         battle->CalculateDamage();
69
70         if (battle->CurrentAttack().IsMonster()) {
71                 Monster &monster(battle->MonsterAt(battle->CurrentAttack().index));
72                 titleBarText = monster.Name();
73                 targetAnimation = AnimationRunner(monster.MeleeAnimation());
74                 moveAnimation = AnimationRunner(monster.AttackAnimation());
75                 monster.SetAnimation(moveAnimation);
76                 AddNumberAnimations(battle->MonsterAt(battle->CurrentAttack().index).GetAttackChoice().Selection());
77         } else if (battle->CurrentAttack().IsCapsule()) {
78                 Capsule &capsule(battle->GetCapsule());
79                 titleBarText = capsule.Name();
80                 targetAnimation = AnimationRunner(capsule.MeleeAnimation());
81                 moveAnimation = AnimationRunner(capsule.AttackAnimation());
82                 capsule.SetAnimation(moveAnimation);
83                 AddNumberAnimations(capsule.GetAttackChoice().Selection());
84         } else {
85                 Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
86                 const AttackChoice &ac(battle->HeroAt(battle->CurrentAttack().index).GetAttackChoice());
87
88                 switch (ac.GetType()) {
89                         case AttackChoice::SWORD:
90                                 if (hero.HasWeapon()) {
91                                         titleBarText = hero.Weapon()->Name();
92                                         targetAnimation = AnimationRunner(hero.Weapon()->AttackAnimation());
93                                 } else {
94                                         titleBarText = "Melee attack!";
95                                         targetAnimation = AnimationRunner(hero.MeleeAnimation());
96                                 }
97                                 moveAnimation = AnimationRunner(hero.AttackAnimation());
98                                 AddNumberAnimations(ac.Selection());
99                                 break;
100                         case AttackChoice::MAGIC:
101                                 titleBarText = ac.GetSpell()->Name();
102                                 moveAnimation = AnimationRunner(hero.SpellAnimation());
103                                 break;
104                         case AttackChoice::DEFEND:
105                                 titleBarText = "Defends.";
106                                 moveAnimation.Clear();
107                                 break;
108                         case AttackChoice::IKARI:
109                                 if (ac.GetItem()->HasIkari()) {
110                                         titleBarText = ac.GetItem()->GetIkari()->Name();
111                                         if (ac.GetItem()->GetIkari()->IsMagical()) {
112                                                 moveAnimation = AnimationRunner(hero.SpellAnimation());
113                                         } else {
114                                                 moveAnimation = AnimationRunner(hero.AttackAnimation());
115                                         }
116                                 }
117                                 break;
118                         case AttackChoice::ITEM:
119                                 titleBarText = ac.GetItem()->Name();
120                                 moveAnimation.Clear();
121                                 break;
122                         case AttackChoice::UNDECIDED:
123                                 titleBarText = "UNDECIDED";
124                                 moveAnimation.Clear();
125                                 break;
126                 }
127         }
128
129         if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850);
130         if (moveAnimation.Valid()) {
131                 moveAnimation.Start(*this);
132                 if (battle->CurrentAttack().IsMonster()) {
133                         battle->MonsterAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
134                 } else if (battle->CurrentAttack().IsHero()) {
135                         battle->HeroAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
136                 } else {
137                         battle->GetCapsule().SetAnimation(moveAnimation);
138                 }
139         }
140         if (targetAnimation.Valid()) {
141                 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
142         } else {
143                 targetAnimationTimer.Clear();
144         }
145 }
146
147 void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) {
148         if (ts.TargetsMonsters()) {
149                 for (int i(0); i < battle->MaxMonsters(); ++i) {
150                         if (ts.IsBad(i)) {
151                                 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
152                                 numberPosition.push_back(
153                                                 battle->MonsterAt(i).Position());
154                         } else if (ts.IsGood(i)) {
155                                 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
156                                 numberPosition.push_back(
157                                                 battle->MonsterAt(i).Position());
158                         }
159                 }
160         } else {
161                 for (int i(0); i < battle->NumHeroes(); ++i) {
162                         if (ts.IsBad(i)) {
163                                 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
164                                 numberPosition.push_back(
165                                                 battle->HeroAt(i).Position());
166                         } else if (ts.IsGood(i)) {
167                                 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
168                                 numberPosition.push_back(
169                                                 battle->HeroAt(i).Position());
170                         }
171                 }
172         }
173 }
174
175 void PerformAttacks::CheckAnimations() {
176         if (targetAnimation.Valid() && targetAnimationTimer.JustHit()) {
177                 targetAnimation.Start(*this);
178         }
179         if (moveAnimation.Valid() && !moveAnimation.Finished()) return;
180         if (targetAnimation.Valid() && !targetAnimation.Finished()) return;
181         if (moveAnimation.Valid() || targetAnimation.Valid()) {
182                 moveAnimation.Clear();
183                 targetAnimation.Clear();
184                 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
185                         i->Start(*this);
186                 }
187         } else {
188                 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
189                         i->CheckTimers(*this);
190                 }
191         }
192 }
193
194 bool PerformAttacks::HasAnimationsRunning() const {
195         if (titleBarTimer.Running()) return true;
196         if (moveAnimation.Valid() && moveAnimation.Running()) return true;
197         if (targetAnimation.Valid() && targetAnimation.Running()) return true;
198         for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
199                 if (i->Running()) return true;
200         }
201         return false;
202 }
203
204 void PerformAttacks::ResetAnimation() {
205         if (moveAnimation.Valid()) {
206                 moveAnimation.Clear();
207                 if (!battle->CurrentAttack().IsMonster()) {
208                         battle->HeroAt(battle->CurrentAttack().index).GetAnimation().Clear();
209                 }
210         }
211         if (targetAnimation.Valid()) {
212                 targetAnimation.Clear();
213         }
214         titleBarTimer.Clear();
215         numberAnimation.clear();
216         numberPosition.clear();
217 }
218
219
220 void PerformAttacks::UpdateWorld(float deltaT) {
221
222 }
223
224 void PerformAttacks::Render(SDL_Surface *screen) {
225         Vector<int> offset(battle->CalculateScreenOffset(screen));
226         battle->RenderBackground(screen, offset);
227         battle->RenderMonsters(screen, offset);
228         battle->RenderHeroes(screen, offset);
229         battle->RenderCapsule(screen, offset);
230         battle->RenderSmallHeroTags(screen, offset);
231         RenderTitleBar(screen, offset);
232         RenderNumbers(screen, offset);
233         RenderTargetAnimation(screen, offset);
234 }
235
236 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
237         if (!titleBarText || !titleBarTimer.Running()) return;
238
239         int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
240         battle->Res().titleFrame->Draw(screen, offset, battle->Width(), height);
241
242         Vector<int> textPosition(
243                         (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
244                         battle->Res().titleFrame->BorderHeight());
245         battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
246 }
247
248 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
249         for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
250                 if (numberAnimation[i].Running()) {
251                         Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
252                         numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
253                 }
254         }
255 }
256
257 void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const math::Vector<int> &offset) const {
258         if (!targetAnimation.Valid() || !targetAnimation.Running()) return;
259         const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection());
260         if (ts.TargetsHeroes()) {
261                 for (vector<Vector<int> >::size_type i(0), end(battle->NumHeroes()); i < end; ++i) {
262                         if (ts.IsSelected(i)) {
263                                 targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + offset);
264                         }
265                 }
266         } else {
267                 for (vector<Vector<int> >::size_type i(0), end(battle->MaxMonsters()); i < end; ++i) {
268                         if (ts.IsSelected(i)) {
269                                 targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + offset);
270                         }
271                 }
272         }
273 }
274
275 }