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