X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fbattle%2Fstates%2FPerformAttacks.cpp;h=960eb6cf51fc5a0e8d12672844047f09f650e13b;hb=b7b7078d04007d5b86ab160288f88b57d76ea408;hp=36b7b1630d41477fa23ba7055ed629ee39078a8c;hpb=4c3d420d8fc933d334def034f5d76ba23934479d;p=l2e.git diff --git a/src/battle/states/PerformAttacks.cpp b/src/battle/states/PerformAttacks.cpp index 36b7b16..960eb6c 100644 --- a/src/battle/states/PerformAttacks.cpp +++ b/src/battle/states/PerformAttacks.cpp @@ -1,22 +1,14 @@ -/* - * PerformAttacks.cpp - * - * Created on: Aug 10, 2012 - * Author: holy - */ - #include "PerformAttacks.h" #include "../BattleState.h" #include "../Hero.h" #include "../Monster.h" +#include "../TargetSelection.h" #include "../../app/Application.h" #include "../../app/Input.h" #include "../../common/Ikari.h" #include "../../common/Item.h" #include "../../common/Spell.h" -#include "../../geometry/operators.h" -#include "../../geometry/Point.h" #include "../../graphics/Animation.h" #include "../../graphics/Font.h" #include "../../graphics/Frame.h" @@ -25,35 +17,51 @@ using app::Application; using app::Input; -using geometry::Point; -using geometry::Vector; +using math::Vector; +using graphics::AnimationRunner; using std::vector; namespace battle { -void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; +PerformAttacks::PerformAttacks(Battle *battle, BattleState *parent) +: battle(battle) +, parent(parent) +, moveAnimation(0) +, targetAnimation(0) +, titleBarText(0) +, cursor(-1) { + +} + + +void PerformAttacks::OnEnterState(SDL_Surface *screen) { battle->CalculateAttackOrder(); - numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1); + numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 + ? battle->MaxMonsters() + : battle->NumHeroes() + 1); numberPosition.reserve(numberAnimation.size()); + OnResize(screen->w, screen->h); } -void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) { +void PerformAttacks::OnExitState(SDL_Surface *screen) { battle->ClearAllAttacks(); - ctrl = 0; } -void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) { +void PerformAttacks::OnResumeState(SDL_Surface *screen) { } -void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) { +void PerformAttacks::OnPauseState(SDL_Surface *screen) { } -void PerformAttacks::Resize(int width, int height) { - +void PerformAttacks::OnResize(int width, int height) { + const Resources &res = parent->Res(); + framePosition = parent->ScreenOffset(); + frameSize = Vector( + parent->Width(), + res.titleFrame->BorderHeight() * 2 + res.titleFont->CharHeight()); } @@ -64,66 +72,89 @@ void PerformAttacks::HandleEvents(const Input &input) { battle->ApplyDamage(); battle->NextAttack(); if (battle->AttacksFinished()) { - ctrl->PopState(); + Ctrl().PopState(); return; } battle->CalculateDamage(); + const Battle::Order &attack = battle->CurrentAttack(); - if (battle->CurrentAttack().isMonster) { - Monster &monster(battle->MonsterAt(battle->CurrentAttack().index)); + if (attack.IsMonster()) { + Monster &monster(battle->MonsterAt(attack.index)); titleBarText = monster.Name(); - moveAnimation = monster.AttackAnimation(); - targetAnimation = monster.MeleeAnimation(); - AddNumberAnimations(battle->MonsterAttackChoiceAt(battle->CurrentAttack().index).Selection()); + targetAnimation = AnimationRunner(monster.MeleeAnimation()); + moveAnimation = AnimationRunner(monster.AttackAnimation()); + monster.SetAnimation(moveAnimation); + AddNumberAnimations(monster.GetAttackChoice().Selection()); + } else if (attack.IsCapsule()) { + Capsule &capsule(battle->GetCapsule()); + titleBarText = capsule.Name(); + targetAnimation = AnimationRunner(capsule.MeleeAnimation()); + moveAnimation = AnimationRunner(capsule.AttackAnimation()); + capsule.SetAnimation(moveAnimation); + AddNumberAnimations(capsule.GetAttackChoice().Selection()); } else { - Hero &hero(battle->HeroAt(battle->CurrentAttack().index)); - const AttackChoice &ac(battle->AttackChoiceAt(battle->CurrentAttack().index)); + Hero &hero(battle->HeroAt(attack.index)); + const AttackChoice &ac(hero.GetAttackChoice()); switch (ac.GetType()) { case AttackChoice::SWORD: if (hero.HasWeapon()) { titleBarText = hero.Weapon()->Name(); - targetAnimation = hero.Weapon()->AttackAnimation(); + targetAnimation = AnimationRunner(hero.Weapon()->AttackAnimation()); } else { titleBarText = "Melee attack!"; - targetAnimation = hero.MeleeAnimation(); + targetAnimation = AnimationRunner(hero.MeleeAnimation()); } - moveAnimation = hero.AttackAnimation(); + moveAnimation = AnimationRunner(hero.AttackAnimation()); AddNumberAnimations(ac.Selection()); break; case AttackChoice::MAGIC: titleBarText = ac.GetSpell()->Name(); - moveAnimation = hero.SpellAnimation(); + moveAnimation = AnimationRunner(hero.SpellAnimation()); break; case AttackChoice::DEFEND: titleBarText = "Defends."; - moveAnimation = 0; + moveAnimation.Clear(); break; case AttackChoice::IKARI: if (ac.GetItem()->HasIkari()) { titleBarText = ac.GetItem()->GetIkari()->Name(); if (ac.GetItem()->GetIkari()->IsMagical()) { - moveAnimation = hero.SpellAnimation(); + moveAnimation = AnimationRunner(hero.SpellAnimation()); } else { - moveAnimation = hero.AttackAnimation(); + moveAnimation = AnimationRunner(hero.AttackAnimation()); } } break; case AttackChoice::ITEM: titleBarText = ac.GetItem()->Name(); - moveAnimation = 0; + moveAnimation.Clear(); break; case AttackChoice::UNDECIDED: titleBarText = "UNDECIDED"; - moveAnimation = 0; + moveAnimation.Clear(); break; } } - if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850); - if (moveAnimation) moveAnimation->Start(*this); - if (targetAnimation) { + if (titleBarText) { + titleBarTimer = GraphicsTimers().StartCountdown(850); + textPosition = parent->ScreenOffset() + Vector( + (parent->Width() - std::strlen(titleBarText) * parent->Res().titleFont->CharWidth()) / 2, + parent->Res().titleFrame->BorderHeight()); + } + if (moveAnimation.Valid()) { + moveAnimation.Start(*this); + if (attack.IsMonster()) { + battle->MonsterAt(attack.index).SetAnimation(moveAnimation); + } else if (attack.IsHero()) { + battle->HeroAt(attack.index).SetAnimation(moveAnimation); + } else { + battle->GetCapsule().SetAnimation(moveAnimation); + } + } + if (targetAnimation.Valid()) { targetAnimationTimer = GraphicsTimers().StartCountdown(150); } else { targetAnimationTimer.Clear(); @@ -131,42 +162,42 @@ void PerformAttacks::HandleEvents(const Input &input) { } void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) { - if (ts.TargetsEnemies()) { + if (ts.TargetsMonsters()) { for (int i(0); i < battle->MaxMonsters(); ++i) { if (ts.IsBad(i)) { - numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite)); + numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), parent->Res().numberAnimationPrototype, parent->Res().bigNumberSprite)); numberPosition.push_back( - battle->MonsterPositions()[i]); + battle->MonsterAt(i).Position() + parent->ScreenOffset()); } else if (ts.IsGood(i)) { - numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite)); + numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), parent->Res().numberAnimationPrototype, parent->Res().greenNumberSprite)); numberPosition.push_back( - battle->MonsterPositions()[i]); + battle->MonsterAt(i).Position() + parent->ScreenOffset()); } } } else { for (int i(0); i < battle->NumHeroes(); ++i) { if (ts.IsBad(i)) { - numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite)); + numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), parent->Res().numberAnimationPrototype, parent->Res().bigNumberSprite)); numberPosition.push_back( - battle->HeroesPositions()[i]); + battle->HeroAt(i).Position() + parent->ScreenOffset()); } else if (ts.IsGood(i)) { - numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite)); + numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), parent->Res().numberAnimationPrototype, parent->Res().greenNumberSprite)); numberPosition.push_back( - battle->HeroesPositions()[i]); + battle->HeroAt(i).Position() + parent->ScreenOffset()); } } } } void PerformAttacks::CheckAnimations() { - if (targetAnimation && targetAnimationTimer.JustHit()) { - targetAnimation->Start(*this); + if (targetAnimation.Valid() && targetAnimationTimer.JustHit()) { + targetAnimation.Start(*this); } - if (moveAnimation && !moveAnimation->Finished()) return; - if (targetAnimation && !targetAnimation->Finished()) return; - if (moveAnimation || targetAnimation) { - moveAnimation = 0; - targetAnimation = 0; + if (moveAnimation.Valid() && !moveAnimation.Finished()) return; + if (targetAnimation.Valid() && !targetAnimation.Finished()) return; + if (moveAnimation.Valid() || targetAnimation.Valid()) { + moveAnimation.Clear(); + targetAnimation.Clear(); for (vector::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) { i->Start(*this); } @@ -179,8 +210,8 @@ void PerformAttacks::CheckAnimations() { bool PerformAttacks::HasAnimationsRunning() const { if (titleBarTimer.Running()) return true; - if (moveAnimation && moveAnimation->Running()) return true; - if (targetAnimation && targetAnimation->Running()) return true; + if (moveAnimation.Valid() && moveAnimation.Running()) return true; + if (targetAnimation.Valid() && targetAnimation.Running()) return true; for (vector::const_iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) { if (i->Running()) return true; } @@ -188,13 +219,14 @@ bool PerformAttacks::HasAnimationsRunning() const { } void PerformAttacks::ResetAnimation() { - if (moveAnimation) { - moveAnimation->Stop(); - moveAnimation = 0; + if (moveAnimation.Valid()) { + moveAnimation.Clear(); + if (!battle->CurrentAttack().IsMonster()) { + battle->HeroAt(battle->CurrentAttack().index).GetAnimation().Clear(); + } } - if (targetAnimation) { - targetAnimation->Stop(); - targetAnimation = 0; + if (targetAnimation.Valid()) { + targetAnimation.Clear(); } titleBarTimer.Clear(); numberAnimation.clear(); @@ -202,49 +234,52 @@ void PerformAttacks::ResetAnimation() { } -void PerformAttacks::UpdateWorld(float deltaT) { +void PerformAttacks::UpdateWorld(Uint32 deltaT) { } void PerformAttacks::Render(SDL_Surface *screen) { - Vector offset(battle->CalculateScreenOffset(screen)); - battle->RenderBackground(screen, offset); - battle->RenderMonsters(screen, offset); - battle->RenderHeroes(screen, offset); - battle->RenderSmallHeroTags(screen, offset); - RenderTitleBar(screen, offset); - RenderNumbers(screen, offset); - RenderTargetAnimation(screen, offset); + parent->RenderBackground(screen); + parent->RenderMonsters(screen); + parent->RenderHeroes(screen); + parent->RenderCapsule(screen); + parent->RenderSmallHeroTags(screen); + RenderTitleBar(screen); + RenderNumbers(screen); + RenderTargetAnimation(screen); } -void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector &offset) const { +void PerformAttacks::RenderTitleBar(SDL_Surface *screen) const { if (!titleBarText || !titleBarTimer.Running()) return; - int height(battle->Res().titleFrame->BorderHeight() * 2 + battle->Res().titleFont->CharHeight()); - battle->Res().titleFrame->Draw(screen, Point(offset.X(), offset.Y()), battle->Width(), height); + parent->Res().titleFrame->Draw(screen, framePosition, frameSize.X(), frameSize.Y()); - Point textPosition( - (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2, - battle->Res().titleFrame->BorderHeight()); - battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset); + parent->Res().titleFont->DrawString(titleBarText, screen, textPosition); } -void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector &offset) const { +void PerformAttacks::RenderNumbers(SDL_Surface *screen) const { for (vector::size_type i(0), end(numberAnimation.size()); i < end; ++i) { if (numberAnimation[i].Running()) { Vector align(numberAnimation[i].Width() / -2, numberAnimation[i].Height() * -3 / 4); - numberAnimation[i].Draw(screen, numberPosition[i] + align + offset); + numberAnimation[i].Draw(screen, numberPosition[i] + align); } } } -void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector &offset) const { - if (!targetAnimation || !targetAnimation->Running()) return; +void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen) const { + if (!targetAnimation.Valid() || !targetAnimation.Running()) return; const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection()); - const vector > &positions(ts.TargetsHeroes() ? battle->HeroesPositions() : battle->MonsterPositions()); - for (vector >::size_type i(0), end(positions.size()); i < end; ++i) { - if (ts.IsSelected(i)) { - targetAnimation->DrawCenter(screen, positions[i] + offset); + if (ts.TargetsHeroes()) { + for (vector >::size_type i(0), end(battle->NumHeroes()); i < end; ++i) { + if (ts.IsSelected(i)) { + targetAnimation.DrawCenter(screen, battle->HeroAt(i).Position() + parent->ScreenOffset()); + } + } + } else { + for (vector >::size_type i(0), end(battle->MaxMonsters()); i < end; ++i) { + if (ts.IsSelected(i)) { + targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + parent->ScreenOffset()); + } } } }