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