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 "../../graphics/Animation.h"
19 #include "../../graphics/Font.h"
20 #include "../../graphics/Frame.h"
24 using app::Application;
26 using geometry::Vector;
27 using graphics::AnimationRunner;
32 void PerformAttacks::OnEnterState(SDL_Surface *screen) {
33 battle->CalculateAttackOrder();
34 numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
35 numberPosition.reserve(numberAnimation.size());
38 void PerformAttacks::OnExitState(SDL_Surface *screen) {
39 battle->ClearAllAttacks();
42 void PerformAttacks::OnResumeState(SDL_Surface *screen) {
46 void PerformAttacks::OnPauseState(SDL_Surface *screen) {
51 void PerformAttacks::OnResize(int width, int height) {
56 void PerformAttacks::HandleEvents(const Input &input) {
58 if (HasAnimationsRunning()) return;
60 battle->ApplyDamage();
62 if (battle->AttacksFinished()) {
67 battle->CalculateDamage();
69 if (battle->CurrentAttack().isMonster) {
70 Monster &monster(battle->MonsterAt(battle->CurrentAttack().index));
71 titleBarText = monster.Name();
72 targetAnimation = AnimationRunner(monster.MeleeAnimation());
73 moveAnimation = AnimationRunner(monster.AttackAnimation());
74 monster.SetAnimation(moveAnimation);
75 AddNumberAnimations(battle->MonsterAt(battle->CurrentAttack().index).GetAttackChoice().Selection());
77 Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
78 const AttackChoice &ac(battle->HeroAt(battle->CurrentAttack().index).GetAttackChoice());
80 switch (ac.GetType()) {
81 case AttackChoice::SWORD:
82 if (hero.HasWeapon()) {
83 titleBarText = hero.Weapon()->Name();
84 targetAnimation = AnimationRunner(hero.Weapon()->AttackAnimation());
86 titleBarText = "Melee attack!";
87 targetAnimation = AnimationRunner(hero.MeleeAnimation());
89 moveAnimation = AnimationRunner(hero.AttackAnimation());
90 AddNumberAnimations(ac.Selection());
92 case AttackChoice::MAGIC:
93 titleBarText = ac.GetSpell()->Name();
94 moveAnimation = AnimationRunner(hero.SpellAnimation());
96 case AttackChoice::DEFEND:
97 titleBarText = "Defends.";
98 moveAnimation.Clear();
100 case AttackChoice::IKARI:
101 if (ac.GetItem()->HasIkari()) {
102 titleBarText = ac.GetItem()->GetIkari()->Name();
103 if (ac.GetItem()->GetIkari()->IsMagical()) {
104 moveAnimation = AnimationRunner(hero.SpellAnimation());
106 moveAnimation = AnimationRunner(hero.AttackAnimation());
110 case AttackChoice::ITEM:
111 titleBarText = ac.GetItem()->Name();
112 moveAnimation.Clear();
114 case AttackChoice::UNDECIDED:
115 titleBarText = "UNDECIDED";
116 moveAnimation.Clear();
121 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850);
122 if (moveAnimation.Valid()) {
123 moveAnimation.Start(*this);
124 if (battle->CurrentAttack().isMonster) {
125 battle->MonsterAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
127 battle->HeroAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
130 if (targetAnimation.Valid()) {
131 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
133 targetAnimationTimer.Clear();
137 void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) {
138 if (ts.TargetsMonsters()) {
139 for (int i(0); i < battle->MaxMonsters(); ++i) {
141 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
142 numberPosition.push_back(
143 battle->MonsterAt(i).Position());
144 } else if (ts.IsGood(i)) {
145 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
146 numberPosition.push_back(
147 battle->MonsterAt(i).Position());
151 for (int i(0); i < battle->NumHeroes(); ++i) {
153 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
154 numberPosition.push_back(
155 battle->HeroAt(i).Position());
156 } else if (ts.IsGood(i)) {
157 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
158 numberPosition.push_back(
159 battle->HeroAt(i).Position());
165 void PerformAttacks::CheckAnimations() {
166 if (targetAnimation.Valid() && targetAnimationTimer.JustHit()) {
167 targetAnimation.Start(*this);
169 if (moveAnimation.Valid() && !moveAnimation.Finished()) return;
170 if (targetAnimation.Valid() && !targetAnimation.Finished()) return;
171 if (moveAnimation.Valid() || targetAnimation.Valid()) {
172 moveAnimation.Clear();
173 targetAnimation.Clear();
174 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
178 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
179 i->CheckTimers(*this);
184 bool PerformAttacks::HasAnimationsRunning() const {
185 if (titleBarTimer.Running()) return true;
186 if (moveAnimation.Valid() && moveAnimation.Running()) return true;
187 if (targetAnimation.Valid() && targetAnimation.Running()) return true;
188 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
189 if (i->Running()) return true;
194 void PerformAttacks::ResetAnimation() {
195 if (moveAnimation.Valid()) {
196 moveAnimation.Clear();
197 if (!battle->CurrentAttack().isMonster) {
198 battle->HeroAt(battle->CurrentAttack().index).GetAnimation().Clear();
201 if (targetAnimation.Valid()) {
202 targetAnimation.Clear();
204 titleBarTimer.Clear();
205 numberAnimation.clear();
206 numberPosition.clear();
210 void PerformAttacks::UpdateWorld(float deltaT) {
214 void PerformAttacks::Render(SDL_Surface *screen) {
215 Vector<int> offset(battle->CalculateScreenOffset(screen));
216 battle->RenderBackground(screen, offset);
217 battle->RenderMonsters(screen, offset);
218 battle->RenderHeroes(screen, offset);
219 battle->RenderSmallHeroTags(screen, offset);
220 RenderTitleBar(screen, offset);
221 RenderNumbers(screen, offset);
222 RenderTargetAnimation(screen, offset);
225 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
226 if (!titleBarText || !titleBarTimer.Running()) return;
228 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
229 battle->Res().titleFrame->Draw(screen, offset, battle->Width(), height);
231 Vector<int> textPosition(
232 (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
233 battle->Res().titleFrame->BorderHeight());
234 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
237 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
238 for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
239 if (numberAnimation[i].Running()) {
240 Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
241 numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
246 void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector<int> &offset) const {
247 if (!targetAnimation.Valid() || !targetAnimation.Running()) return;
248 const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection());
249 if (ts.TargetsHeroes()) {
250 for (vector<Vector<int> >::size_type i(0), end(battle->NumHeroes()); i < end; ++i) {
251 if (ts.IsSelected(i)) {
252 targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + offset);
256 for (vector<Vector<int> >::size_type i(0), end(battle->MaxMonsters()); i < end; ++i) {
257 if (ts.IsSelected(i)) {
258 targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + offset);