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) {
42 battle->ClearAllAttacks();
46 void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) {
50 void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) {
55 void PerformAttacks::Resize(int width, int height) {
60 void PerformAttacks::HandleEvents(const Input &input) {
62 if (HasAnimationsRunning()) return;
64 battle->ApplyDamage();
66 if (battle->AttacksFinished()) {
71 battle->CalculateDamage();
73 if (battle->CurrentAttack().isMonster) {
74 Monster &monster(battle->MonsterAt(battle->CurrentAttack().index));
75 titleBarText = monster.Name();
76 moveAnimation = monster.AttackAnimation();
77 targetAnimation = monster.MeleeAnimation();
78 AddNumberAnimations(battle->MonsterAttackChoiceAt(battle->CurrentAttack().index).Selection());
80 Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
81 const AttackChoice &ac(battle->AttackChoiceAt(battle->CurrentAttack().index));
83 switch (ac.GetType()) {
84 case AttackChoice::SWORD:
85 if (hero.HasWeapon()) {
86 titleBarText = hero.Weapon()->Name();
87 targetAnimation = hero.Weapon()->AttackAnimation();
89 titleBarText = "Melee attack!";
90 targetAnimation = hero.MeleeAnimation();
92 moveAnimation = hero.AttackAnimation();
93 AddNumberAnimations(ac.Selection());
95 case AttackChoice::MAGIC:
96 titleBarText = ac.GetSpell()->Name();
97 moveAnimation = hero.SpellAnimation();
99 case AttackChoice::DEFEND:
100 titleBarText = "Defends.";
103 case AttackChoice::IKARI:
104 if (ac.GetItem()->HasIkari()) {
105 titleBarText = ac.GetItem()->GetIkari()->Name();
106 if (ac.GetItem()->GetIkari()->IsMagical()) {
107 moveAnimation = hero.SpellAnimation();
109 moveAnimation = hero.AttackAnimation();
113 case AttackChoice::ITEM:
114 titleBarText = ac.GetItem()->Name();
117 case AttackChoice::UNDECIDED:
118 titleBarText = "UNDECIDED";
124 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850);
125 if (moveAnimation) moveAnimation->Start(*this);
126 if (targetAnimation) {
127 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
129 targetAnimationTimer.Clear();
133 void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) {
134 if (ts.TargetsEnemies()) {
135 for (int i(0); i < battle->MaxMonsters(); ++i) {
137 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
138 numberPosition.push_back(
139 battle->MonsterPositions()[i]);
140 } else if (ts.IsGood(i)) {
141 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
142 numberPosition.push_back(
143 battle->MonsterPositions()[i]);
147 for (int i(0); i < battle->NumHeroes(); ++i) {
149 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
150 numberPosition.push_back(
151 battle->HeroesPositions()[i]);
152 } else if (ts.IsGood(i)) {
153 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
154 numberPosition.push_back(
155 battle->HeroesPositions()[i]);
161 void PerformAttacks::CheckAnimations() {
162 if (targetAnimation && targetAnimationTimer.JustHit()) {
163 targetAnimation->Start(*this);
165 if (moveAnimation && !moveAnimation->Finished()) return;
166 if (targetAnimation && !targetAnimation->Finished()) return;
167 if (moveAnimation || targetAnimation) {
170 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
174 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
175 i->CheckTimers(*this);
180 bool PerformAttacks::HasAnimationsRunning() const {
181 if (titleBarTimer.Running()) return true;
182 if (moveAnimation && moveAnimation->Running()) return true;
183 if (targetAnimation && targetAnimation->Running()) return true;
184 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
185 if (i->Running()) return true;
190 void PerformAttacks::ResetAnimation() {
192 moveAnimation->Stop();
195 if (targetAnimation) {
196 targetAnimation->Stop();
199 titleBarTimer.Clear();
200 numberAnimation.clear();
201 numberPosition.clear();
205 void PerformAttacks::UpdateWorld(float deltaT) {
209 void PerformAttacks::Render(SDL_Surface *screen) {
210 Vector<int> offset(battle->CalculateScreenOffset(screen));
211 battle->RenderBackground(screen, offset);
212 battle->RenderMonsters(screen, offset);
213 battle->RenderHeroes(screen, offset);
214 battle->RenderSmallHeroTags(screen, offset);
215 RenderTitleBar(screen, offset);
216 RenderNumbers(screen, offset);
217 RenderTargetAnimation(screen, offset);
220 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
221 if (!titleBarText || !titleBarTimer.Running()) return;
223 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
224 battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->Width(), height);
226 Point<int> textPosition(
227 (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
228 battle->Res().titleFrame->BorderHeight());
229 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
232 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
233 for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
234 if (numberAnimation[i].Running()) {
235 Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
236 numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
241 void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector<int> &offset) const {
242 if (!targetAnimation || !targetAnimation->Running()) return;
243 const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection());
244 const vector<Point<int> > &positions(ts.TargetsHeroes() ? battle->HeroesPositions() : battle->MonsterPositions());
245 for (vector<Point<int> >::size_type i(0), end(positions.size()); i < end; ++i) {
246 if (ts.IsSelected(i)) {
247 targetAnimation->DrawCenter(screen, positions[i] + offset);