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