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::EnterState(Application &c, 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::ExitState(Application &c, SDL_Surface *screen) {
40 battle->ClearAllAttacks();
44 void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) {
48 void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) {
53 void PerformAttacks::Resize(int width, int height) {
58 void PerformAttacks::HandleEvents(const Input &input) {
60 if (HasAnimationsRunning()) return;
62 battle->ApplyDamage();
64 if (battle->AttacksFinished()) {
69 battle->CalculateDamage();
71 if (battle->CurrentAttack().isMonster) {
72 Monster &monster(battle->MonsterAt(battle->CurrentAttack().index));
73 titleBarText = monster.Name();
74 targetAnimation = AnimationRunner(monster.MeleeAnimation());
75 moveAnimation = AnimationRunner(monster.AttackAnimation());
76 monster.SetAnimation(moveAnimation);
77 AddNumberAnimations(battle->MonsterAt(battle->CurrentAttack().index).GetAttackChoice().Selection());
79 Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
80 const AttackChoice &ac(battle->HeroAt(battle->CurrentAttack().index).GetAttackChoice());
82 switch (ac.GetType()) {
83 case AttackChoice::SWORD:
84 if (hero.HasWeapon()) {
85 titleBarText = hero.Weapon()->Name();
86 targetAnimation = AnimationRunner(hero.Weapon()->AttackAnimation());
88 titleBarText = "Melee attack!";
89 targetAnimation = AnimationRunner(hero.MeleeAnimation());
91 moveAnimation = AnimationRunner(hero.AttackAnimation());
92 AddNumberAnimations(ac.Selection());
94 case AttackChoice::MAGIC:
95 titleBarText = ac.GetSpell()->Name();
96 moveAnimation = AnimationRunner(hero.SpellAnimation());
98 case AttackChoice::DEFEND:
99 titleBarText = "Defends.";
100 moveAnimation.Clear();
102 case AttackChoice::IKARI:
103 if (ac.GetItem()->HasIkari()) {
104 titleBarText = ac.GetItem()->GetIkari()->Name();
105 if (ac.GetItem()->GetIkari()->IsMagical()) {
106 moveAnimation = AnimationRunner(hero.SpellAnimation());
108 moveAnimation = AnimationRunner(hero.AttackAnimation());
112 case AttackChoice::ITEM:
113 titleBarText = ac.GetItem()->Name();
114 moveAnimation.Clear();
116 case AttackChoice::UNDECIDED:
117 titleBarText = "UNDECIDED";
118 moveAnimation.Clear();
123 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850);
124 if (moveAnimation.Valid()) {
125 moveAnimation.Start(*this);
126 if (battle->CurrentAttack().isMonster) {
127 battle->MonsterAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
129 battle->HeroAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
132 if (targetAnimation.Valid()) {
133 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
135 targetAnimationTimer.Clear();
139 void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) {
140 if (ts.TargetsMonsters()) {
141 for (int i(0); i < battle->MaxMonsters(); ++i) {
143 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
144 numberPosition.push_back(
145 battle->MonsterAt(i).Position());
146 } else if (ts.IsGood(i)) {
147 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
148 numberPosition.push_back(
149 battle->MonsterAt(i).Position());
153 for (int i(0); i < battle->NumHeroes(); ++i) {
155 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
156 numberPosition.push_back(
157 battle->HeroAt(i).Position());
158 } else if (ts.IsGood(i)) {
159 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
160 numberPosition.push_back(
161 battle->HeroAt(i).Position());
167 void PerformAttacks::CheckAnimations() {
168 if (targetAnimation.Valid() && targetAnimationTimer.JustHit()) {
169 targetAnimation.Start(*this);
171 if (moveAnimation.Valid() && !moveAnimation.Finished()) return;
172 if (targetAnimation.Valid() && !targetAnimation.Finished()) return;
173 if (moveAnimation.Valid() || targetAnimation.Valid()) {
174 moveAnimation.Clear();
175 targetAnimation.Clear();
176 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
180 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
181 i->CheckTimers(*this);
186 bool PerformAttacks::HasAnimationsRunning() const {
187 if (titleBarTimer.Running()) return true;
188 if (moveAnimation.Valid() && moveAnimation.Running()) return true;
189 if (targetAnimation.Valid() && targetAnimation.Running()) return true;
190 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
191 if (i->Running()) return true;
196 void PerformAttacks::ResetAnimation() {
197 if (moveAnimation.Valid()) {
198 moveAnimation.Clear();
199 if (!battle->CurrentAttack().isMonster) {
200 battle->HeroAt(battle->CurrentAttack().index).GetAnimation().Clear();
203 if (targetAnimation.Valid()) {
204 targetAnimation.Clear();
206 titleBarTimer.Clear();
207 numberAnimation.clear();
208 numberPosition.clear();
212 void PerformAttacks::UpdateWorld(float deltaT) {
216 void PerformAttacks::Render(SDL_Surface *screen) {
217 Vector<int> offset(battle->CalculateScreenOffset(screen));
218 battle->RenderBackground(screen, offset);
219 battle->RenderMonsters(screen, offset);
220 battle->RenderHeroes(screen, offset);
221 battle->RenderSmallHeroTags(screen, offset);
222 RenderTitleBar(screen, offset);
223 RenderNumbers(screen, offset);
224 RenderTargetAnimation(screen, offset);
227 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
228 if (!titleBarText || !titleBarTimer.Running()) return;
230 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
231 battle->Res().titleFrame->Draw(screen, offset, battle->Width(), height);
233 Vector<int> textPosition(
234 (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
235 battle->Res().titleFrame->BorderHeight());
236 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
239 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
240 for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
241 if (numberAnimation[i].Running()) {
242 Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
243 numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
248 void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector<int> &offset) const {
249 if (!targetAnimation.Valid() || !targetAnimation.Running()) return;
250 const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection());
251 if (ts.TargetsHeroes()) {
252 for (vector<Vector<int> >::size_type i(0), end(battle->NumHeroes()); i < end; ++i) {
253 if (ts.IsSelected(i)) {
254 targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + offset);
258 for (vector<Vector<int> >::size_type i(0), end(battle->MaxMonsters()); i < end; ++i) {
259 if (ts.IsSelected(i)) {
260 targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + offset);