]> git.localhorst.tv Git - l2e.git/blob - src/battle/states/PerformAttacks.cpp
added Maxim's melee animation
[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 "../AttackAnimation.h"
11 #include "../BattleState.h"
12 #include "../Hero.h"
13 #include "../Monster.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 "../../geometry/operators.h"
20 #include "../../geometry/Point.h"
21 #include "../../graphics/Animation.h"
22 #include "../../graphics/Font.h"
23 #include "../../graphics/Frame.h"
24
25 #include <cstring>
26
27 using app::Application;
28 using app::Input;
29 using geometry::Point;
30 using geometry::Vector;
31 using std::vector;
32
33 namespace battle {
34
35 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
36         ctrl = &c;
37         battle->WriteOrder(order);
38         numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
39         numberPosition.reserve(numberAnimation.size());
40 }
41
42 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
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         if (attackAnimation) attackAnimation->Update();
62         CheckNumberAnimation();
63         if (HasAnimationsRunning()) return;
64         ResetAnimation();
65         AdvanceCursor();
66         if (Finished()) {
67                 battle->ClearAllAttacks();
68                 ctrl->PopState();
69                 return;
70         }
71
72         if (order[cursor].isMonster) {
73                 const Monster &monster(battle->MonsterAt(order[cursor].index));
74                 titleBarText = monster.Name();
75                 moveAnimation = 0;
76         } else {
77                 Hero &hero(battle->HeroAt(order[cursor].index));
78                 const AttackChoice &ac(battle->AttackChoiceAt(order[cursor].index));
79
80                 switch (ac.GetType()) {
81                         case AttackChoice::SWORD:
82                                 if (hero.HasWeapon()) {
83                                         titleBarText = hero.Weapon()->Name();
84                                         moveAnimation = hero.AttackAnimation();
85                                 } else {
86                                         titleBarText = "Melee attack!";
87                                         if (hero.MeleeAnimation()) {
88                                                 moveAnimation = 0;
89                                                 attackAnimation = hero.MeleeAnimation();
90                                         } else {
91                                                 moveAnimation = hero.AttackAnimation();
92                                                 attackAnimation = 0;
93                                         }
94                                 }
95                                 numberAnimation.push_back(NumberAnimation(15, battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
96                                 if (ac.Selection().TargetsEnemies()) {
97                                         numberPosition.push_back(
98                                                         battle->MonsterPositions()[ac.Selection().SingleSelection()]);
99                                 } else {
100                                         numberPosition.push_back(
101                                                         battle->HeroesPositions()[ac.Selection().SingleSelection()]);
102                                 }
103                                 break;
104                         case AttackChoice::MAGIC:
105                                 titleBarText = ac.GetSpell()->Name();
106                                 moveAnimation = hero.SpellAnimation();
107                                 break;
108                         case AttackChoice::DEFEND:
109                                 titleBarText = "Defends.";
110                                 moveAnimation = 0;
111                                 break;
112                         case AttackChoice::IKARI:
113                                 if (ac.GetItem()->HasIkari()) {
114                                         titleBarText = ac.GetItem()->GetIkari()->Name();
115                                         if (ac.GetItem()->GetIkari()->IsMagical()) {
116                                                 moveAnimation = hero.SpellAnimation();
117                                         } else {
118                                                 moveAnimation = hero.AttackAnimation();
119                                         }
120                                 }
121                                 break;
122                         case AttackChoice::ITEM:
123                                 titleBarText = ac.GetItem()->Name();
124                                 moveAnimation = 0;
125                                 break;
126                         case AttackChoice::UNDECIDED:
127                                 titleBarText = "UNDECIDED";
128                                 moveAnimation = 0;
129                                 break;
130                 }
131         }
132
133         if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(1500);
134         if (moveAnimation) moveAnimation->Start(*this);
135         if (attackAnimation) attackAnimation->Start(battle, this);
136 }
137
138 void PerformAttacks::CheckNumberAnimation() {
139         if (moveAnimation && moveAnimation->Running()) return;
140         if (attackAnimation && !attackAnimation->Finished()) return;
141         if (moveAnimation || attackAnimation) {
142                 moveAnimation = 0;
143                 attackAnimation = 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         for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
158                 if (i->Running()) return true;
159         }
160         return false;
161 }
162
163 void PerformAttacks::ResetAnimation() {
164         if (moveAnimation) moveAnimation->Stop();
165         if (attackAnimation) attackAnimation = 0;
166         titleBarTimer.Clear();
167         numberAnimation.clear();
168         numberPosition.clear();
169 }
170
171 void PerformAttacks::AdvanceCursor() {
172         ++cursor;
173         while (cursor < int(order.size())) {
174                 if (order[cursor].isMonster) {
175                         if (battle->MonsterAt(order[cursor].index).Health() > 0) break;
176                 } else {
177                         if (battle->HeroAt(order[cursor].index).Health() > 0) break;
178                 }
179                 ++cursor;
180         }
181 }
182
183
184 void PerformAttacks::UpdateWorld(float deltaT) {
185
186 }
187
188 void PerformAttacks::Render(SDL_Surface *screen) {
189         Vector<int> offset(battle->CalculateScreenOffset(screen));
190         battle->RenderBackground(screen, offset);
191         battle->RenderMonsters(screen, offset);
192         battle->RenderHeroes(screen, offset);
193         battle->RenderSmallHeroTags(screen, offset);
194         RenderTitleBar(screen, offset);
195         RenderNumbers(screen, offset);
196         if (attackAnimation) attackAnimation->Render(screen, offset);
197 }
198
199 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) {
200         if (!titleBarText || !titleBarTimer.Running()) return;
201
202         int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
203         battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->Width(), height);
204
205         Point<int> textPosition(
206                         (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
207                         battle->Res().titleFrame->BorderHeight());
208         battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
209 }
210
211 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) {
212         for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
213                 if (numberAnimation[i].Running()) {
214                         Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
215                         numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
216                 }
217         }
218 }
219
220 }