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