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->MonsterAttackChoiceAt(battle->CurrentAttack().index).Selection());
82 Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
83 const AttackChoice &ac(battle->AttackChoiceAt(battle->CurrentAttack().index));
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();
124 hero.SetAnimation(moveAnimation);
127 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850);
128 if (moveAnimation.Valid()) moveAnimation.Start(*this);
129 if (targetAnimation.Valid()) {
130 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
132 targetAnimationTimer.Clear();
136 void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) {
137 if (ts.TargetsEnemies()) {
138 for (int i(0); i < battle->MaxMonsters(); ++i) {
140 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
141 numberPosition.push_back(
142 battle->MonsterPositions()[i]);
143 } else if (ts.IsGood(i)) {
144 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
145 numberPosition.push_back(
146 battle->MonsterPositions()[i]);
150 for (int i(0); i < battle->NumHeroes(); ++i) {
152 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
153 numberPosition.push_back(
154 battle->HeroesPositions()[i]);
155 } else if (ts.IsGood(i)) {
156 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
157 numberPosition.push_back(
158 battle->HeroesPositions()[i]);
164 void PerformAttacks::CheckAnimations() {
165 if (targetAnimation.Valid() && targetAnimationTimer.JustHit()) {
166 targetAnimation.Start(*this);
168 if (moveAnimation.Valid() && !moveAnimation.Finished()) return;
169 if (targetAnimation.Valid() && !targetAnimation.Finished()) return;
170 if (moveAnimation.Valid() || targetAnimation.Valid()) {
171 moveAnimation.Clear();
172 targetAnimation.Clear();
173 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
177 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
178 i->CheckTimers(*this);
183 bool PerformAttacks::HasAnimationsRunning() const {
184 if (titleBarTimer.Running()) return true;
185 if (moveAnimation.Valid() && moveAnimation.Running()) return true;
186 if (targetAnimation.Valid() && targetAnimation.Running()) return true;
187 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
188 if (i->Running()) return true;
193 void PerformAttacks::ResetAnimation() {
194 if (moveAnimation.Valid()) {
195 moveAnimation.Clear();
196 if (!battle->CurrentAttack().isMonster) {
197 battle->HeroAt(battle->CurrentAttack().index).GetAnimation().Clear();
200 if (targetAnimation.Valid()) {
201 targetAnimation.Clear();
203 titleBarTimer.Clear();
204 numberAnimation.clear();
205 numberPosition.clear();
209 void PerformAttacks::UpdateWorld(float deltaT) {
213 void PerformAttacks::Render(SDL_Surface *screen) {
214 Vector<int> offset(battle->CalculateScreenOffset(screen));
215 battle->RenderBackground(screen, offset);
216 battle->RenderMonsters(screen, offset);
217 battle->RenderHeroes(screen, offset);
218 battle->RenderSmallHeroTags(screen, offset);
219 RenderTitleBar(screen, offset);
220 RenderNumbers(screen, offset);
221 RenderTargetAnimation(screen, offset);
224 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
225 if (!titleBarText || !titleBarTimer.Running()) return;
227 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
228 battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->Width(), height);
230 Point<int> textPosition(
231 (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
232 battle->Res().titleFrame->BorderHeight());
233 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
236 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
237 for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
238 if (numberAnimation[i].Running()) {
239 Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
240 numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
245 void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector<int> &offset) const {
246 if (!targetAnimation.Valid() || !targetAnimation.Running()) return;
247 const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection());
248 const vector<Point<int> > &positions(ts.TargetsHeroes() ? battle->HeroesPositions() : battle->MonsterPositions());
249 for (vector<Point<int> >::size_type i(0), end(positions.size()); i < end; ++i) {
250 if (ts.IsSelected(i)) {
251 targetAnimation.DrawCenter(screen, positions[i] + offset);