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 PerformAttacks::PerformAttacks(Battle *battle, BattleState *parent)
37 void PerformAttacks::OnEnterState(SDL_Surface *screen) {
38 battle->CalculateAttackOrder();
39 numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1
40 ? battle->MaxMonsters()
41 : battle->NumHeroes() + 1);
42 numberPosition.reserve(numberAnimation.size());
43 OnResize(screen->w, screen->h);
46 void PerformAttacks::OnExitState(SDL_Surface *screen) {
47 battle->ClearAllAttacks();
50 void PerformAttacks::OnResumeState(SDL_Surface *screen) {
54 void PerformAttacks::OnPauseState(SDL_Surface *screen) {
59 void PerformAttacks::OnResize(int width, int height) {
60 const Resources &res = parent->Res();
61 framePosition = parent->ScreenOffset();
62 frameSize = Vector<int>(
64 res.titleFrame->BorderHeight() * 2 + res.titleFont->CharHeight());
68 void PerformAttacks::HandleEvents(const Input &input) {
70 if (HasAnimationsRunning()) return;
72 battle->ApplyDamage();
74 if (battle->AttacksFinished()) {
79 battle->CalculateDamage();
80 const Battle::Order &attack = battle->CurrentAttack();
82 if (attack.IsMonster()) {
83 Monster &monster(battle->MonsterAt(attack.index));
84 titleBarText = monster.Name();
85 targetAnimation = AnimationRunner(monster.MeleeAnimation());
86 moveAnimation = AnimationRunner(monster.AttackAnimation());
87 monster.SetAnimation(moveAnimation);
88 AddNumberAnimations(monster.GetAttackChoice().Selection());
89 } else if (attack.IsCapsule()) {
90 Capsule &capsule(battle->GetCapsule());
91 titleBarText = capsule.Name();
92 targetAnimation = AnimationRunner(capsule.MeleeAnimation());
93 moveAnimation = AnimationRunner(capsule.AttackAnimation());
94 capsule.SetAnimation(moveAnimation);
95 AddNumberAnimations(capsule.GetAttackChoice().Selection());
97 Hero &hero(battle->HeroAt(attack.index));
98 const AttackChoice &ac(hero.GetAttackChoice());
100 switch (ac.GetType()) {
101 case AttackChoice::SWORD:
102 if (hero.HasWeapon()) {
103 titleBarText = hero.Weapon()->Name();
104 targetAnimation = AnimationRunner(hero.Weapon()->AttackAnimation());
106 titleBarText = "Melee attack!";
107 targetAnimation = AnimationRunner(hero.MeleeAnimation());
109 moveAnimation = AnimationRunner(hero.AttackAnimation());
110 AddNumberAnimations(ac.Selection());
112 case AttackChoice::MAGIC:
113 titleBarText = ac.GetSpell()->Name();
114 moveAnimation = AnimationRunner(hero.SpellAnimation());
116 case AttackChoice::DEFEND:
117 titleBarText = "Defends.";
118 moveAnimation.Clear();
120 case AttackChoice::IKARI:
121 if (ac.GetItem()->HasIkari()) {
122 titleBarText = ac.GetItem()->GetIkari()->Name();
123 if (ac.GetItem()->GetIkari()->IsMagical()) {
124 moveAnimation = AnimationRunner(hero.SpellAnimation());
126 moveAnimation = AnimationRunner(hero.AttackAnimation());
130 case AttackChoice::ITEM:
131 titleBarText = ac.GetItem()->Name();
132 moveAnimation.Clear();
134 case AttackChoice::UNDECIDED:
135 titleBarText = "UNDECIDED";
136 moveAnimation.Clear();
142 titleBarTimer = GraphicsTimers().StartCountdown(850);
143 textPosition = parent->ScreenOffset() + Vector<int>(
144 (parent->Width() - std::strlen(titleBarText) * parent->Res().titleFont->CharWidth()) / 2,
145 parent->Res().titleFrame->BorderHeight());
147 if (moveAnimation.Valid()) {
148 moveAnimation.Start(*this);
149 if (attack.IsMonster()) {
150 battle->MonsterAt(attack.index).SetAnimation(moveAnimation);
151 } else if (attack.IsHero()) {
152 battle->HeroAt(attack.index).SetAnimation(moveAnimation);
154 battle->GetCapsule().SetAnimation(moveAnimation);
157 if (targetAnimation.Valid()) {
158 targetAnimationTimer = GraphicsTimers().StartCountdown(150);
160 targetAnimationTimer.Clear();
164 void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) {
165 if (ts.TargetsMonsters()) {
166 for (int i(0); i < battle->MaxMonsters(); ++i) {
168 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), parent->Res().numberAnimationPrototype, parent->Res().bigNumberSprite));
169 numberPosition.push_back(
170 battle->MonsterAt(i).Position() + parent->ScreenOffset());
171 } else if (ts.IsGood(i)) {
172 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), parent->Res().numberAnimationPrototype, parent->Res().greenNumberSprite));
173 numberPosition.push_back(
174 battle->MonsterAt(i).Position() + parent->ScreenOffset());
178 for (int i(0); i < battle->NumHeroes(); ++i) {
180 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), parent->Res().numberAnimationPrototype, parent->Res().bigNumberSprite));
181 numberPosition.push_back(
182 battle->HeroAt(i).Position() + parent->ScreenOffset());
183 } else if (ts.IsGood(i)) {
184 numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), parent->Res().numberAnimationPrototype, parent->Res().greenNumberSprite));
185 numberPosition.push_back(
186 battle->HeroAt(i).Position() + parent->ScreenOffset());
192 void PerformAttacks::CheckAnimations() {
193 if (targetAnimation.Valid() && targetAnimationTimer.JustHit()) {
194 targetAnimation.Start(*this);
196 if (moveAnimation.Valid() && !moveAnimation.Finished()) return;
197 if (targetAnimation.Valid() && !targetAnimation.Finished()) return;
198 if (moveAnimation.Valid() || targetAnimation.Valid()) {
199 moveAnimation.Clear();
200 targetAnimation.Clear();
201 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
205 for (vector<NumberAnimation>::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
206 i->CheckTimers(*this);
211 bool PerformAttacks::HasAnimationsRunning() const {
212 if (titleBarTimer.Running()) return true;
213 if (moveAnimation.Valid() && moveAnimation.Running()) return true;
214 if (targetAnimation.Valid() && targetAnimation.Running()) return true;
215 for (vector<NumberAnimation>::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) {
216 if (i->Running()) return true;
221 void PerformAttacks::ResetAnimation() {
222 if (moveAnimation.Valid()) {
223 moveAnimation.Clear();
224 if (!battle->CurrentAttack().IsMonster()) {
225 battle->HeroAt(battle->CurrentAttack().index).GetAnimation().Clear();
228 if (targetAnimation.Valid()) {
229 targetAnimation.Clear();
231 titleBarTimer.Clear();
232 numberAnimation.clear();
233 numberPosition.clear();
237 void PerformAttacks::UpdateWorld(Uint32 deltaT) {
241 void PerformAttacks::Render(SDL_Surface *screen) {
242 parent->RenderBackground(screen);
243 parent->RenderMonsters(screen);
244 parent->RenderHeroes(screen);
245 parent->RenderCapsule(screen);
246 parent->RenderSmallHeroTags(screen);
247 RenderTitleBar(screen);
248 RenderNumbers(screen);
249 RenderTargetAnimation(screen);
252 void PerformAttacks::RenderTitleBar(SDL_Surface *screen) const {
253 if (!titleBarText || !titleBarTimer.Running()) return;
255 parent->Res().titleFrame->Draw(screen, framePosition, frameSize.X(), frameSize.Y());
257 parent->Res().titleFont->DrawString(titleBarText, screen, textPosition);
260 void PerformAttacks::RenderNumbers(SDL_Surface *screen) const {
261 for (vector<NumberAnimation>::size_type i(0), end(numberAnimation.size()); i < end; ++i) {
262 if (numberAnimation[i].Running()) {
263 Vector<int> align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4);
264 numberAnimation[i].Draw(screen, numberPosition[i] + align);
269 void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen) const {
270 if (!targetAnimation.Valid() || !targetAnimation.Running()) return;
271 const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection());
272 if (ts.TargetsHeroes()) {
273 for (vector<Vector<int> >::size_type i(0), end(battle->NumHeroes()); i < end; ++i) {
274 if (ts.IsSelected(i)) {
275 targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + parent->ScreenOffset());
279 for (vector<Vector<int> >::size_type i(0), end(battle->MaxMonsters()); i < end; ++i) {
280 if (ts.IsSelected(i)) {
281 targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + parent->ScreenOffset());