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;
30 using graphics::AnimationRunner;
35 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
37 battle->CalculateAttackOrder();
38 numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
39 numberPosition.reserve(numberAnimation.size());
42 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
43 battle->ClearAllAttacks();
47 void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) {
51 void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) {
56 void PerformAttacks::Resize(int width, int height) {
61 void PerformAttacks::HandleEvents(const Input &input) {
63 if (HasAnimationsRunning()) return;
65 battle->ApplyDamage();
67 if (battle->AttacksFinished()) {
72 battle->CalculateDamage();
74 if (battle->CurrentAttack().isMonster) {
75 Monster &monster(battle->MonsterAt(battle->CurrentAttack().index));
76 titleBarText = monster.Name();
77 targetAnimation = AnimationRunner(monster.MeleeAnimation());
78 moveAnimation = AnimationRunner(monster.AttackAnimation());
79 monster.SetAnimation(moveAnimation);
80 AddNumberAnimations(battle->MonsterAt(battle->CurrentAttack().index).GetAttackChoice().Selection());
82 Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
83 const AttackChoice &ac(battle->HeroAt(battle->CurrentAttack().index).GetAttackChoice());
85 switch (ac.GetType()) {
86 case AttackChoice::SWORD:
87 if (hero.HasWeapon()) {
88 titleBarText = hero.Weapon()->Name();
89 targetAnimation = AnimationRunner(hero.Weapon()->AttackAnimation());
91 titleBarText = "Melee attack!";
92 targetAnimation = AnimationRunner(hero.MeleeAnimation());
94 moveAnimation = AnimationRunner(hero.AttackAnimation());
95 AddNumberAnimations(ac.Selection());
97 case AttackChoice::MAGIC:
98 titleBarText = ac.GetSpell()->Name();
99 moveAnimation = AnimationRunner(hero.SpellAnimation());
101 case AttackChoice::DEFEND:
102 titleBarText = "Defends.";
103 moveAnimation.Clear();
105 case AttackChoice::IKARI:
106 if (ac.GetItem()->HasIkari()) {
107 titleBarText = ac.GetItem()->GetIkari()->Name();
108 if (ac.GetItem()->GetIkari()->IsMagical()) {
109 moveAnimation = AnimationRunner(hero.SpellAnimation());
111 moveAnimation = AnimationRunner(hero.AttackAnimation());
115 case AttackChoice::ITEM:
116 titleBarText = ac.GetItem()->Name();
117 moveAnimation.Clear();
119 case AttackChoice::UNDECIDED:
120 titleBarText = "UNDECIDED";
121 moveAnimation.Clear();
126 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850);
127 if (moveAnimation.Valid()) {
128 moveAnimation.Start(*this);
129 if (battle->CurrentAttack().isMonster) {
130 battle->MonsterAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
132 battle->HeroAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
135 if (targetAnimation.Valid()) {
136 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
138 targetAnimationTimer.Clear();
142 void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) {
143 if (ts.TargetsMonsters()) {
144 for (int i(0); i < battle->MaxMonsters(); ++i) {
146 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
147 numberPosition.push_back(
148 battle->MonsterAt(i).Position());
149 } else if (ts.IsGood(i)) {
150 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
151 numberPosition.push_back(
152 battle->MonsterAt(i).Position());
156 for (int i(0); i < battle->NumHeroes(); ++i) {
158 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
159 numberPosition.push_back(
160 battle->HeroAt(i).Position());
161 } else if (ts.IsGood(i)) {
162 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
163 numberPosition.push_back(
164 battle->HeroAt(i).Position());
170 void PerformAttacks::CheckAnimations() {
171 if (targetAnimation.Valid() && targetAnimationTimer.JustHit()) {
172 targetAnimation.Start(*this);
174 if (moveAnimation.Valid() && !moveAnimation.Finished()) return;
175 if (targetAnimation.Valid() && !targetAnimation.Finished()) return;
176 if (moveAnimation.Valid() || targetAnimation.Valid()) {
177 moveAnimation.Clear();
178 targetAnimation.Clear();
179 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
183 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
184 i->CheckTimers(*this);
189 bool PerformAttacks::HasAnimationsRunning() const {
190 if (titleBarTimer.Running()) return true;
191 if (moveAnimation.Valid() && moveAnimation.Running()) return true;
192 if (targetAnimation.Valid() && targetAnimation.Running()) return true;
193 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
194 if (i->Running()) return true;
199 void PerformAttacks::ResetAnimation() {
200 if (moveAnimation.Valid()) {
201 moveAnimation.Clear();
202 if (!battle->CurrentAttack().isMonster) {
203 battle->HeroAt(battle->CurrentAttack().index).GetAnimation().Clear();
206 if (targetAnimation.Valid()) {
207 targetAnimation.Clear();
209 titleBarTimer.Clear();
210 numberAnimation.clear();
211 numberPosition.clear();
215 void PerformAttacks::UpdateWorld(float deltaT) {
219 void PerformAttacks::Render(SDL_Surface *screen) {
220 Vector<int> offset(battle->CalculateScreenOffset(screen));
221 battle->RenderBackground(screen, offset);
222 battle->RenderMonsters(screen, offset);
223 battle->RenderHeroes(screen, offset);
224 battle->RenderSmallHeroTags(screen, offset);
225 RenderTitleBar(screen, offset);
226 RenderNumbers(screen, offset);
227 RenderTargetAnimation(screen, offset);
230 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
231 if (!titleBarText || !titleBarTimer.Running()) return;
233 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
234 battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->Width(), height);
236 Point<int> textPosition(
237 (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
238 battle->Res().titleFrame->BorderHeight());
239 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
242 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
243 for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
244 if (numberAnimation[i].Running()) {
245 Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
246 numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
251 void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector<int> &offset) const {
252 if (!targetAnimation.Valid() || !targetAnimation.Running()) return;
253 const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection());
254 if (ts.TargetsHeroes()) {
255 for (vector<Point<int> >::size_type i(0), end(battle->NumHeroes()); i < end; ++i) {
256 if (ts.IsSelected(i)) {
257 targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + offset);
261 for (vector<Point<int> >::size_type i(0), end(battle->MaxMonsters()); i < end; ++i) {
262 if (ts.IsSelected(i)) {
263 targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + offset);