4 * Created on: Aug 10, 2012
8 #include "PerformAttacks.h"
10 #include "../BattleState.h"
12 #include "../Monster.h"
13 #include "../TargetSelection.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 "../../graphics/Animation.h"
20 #include "../../graphics/Font.h"
21 #include "../../graphics/Frame.h"
25 using app::Application;
28 using graphics::AnimationRunner;
33 void PerformAttacks::OnEnterState(SDL_Surface *screen) {
34 battle->CalculateAttackOrder();
35 numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
36 numberPosition.reserve(numberAnimation.size());
39 void PerformAttacks::OnExitState(SDL_Surface *screen) {
40 battle->ClearAllAttacks();
43 void PerformAttacks::OnResumeState(SDL_Surface *screen) {
47 void PerformAttacks::OnPauseState(SDL_Surface *screen) {
52 void PerformAttacks::OnResize(int width, int height) {
57 void PerformAttacks::HandleEvents(const Input &input) {
59 if (HasAnimationsRunning()) return;
61 battle->ApplyDamage();
63 if (battle->AttacksFinished()) {
68 battle->CalculateDamage();
70 if (battle->CurrentAttack().IsMonster()) {
71 Monster &monster(battle->MonsterAt(battle->CurrentAttack().index));
72 titleBarText = monster.Name();
73 targetAnimation = AnimationRunner(monster.MeleeAnimation());
74 moveAnimation = AnimationRunner(monster.AttackAnimation());
75 monster.SetAnimation(moveAnimation);
76 AddNumberAnimations(battle->MonsterAt(battle->CurrentAttack().index).GetAttackChoice().Selection());
77 } else if (battle->CurrentAttack().IsCapsule()) {
78 Capsule &capsule(battle->GetCapsule());
79 titleBarText = capsule.Name();
80 targetAnimation = AnimationRunner(capsule.MeleeAnimation());
81 moveAnimation = AnimationRunner(capsule.AttackAnimation());
82 capsule.SetAnimation(moveAnimation);
83 AddNumberAnimations(capsule.GetAttackChoice().Selection());
85 Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
86 const AttackChoice &ac(battle->HeroAt(battle->CurrentAttack().index).GetAttackChoice());
88 switch (ac.GetType()) {
89 case AttackChoice::SWORD:
90 if (hero.HasWeapon()) {
91 titleBarText = hero.Weapon()->Name();
92 targetAnimation = AnimationRunner(hero.Weapon()->AttackAnimation());
94 titleBarText = "Melee attack!";
95 targetAnimation = AnimationRunner(hero.MeleeAnimation());
97 moveAnimation = AnimationRunner(hero.AttackAnimation());
98 AddNumberAnimations(ac.Selection());
100 case AttackChoice::MAGIC:
101 titleBarText = ac.GetSpell()->Name();
102 moveAnimation = AnimationRunner(hero.SpellAnimation());
104 case AttackChoice::DEFEND:
105 titleBarText = "Defends.";
106 moveAnimation.Clear();
108 case AttackChoice::IKARI:
109 if (ac.GetItem()->HasIkari()) {
110 titleBarText = ac.GetItem()->GetIkari()->Name();
111 if (ac.GetItem()->GetIkari()->IsMagical()) {
112 moveAnimation = AnimationRunner(hero.SpellAnimation());
114 moveAnimation = AnimationRunner(hero.AttackAnimation());
118 case AttackChoice::ITEM:
119 titleBarText = ac.GetItem()->Name();
120 moveAnimation.Clear();
122 case AttackChoice::UNDECIDED:
123 titleBarText = "UNDECIDED";
124 moveAnimation.Clear();
129 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850);
130 if (moveAnimation.Valid()) {
131 moveAnimation.Start(*this);
132 if (battle->CurrentAttack().IsMonster()) {
133 battle->MonsterAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
134 } else if (battle->CurrentAttack().IsHero()) {
135 battle->HeroAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
137 battle->GetCapsule().SetAnimation(moveAnimation);
140 if (targetAnimation.Valid()) {
141 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
143 targetAnimationTimer.Clear();
147 void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) {
148 if (ts.TargetsMonsters()) {
149 for (int i(0); i < battle->MaxMonsters(); ++i) {
151 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
152 numberPosition.push_back(
153 battle->MonsterAt(i).Position());
154 } else if (ts.IsGood(i)) {
155 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
156 numberPosition.push_back(
157 battle->MonsterAt(i).Position());
161 for (int i(0); i < battle->NumHeroes(); ++i) {
163 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
164 numberPosition.push_back(
165 battle->HeroAt(i).Position());
166 } else if (ts.IsGood(i)) {
167 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
168 numberPosition.push_back(
169 battle->HeroAt(i).Position());
175 void PerformAttacks::CheckAnimations() {
176 if (targetAnimation.Valid() && targetAnimationTimer.JustHit()) {
177 targetAnimation.Start(*this);
179 if (moveAnimation.Valid() && !moveAnimation.Finished()) return;
180 if (targetAnimation.Valid() && !targetAnimation.Finished()) return;
181 if (moveAnimation.Valid() || targetAnimation.Valid()) {
182 moveAnimation.Clear();
183 targetAnimation.Clear();
184 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
188 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
189 i->CheckTimers(*this);
194 bool PerformAttacks::HasAnimationsRunning() const {
195 if (titleBarTimer.Running()) return true;
196 if (moveAnimation.Valid() && moveAnimation.Running()) return true;
197 if (targetAnimation.Valid() && targetAnimation.Running()) return true;
198 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
199 if (i->Running()) return true;
204 void PerformAttacks::ResetAnimation() {
205 if (moveAnimation.Valid()) {
206 moveAnimation.Clear();
207 if (!battle->CurrentAttack().IsMonster()) {
208 battle->HeroAt(battle->CurrentAttack().index).GetAnimation().Clear();
211 if (targetAnimation.Valid()) {
212 targetAnimation.Clear();
214 titleBarTimer.Clear();
215 numberAnimation.clear();
216 numberPosition.clear();
220 void PerformAttacks::UpdateWorld(Uint32 deltaT) {
224 void PerformAttacks::Render(SDL_Surface *screen) {
225 Vector<int> offset(battle->CalculateScreenOffset(screen));
226 battle->RenderBackground(screen, offset);
227 battle->RenderMonsters(screen, offset);
228 battle->RenderHeroes(screen, offset);
229 battle->RenderCapsule(screen, offset);
230 battle->RenderSmallHeroTags(screen, offset);
231 RenderTitleBar(screen, offset);
232 RenderNumbers(screen, offset);
233 RenderTargetAnimation(screen, offset);
236 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
237 if (!titleBarText || !titleBarTimer.Running()) return;
239 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
240 battle->Res().titleFrame->Draw(screen, offset, battle->Width(), height);
242 Vector<int> textPosition(
243 (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
244 battle->Res().titleFrame->BorderHeight());
245 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
248 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
249 for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
250 if (numberAnimation[i].Running()) {
251 Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
252 numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
257 void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const math::Vector<int> &offset) const {
258 if (!targetAnimation.Valid() || !targetAnimation.Running()) return;
259 const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection());
260 if (ts.TargetsHeroes()) {
261 for (vector<Vector<int> >::size_type i(0), end(battle->NumHeroes()); i < end; ++i) {
262 if (ts.IsSelected(i)) {
263 targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + offset);
267 for (vector<Vector<int> >::size_type i(0), end(battle->MaxMonsters()); i < end; ++i) {
268 if (ts.IsSelected(i)) {
269 targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + offset);