]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/PerformAttacks.cpp
renamed TargetSelections Enemies to Monsters
[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->MonsterAt(battle->CurrentAttack().index).GetAttackChoice().Selection());
81         } else {
82                 Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
83                 const AttackChoice &ac(battle->HeroAt(battle->CurrentAttack().index).GetAttackChoice());
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         }
125
126         if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850);
127         if (moveAnimation.Valid()) {
128                 moveAnimation.Start(*this);
129                 if (battle->CurrentAttack().isMonster) {
130                         battle->MonsterAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
131                 } else {
132                         battle->HeroAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
133                 }
134         }
135         if (targetAnimation.Valid()) {
136                 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
137         } else {
138                 targetAnimationTimer.Clear();
139         }
140 }
141
142 void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) {
143         if (ts.TargetsMonsters()) {
144                 for (int i(0); i < battle->MaxMonsters(); ++i) {
145                         if (ts.IsBad(i)) {
146                                 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
147                                 numberPosition.push_back(
148                                                 battle->MonsterAt(i).Position());
149                         } else if (ts.IsGood(i)) {
150                                 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
151                                 numberPosition.push_back(
152                                                 battle->MonsterAt(i).Position());
153                         }
154                 }
155         } else {
156                 for (int i(0); i < battle->NumHeroes(); ++i) {
157                         if (ts.IsBad(i)) {
158                                 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
159                                 numberPosition.push_back(
160                                                 battle->HeroAt(i).Position());
161                         } else if (ts.IsGood(i)) {
162                                 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
163                                 numberPosition.push_back(
164                                                 battle->HeroAt(i).Position());
165                         }
166                 }
167         }
168 }
169
170 void PerformAttacks::CheckAnimations() {
171         if (targetAnimation.Valid() && targetAnimationTimer.JustHit()) {
172                 targetAnimation.Start(*this);
173         }
174         if (moveAnimation.Valid() && !moveAnimation.Finished()) return;
175         if (targetAnimation.Valid() && !targetAnimation.Finished()) return;
176         if (moveAnimation.Valid() || targetAnimation.Valid()) {
177                 moveAnimation.Clear();
178                 targetAnimation.Clear();
179                 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
180                         i->Start(*this);
181                 }
182         } else {
183                 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
184                         i->CheckTimers(*this);
185                 }
186         }
187 }
188
189 bool PerformAttacks::HasAnimationsRunning() const {
190         if (titleBarTimer.Running()) return true;
191         if (moveAnimation.Valid() && moveAnimation.Running()) return true;
192         if (targetAnimation.Valid() && targetAnimation.Running()) return true;
193         for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
194                 if (i->Running()) return true;
195         }
196         return false;
197 }
198
199 void PerformAttacks::ResetAnimation() {
200         if (moveAnimation.Valid()) {
201                 moveAnimation.Clear();
202                 if (!battle->CurrentAttack().isMonster) {
203                         battle->HeroAt(battle->CurrentAttack().index).GetAnimation().Clear();
204                 }
205         }
206         if (targetAnimation.Valid()) {
207                 targetAnimation.Clear();
208         }
209         titleBarTimer.Clear();
210         numberAnimation.clear();
211         numberPosition.clear();
212 }
213
214
215 void PerformAttacks::UpdateWorld(float deltaT) {
216
217 }
218
219 void PerformAttacks::Render(SDL_Surface *screen) {
220         Vector<int> offset(battle->CalculateScreenOffset(screen));
221         battle->RenderBackground(screen, offset);
222         battle->RenderMonsters(screen, offset);
223         battle->RenderHeroes(screen, offset);
224         battle->RenderSmallHeroTags(screen, offset);
225         RenderTitleBar(screen, offset);
226         RenderNumbers(screen, offset);
227         RenderTargetAnimation(screen, offset);
228 }
229
230 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
231         if (!titleBarText || !titleBarTimer.Running()) return;
232
233         int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
234         battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->Width(), height);
235
236         Point<int> textPosition(
237                         (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
238                         battle->Res().titleFrame->BorderHeight());
239         battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
240 }
241
242 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
243         for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
244                 if (numberAnimation[i].Running()) {
245                         Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
246                         numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
247                 }
248         }
249 }
250
251 void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector<int> &offset) const {
252         if (!targetAnimation.Valid() || !targetAnimation.Running()) return;
253         const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection());
254         if (ts.TargetsHeroes()) {
255                 for (vector<Point<int> >::size_type i(0), end(battle->NumHeroes()); i < end; ++i) {
256                         if (ts.IsSelected(i)) {
257                                 targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + offset);
258                         }
259                 }
260         } else {
261                 for (vector<Point<int> >::size_type i(0), end(battle->MaxMonsters()); i < end; ++i) {
262                         if (ts.IsSelected(i)) {
263                                 targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + offset);
264                         }
265                 }
266         }
267 }
268
269 }