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