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