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