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;
34 void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) {
36 battle->CalculateAttackOrder();
37 numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
38 numberPosition.reserve(numberAnimation.size());
41 void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) {
42 battle->ClearAllAttacks();
46 void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) {
50 void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) {
55 void PerformAttacks::Resize(int width, int height) {
60 void PerformAttacks::HandleEvents(const Input &input) {
62 if (HasAnimationsRunning()) return;
64 battle->ApplyDamage();
66 if (battle->AttacksFinished()) {
71 battle->CalculateDamage();
73 if (battle->CurrentAttack().isMonster) {
74 Monster &monster(battle->MonsterAt(battle->CurrentAttack().index));
75 titleBarText = monster.Name();
76 moveAnimation = monster.AttackAnimation();
78 Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
79 const AttackChoice &ac(battle->AttackChoiceAt(battle->CurrentAttack().index));
81 switch (ac.GetType()) {
82 case AttackChoice::SWORD:
83 if (hero.HasWeapon()) {
84 titleBarText = hero.Weapon()->Name();
85 targetAnimation = hero.Weapon()->AttackAnimation();
87 titleBarText = "Melee attack!";
88 targetAnimation = hero.MeleeAnimation();
90 moveAnimation = hero.AttackAnimation();
92 if (ac.Selection().TargetsEnemies()) {
93 for (int i(0); i < battle->MaxMonsters(); ++i) {
94 if (ac.Selection().IsSelected(i)) {
95 numberAnimation.push_back(NumberAnimation(ac.Selection().GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
96 numberPosition.push_back(
97 battle->MonsterPositions()[i]);
101 for (int i(0); i < battle->NumHeroes(); ++i) {
102 if (ac.Selection().IsSelected(i)) {
103 numberAnimation.push_back(NumberAnimation(ac.Selection().GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
104 numberPosition.push_back(
105 battle->HeroesPositions()[i]);
110 case AttackChoice::MAGIC:
111 titleBarText = ac.GetSpell()->Name();
112 moveAnimation = hero.SpellAnimation();
114 case AttackChoice::DEFEND:
115 titleBarText = "Defends.";
118 case AttackChoice::IKARI:
119 if (ac.GetItem()->HasIkari()) {
120 titleBarText = ac.GetItem()->GetIkari()->Name();
121 if (ac.GetItem()->GetIkari()->IsMagical()) {
122 moveAnimation = hero.SpellAnimation();
124 moveAnimation = hero.AttackAnimation();
128 case AttackChoice::ITEM:
129 titleBarText = ac.GetItem()->Name();
132 case AttackChoice::UNDECIDED:
133 titleBarText = "UNDECIDED";
139 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(1500);
140 if (moveAnimation) moveAnimation->Start(*this);
141 if (targetAnimation) {
142 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
144 targetAnimationTimer.Clear();
148 void PerformAttacks::CheckAnimations() {
149 if (targetAnimation && targetAnimationTimer.JustHit()) {
150 targetAnimation->Start(*this);
152 if (moveAnimation && !moveAnimation->Finished()) return;
153 if (targetAnimation && !targetAnimation->Finished()) return;
154 if (moveAnimation || targetAnimation) {
157 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
161 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
162 i->CheckTimers(*this);
167 bool PerformAttacks::HasAnimationsRunning() const {
168 if (titleBarTimer.Running()) return true;
169 if (moveAnimation && moveAnimation->Running()) return true;
170 if (targetAnimation && targetAnimation->Running()) return true;
171 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
172 if (i->Running()) return true;
177 void PerformAttacks::ResetAnimation() {
179 moveAnimation->Stop();
182 if (targetAnimation) {
183 targetAnimation->Stop();
186 titleBarTimer.Clear();
187 numberAnimation.clear();
188 numberPosition.clear();
192 void PerformAttacks::UpdateWorld(float deltaT) {
196 void PerformAttacks::Render(SDL_Surface *screen) {
197 Vector<int> offset(battle->CalculateScreenOffset(screen));
198 battle->RenderBackground(screen, offset);
199 battle->RenderMonsters(screen, offset);
200 battle->RenderHeroes(screen, offset);
201 battle->RenderSmallHeroTags(screen, offset);
202 RenderTitleBar(screen, offset);
203 RenderNumbers(screen, offset);
204 RenderTargetAnimation(screen, offset);
207 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
208 if (!titleBarText || !titleBarTimer.Running()) return;
210 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
211 battle->Res().titleFrame->Draw(screen, Point<int>(offset.X(), offset.Y()), battle->Width(), height);
213 Point<int> textPosition(
214 (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
215 battle->Res().titleFrame->BorderHeight());
216 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
219 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
220 for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
221 if (numberAnimation[i].Running()) {
222 Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
223 numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
228 void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector<int> &offset) const {
229 if (!targetAnimation || !targetAnimation->Running()) return;
230 if (battle->CurrentAttack().isMonster) return; // no monsters for now
231 const TargetSelection &ts(battle->AttackChoiceAt(battle->CurrentAttack().index).Selection());
232 const vector<Point<int> > &positions(ts.TargetsHeroes() ? battle->HeroesPositions() : battle->MonsterPositions());
233 for (vector<Point<int> >::size_type i(0), end(positions.size()); i < end; ++i) {
234 if (ts.IsSelected(i)) {
235 targetAnimation->DrawCenter(screen, positions[i] + offset);