]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/PerformAttacks.cpp
2a8f3a53b697a4aecb3e5270c4e7c65d4be69ff9
[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->WriteOrder(order);
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         AdvanceCursor();
64         if (Finished()) {
65                 battle->ClearAllAttacks();
66                 ctrl->PopState();
67                 return;
68         }
69
70         if (order[cursor].isMonster) {
71                 const Monster &monster(battle->MonsterAt(order[cursor].index));
72                 titleBarText = monster.Name();
73                 moveAnimation = 0;
74         } else {
75                 Hero &hero(battle->HeroAt(order[cursor].index));
76                 const AttackChoice &ac(battle->AttackChoiceAt(order[cursor].index));
77
78                 switch (ac.GetType()) {
79                         case AttackChoice::SWORD:
80                                 if (hero.HasWeapon()) {
81                                         titleBarText = hero.Weapon()->Name();
82                                         targetAnimation = hero.Weapon()->AttackAnimation();
83                                 } else {
84                                         titleBarText = "Melee attack!";
85                                         targetAnimation = hero.MeleeAnimation();
86                                 }
87                                 moveAnimation = hero.AttackAnimation();
88
89                                 numberAnimation.push_back(NumberAnimation(15, battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
90                                 if (ac.Selection().TargetsEnemies()) {
91                                         numberPosition.push_back(
92                                                         battle->MonsterPositions()[ac.Selection().SingleSelection()]);
93                                 } else {
94                                         numberPosition.push_back(
95                                                         battle->HeroesPositions()[ac.Selection().SingleSelection()]);
96                                 }
97                                 break;
98                         case AttackChoice::MAGIC:
99                                 titleBarText = ac.GetSpell()->Name();
100                                 moveAnimation = hero.SpellAnimation();
101                                 break;
102                         case AttackChoice::DEFEND:
103                                 titleBarText = "Defends.";
104                                 moveAnimation = 0;
105                                 break;
106                         case AttackChoice::IKARI:
107                                 if (ac.GetItem()->HasIkari()) {
108                                         titleBarText = ac.GetItem()->GetIkari()->Name();
109                                         if (ac.GetItem()->GetIkari()->IsMagical()) {
110                                                 moveAnimation = hero.SpellAnimation();
111                                         } else {
112                                                 moveAnimation = hero.AttackAnimation();
113                                         }
114                                 }
115                                 break;
116                         case AttackChoice::ITEM:
117                                 titleBarText = ac.GetItem()->Name();
118                                 moveAnimation = 0;
119                                 break;
120                         case AttackChoice::UNDECIDED:
121                                 titleBarText = "UNDECIDED";
122                                 moveAnimation = 0;
123                                 break;
124                 }
125         }
126
127         if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(1500);
128         if (moveAnimation) moveAnimation->Start(*this);
129         if (targetAnimation) {
130                 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
131         } else {
132                 targetAnimationTimer.Clear();
133         }
134 }
135
136 void PerformAttacks::CheckAnimations() {
137         if (targetAnimation && targetAnimationTimer.JustHit()) {
138                 targetAnimation->Start(*this);
139         }
140         if (moveAnimation && !moveAnimation->Finished()) return;
141         if (targetAnimation && !targetAnimation->Finished()) return;
142         if (moveAnimation || targetAnimation) {
143                 moveAnimation = 0;
144                 targetAnimation = 0;
145                 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
146                         i->Start(*this);
147                 }
148         } else {
149                 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
150                         i->CheckTimers(*this);
151                 }
152         }
153 }
154
155 bool PerformAttacks::HasAnimationsRunning() const {
156         if (titleBarTimer.Running()) return true;
157         if (moveAnimation && moveAnimation->Running()) return true;
158         if (targetAnimation && targetAnimation->Running()) return true;
159         for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
160                 if (i->Running()) return true;
161         }
162         return false;
163 }
164
165 void PerformAttacks::ResetAnimation() {
166         if (moveAnimation) {
167                 moveAnimation->Stop();
168                 moveAnimation = 0;
169         }
170         if (targetAnimation) {
171                 targetAnimation->Stop();
172                 targetAnimation = 0;
173         }
174         titleBarTimer.Clear();
175         numberAnimation.clear();
176         numberPosition.clear();
177 }
178
179 void PerformAttacks::AdvanceCursor() {
180         ++cursor;
181         while (cursor < int(order.size())) {
182                 if (order[cursor].isMonster) {
183                         if (battle->MonsterAt(order[cursor].index).Health() > 0) break;
184                 } else {
185                         if (battle->HeroAt(order[cursor].index).Health() > 0) break;
186                 }
187                 ++cursor;
188         }
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 (order[cursor].isMonster) return; // no monsters for now
231         const TargetSelection &ts(battle->AttackChoiceAt(order[cursor].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 }