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->CalculateAttackOrder();
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;
64 if (battle->AttacksFinished()) {
65 battle->ClearAllAttacks();
70 battle->CalculateDamage();
72 if (battle->CurrentAttack().isMonster) {
73 const Monster &monster(battle->MonsterAt(battle->CurrentAttack().index));
74 titleBarText = monster.Name();
77 Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
78 const AttackChoice &ac(battle->AttackChoiceAt(battle->CurrentAttack().index));
80 switch (ac.GetType()) {
81 case AttackChoice::SWORD:
82 if (hero.HasWeapon()) {
83 titleBarText = hero.Weapon()->Name();
84 targetAnimation = hero.Weapon()->AttackAnimation();
86 titleBarText = "Melee attack!";
87 targetAnimation = hero.MeleeAnimation();
89 moveAnimation = hero.AttackAnimation();
91 if (ac.Selection().TargetsEnemies()) {
92 for (int i(0); i < battle->MaxMonsters(); ++i) {
93 if (ac.Selection().IsSelected(i)) {
94 numberAnimation.push_back(NumberAnimation(ac.Selection().GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
95 numberPosition.push_back(
96 battle->MonsterPositions()[i]);
100 for (int i(0); i < battle->NumHeroes(); ++i) {
101 if (ac.Selection().IsSelected(i)) {
102 numberAnimation.push_back(NumberAnimation(ac.Selection().GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
103 numberPosition.push_back(
104 battle->HeroesPositions()[i]);
109 case AttackChoice::MAGIC:
110 titleBarText = ac.GetSpell()->Name();
111 moveAnimation = hero.SpellAnimation();
113 case AttackChoice::DEFEND:
114 titleBarText = "Defends.";
117 case AttackChoice::IKARI:
118 if (ac.GetItem()->HasIkari()) {
119 titleBarText = ac.GetItem()->GetIkari()->Name();
120 if (ac.GetItem()->GetIkari()->IsMagical()) {
121 moveAnimation = hero.SpellAnimation();
123 moveAnimation = hero.AttackAnimation();
127 case AttackChoice::ITEM:
128 titleBarText = ac.GetItem()->Name();
131 case AttackChoice::UNDECIDED:
132 titleBarText = "UNDECIDED";
138 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(1500);
139 if (moveAnimation) moveAnimation->Start(*this);
140 if (targetAnimation) {
141 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
143 targetAnimationTimer.Clear();
147 void PerformAttacks::CheckAnimations() {
148 if (targetAnimation && targetAnimationTimer.JustHit()) {
149 targetAnimation->Start(*this);
151 if (moveAnimation && !moveAnimation->Finished()) return;
152 if (targetAnimation && !targetAnimation->Finished()) return;
153 if (moveAnimation || targetAnimation) {
156 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
160 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
161 i->CheckTimers(*this);
166 bool PerformAttacks::HasAnimationsRunning() const {
167 if (titleBarTimer.Running()) return true;
168 if (moveAnimation && moveAnimation->Running()) return true;
169 if (targetAnimation && targetAnimation->Running()) return true;
170 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
171 if (i->Running()) return true;
176 void PerformAttacks::ResetAnimation() {
178 moveAnimation->Stop();
181 if (targetAnimation) {
182 targetAnimation->Stop();
185 titleBarTimer.Clear();
186 numberAnimation.clear();
187 numberPosition.clear();
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 (battle->CurrentAttack().isMonster) return; // no monsters for now
230 const TargetSelection &ts(battle->AttackChoiceAt(battle->CurrentAttack().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);