4 * Created on: Aug 10, 2012
8 #include "PerformAttacks.h"
10 #include "../BattleState.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"
26 using app::Application;
28 using geometry::Point;
29 using geometry::Vector;
34 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
36 battle->WriteOrder(order);
37 numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
38 numberPosition.reserve(numberAnimation.size());
41 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
45 void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) {
49 void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) {
54 void PerformAttacks::Resize(int width, int height) {
59 void PerformAttacks::HandleEvents(const Input &input) {
61 if (HasAnimationsRunning()) return;
65 battle->ClearAllAttacks();
70 if (order[cursor].isMonster) {
71 const Monster &monster(battle->MonsterAt(order[cursor].index));
72 titleBarText = monster.Name();
75 Hero &hero(battle->HeroAt(order[cursor].index));
76 const AttackChoice &ac(battle->AttackChoiceAt(order[cursor].index));
78 switch (ac.GetType()) {
79 case AttackChoice::SWORD:
80 if (hero.HasWeapon()) {
81 titleBarText = hero.Weapon()->Name();
83 titleBarText = "Melee attack!";
85 moveAnimation = hero.AttackAnimation();
86 targetAnimation = hero.MeleeAnimation();
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()]);
93 numberPosition.push_back(
94 battle->HeroesPositions()[ac.Selection().SingleSelection()]);
97 case AttackChoice::MAGIC:
98 titleBarText = ac.GetSpell()->Name();
99 moveAnimation = hero.SpellAnimation();
101 case AttackChoice::DEFEND:
102 titleBarText = "Defends.";
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();
111 moveAnimation = hero.AttackAnimation();
115 case AttackChoice::ITEM:
116 titleBarText = ac.GetItem()->Name();
119 case AttackChoice::UNDECIDED:
120 titleBarText = "UNDECIDED";
126 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(1500);
127 if (moveAnimation) moveAnimation->Start(*this);
128 if (targetAnimation) {
129 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
131 targetAnimationTimer.Clear();
135 void PerformAttacks::CheckAnimations() {
136 if (targetAnimation && targetAnimationTimer.JustHit()) {
137 targetAnimation->Start(*this);
139 if (moveAnimation && !moveAnimation->Finished()) return;
140 if (targetAnimation && !targetAnimation->Finished()) return;
141 if (moveAnimation || targetAnimation) {
144 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
148 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
149 i->CheckTimers(*this);
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;
164 void PerformAttacks::ResetAnimation() {
166 moveAnimation->Stop();
169 if (targetAnimation) {
170 targetAnimation->Stop();
173 titleBarTimer.Clear();
174 numberAnimation.clear();
175 numberPosition.clear();
178 void PerformAttacks::AdvanceCursor() {
180 while (cursor < int(order.size())) {
181 if (order[cursor].isMonster) {
182 if (battle->MonsterAt(order[cursor].index).Health() > 0) break;
184 if (battle->HeroAt(order[cursor].index).Health() > 0) break;
191 void PerformAttacks::UpdateWorld(float deltaT) {
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);
206 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
207 if (!titleBarText || !titleBarTimer.Running()) return;
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);
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);
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);
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);