1 #include "PerformAttacks.h"
3 #include "../BattleState.h"
5 #include "../Monster.h"
6 #include "../TargetSelection.h"
7 #include "../../app/Application.h"
8 #include "../../app/Input.h"
9 #include "../../common/Ikari.h"
10 #include "../../common/Item.h"
11 #include "../../common/Spell.h"
12 #include "../../graphics/Animation.h"
13 #include "../../graphics/Font.h"
14 #include "../../graphics/Frame.h"
18 using app::Application;
21 using graphics::AnimationRunner;
26 void PerformAttacks::OnEnterState(SDL_Surface *screen) {
27 battle->CalculateAttackOrder();
28 numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1);
29 numberPosition.reserve(numberAnimation.size());
32 void PerformAttacks::OnExitState(SDL_Surface *screen) {
33 battle->ClearAllAttacks();
36 void PerformAttacks::OnResumeState(SDL_Surface *screen) {
40 void PerformAttacks::OnPauseState(SDL_Surface *screen) {
45 void PerformAttacks::OnResize(int width, int height) {
50 void PerformAttacks::HandleEvents(const Input &input) {
52 if (HasAnimationsRunning()) return;
54 battle->ApplyDamage();
56 if (battle->AttacksFinished()) {
61 battle->CalculateDamage();
63 if (battle->CurrentAttack().IsMonster()) {
64 Monster &monster(battle->MonsterAt(battle->CurrentAttack().index));
65 titleBarText = monster.Name();
66 targetAnimation = AnimationRunner(monster.MeleeAnimation());
67 moveAnimation = AnimationRunner(monster.AttackAnimation());
68 monster.SetAnimation(moveAnimation);
69 AddNumberAnimations(battle->MonsterAt(battle->CurrentAttack().index).GetAttackChoice().Selection());
70 } else if (battle->CurrentAttack().IsCapsule()) {
71 Capsule &capsule(battle->GetCapsule());
72 titleBarText = capsule.Name();
73 targetAnimation = AnimationRunner(capsule.MeleeAnimation());
74 moveAnimation = AnimationRunner(capsule.AttackAnimation());
75 capsule.SetAnimation(moveAnimation);
76 AddNumberAnimations(capsule.GetAttackChoice().Selection());
78 Hero &hero(battle->HeroAt(battle->CurrentAttack().index));
79 const AttackChoice &ac(battle->HeroAt(battle->CurrentAttack().index).GetAttackChoice());
81 switch (ac.GetType()) {
82 case AttackChoice::SWORD:
83 if (hero.HasWeapon()) {
84 titleBarText = hero.Weapon()->Name();
85 targetAnimation = AnimationRunner(hero.Weapon()->AttackAnimation());
87 titleBarText = "Melee attack!";
88 targetAnimation = AnimationRunner(hero.MeleeAnimation());
90 moveAnimation = AnimationRunner(hero.AttackAnimation());
91 AddNumberAnimations(ac.Selection());
93 case AttackChoice::MAGIC:
94 titleBarText = ac.GetSpell()->Name();
95 moveAnimation = AnimationRunner(hero.SpellAnimation());
97 case AttackChoice::DEFEND:
98 titleBarText = "Defends.";
99 moveAnimation.Clear();
101 case AttackChoice::IKARI:
102 if (ac.GetItem()->HasIkari()) {
103 titleBarText = ac.GetItem()->GetIkari()->Name();
104 if (ac.GetItem()->GetIkari()->IsMagical()) {
105 moveAnimation = AnimationRunner(hero.SpellAnimation());
107 moveAnimation = AnimationRunner(hero.AttackAnimation());
111 case AttackChoice::ITEM:
112 titleBarText = ac.GetItem()->Name();
113 moveAnimation.Clear();
115 case AttackChoice::UNDECIDED:
116 titleBarText = "UNDECIDED";
117 moveAnimation.Clear();
122 if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850);
123 if (moveAnimation.Valid()) {
124 moveAnimation.Start(*this);
125 if (battle->CurrentAttack().IsMonster()) {
126 battle->MonsterAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
127 } else if (battle->CurrentAttack().IsHero()) {
128 battle->HeroAt(battle->CurrentAttack().index).SetAnimation(moveAnimation);
130 battle->GetCapsule().SetAnimation(moveAnimation);
133 if (targetAnimation.Valid()) {
134 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
136 targetAnimationTimer.Clear();
140 void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) {
141 if (ts.TargetsMonsters()) {
142 for (int i(0); i < battle->MaxMonsters(); ++i) {
144 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
145 numberPosition.push_back(
146 battle->MonsterAt(i).Position());
147 } else if (ts.IsGood(i)) {
148 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
149 numberPosition.push_back(
150 battle->MonsterAt(i).Position());
154 for (int i(0); i < battle->NumHeroes(); ++i) {
156 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite));
157 numberPosition.push_back(
158 battle->HeroAt(i).Position());
159 } else if (ts.IsGood(i)) {
160 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite));
161 numberPosition.push_back(
162 battle->HeroAt(i).Position());
168 void PerformAttacks::CheckAnimations() {
169 if (targetAnimation.Valid() && targetAnimationTimer.JustHit()) {
170 targetAnimation.Start(*this);
172 if (moveAnimation.Valid() && !moveAnimation.Finished()) return;
173 if (targetAnimation.Valid() && !targetAnimation.Finished()) return;
174 if (moveAnimation.Valid() || targetAnimation.Valid()) {
175 moveAnimation.Clear();
176 targetAnimation.Clear();
177 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
181 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
182 i->CheckTimers(*this);
187 bool PerformAttacks::HasAnimationsRunning() const {
188 if (titleBarTimer.Running()) return true;
189 if (moveAnimation.Valid() && moveAnimation.Running()) return true;
190 if (targetAnimation.Valid() && targetAnimation.Running()) return true;
191 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
192 if (i->Running()) return true;
197 void PerformAttacks::ResetAnimation() {
198 if (moveAnimation.Valid()) {
199 moveAnimation.Clear();
200 if (!battle->CurrentAttack().IsMonster()) {
201 battle->HeroAt(battle->CurrentAttack().index).GetAnimation().Clear();
204 if (targetAnimation.Valid()) {
205 targetAnimation.Clear();
207 titleBarTimer.Clear();
208 numberAnimation.clear();
209 numberPosition.clear();
213 void PerformAttacks::UpdateWorld(Uint32 deltaT) {
217 void PerformAttacks::Render(SDL_Surface *screen) {
218 Vector<int> offset(battle->CalculateScreenOffset(screen));
219 battle->RenderBackground(screen, offset);
220 battle->RenderMonsters(screen, offset);
221 battle->RenderHeroes(screen, offset);
222 battle->RenderCapsule(screen, offset);
223 battle->RenderSmallHeroTags(screen, offset);
224 RenderTitleBar(screen, offset);
225 RenderNumbers(screen, offset);
226 RenderTargetAnimation(screen, offset);
229 void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector<int> &offset) const {
230 if (!titleBarText || !titleBarTimer.Running()) return;
232 int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight());
233 battle->Res().titleFrame->Draw(screen, offset, battle->Width(), height);
235 Vector<int> textPosition(
236 (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2,
237 battle->Res().titleFrame->BorderHeight());
238 battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset);
241 void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector<int> &offset) const {
242 for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
243 if (numberAnimation[i].Running()) {
244 Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
245 numberAnimation[i].Draw(screen, numberPosition[i] + align + offset);
250 void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const math::Vector<int> &offset) const {
251 if (!targetAnimation.Valid() || !targetAnimation.Running()) return;
252 const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection());
253 if (ts.TargetsHeroes()) {
254 for (vector<Vector<int> >::size_type i(0), end(battle->NumHeroes()); i < end; ++i) {
255 if (ts.IsSelected(i)) {
256 targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + offset);
260 for (vector<Vector<int> >::size_type i(0), end(battle->MaxMonsters()); i < end; ++i) {
261 if (ts.IsSelected(i)) {
262 targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + offset);