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