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