X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fbattle%2Fstates%2FPerformAttacks.cpp;h=92e5185cf70488a5ce8baf9a330011ef7ba51934;hb=0b11a24a8b08c49d6e4301573602fb6d01e7a8c8;hp=6c318aaf04138e86c627a94429dcdec23e260911;hpb=8456b7dac2051bfd2b507a39854c1428eb4d91cd;p=l2e.git diff --git a/src/battle/states/PerformAttacks.cpp b/src/battle/states/PerformAttacks.cpp index 6c318aa..92e5185 100644 --- a/src/battle/states/PerformAttacks.cpp +++ b/src/battle/states/PerformAttacks.cpp @@ -1,23 +1,14 @@ -/* - * PerformAttacks.cpp - * - * Created on: Aug 10, 2012 - * Author: holy - */ - #include "PerformAttacks.h" -#include "../AttackAnimation.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" @@ -26,121 +17,173 @@ 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; - battle->WriteOrder(order); +void PerformAttacks::OnEnterState(SDL_Surface *screen) { + battle->CalculateAttackOrder(); 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) { - ctrl = 0; +void PerformAttacks::OnExitState(SDL_Surface *screen) { + battle->ClearAllAttacks(); } -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 = battle->Res(); + framePosition = battle->ScreenOffset(); + frameSize = Vector( + battle->Width(), + res.titleFrame->BorderHeight() * 2 + res.titleFont->CharHeight()); } void PerformAttacks::HandleEvents(const Input &input) { - if (attackAnimation) attackAnimation->Update(); - CheckNumberAnimation(); + CheckAnimations(); if (HasAnimationsRunning()) return; ResetAnimation(); - AdvanceCursor(); - if (Finished()) { - battle->ClearAllAttacks(); - ctrl->PopState(); + battle->ApplyDamage(); + battle->NextAttack(); + if (battle->AttacksFinished()) { + Ctrl().PopState(); return; } - if (order[cursor].isMonster) { - const Monster &monster(battle->MonsterAt(order[cursor].index)); + battle->CalculateDamage(); + + if (battle->CurrentAttack().IsMonster()) { + Monster &monster(battle->MonsterAt(battle->CurrentAttack().index)); titleBarText = monster.Name(); - moveAnimation = 0; + targetAnimation = AnimationRunner(monster.MeleeAnimation()); + moveAnimation = AnimationRunner(monster.AttackAnimation()); + monster.SetAnimation(moveAnimation); + AddNumberAnimations(battle->MonsterAt(battle->CurrentAttack().index).GetAttackChoice().Selection()); + } else if (battle->CurrentAttack().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(order[cursor].index)); - const AttackChoice &ac(battle->AttackChoiceAt(order[cursor].index)); + Hero &hero(battle->HeroAt(battle->CurrentAttack().index)); + const AttackChoice &ac(battle->HeroAt(battle->CurrentAttack().index).GetAttackChoice()); switch (ac.GetType()) { case AttackChoice::SWORD: if (hero.HasWeapon()) { titleBarText = hero.Weapon()->Name(); - moveAnimation = hero.AttackAnimation(); + targetAnimation = AnimationRunner(hero.Weapon()->AttackAnimation()); } else { titleBarText = "Melee attack!"; - if (hero.MeleeAnimation()) { - moveAnimation = 0; - attackAnimation = hero.MeleeAnimation(); - } else { - moveAnimation = hero.AttackAnimation(); - attackAnimation = 0; - } - } - numberAnimation.push_back(NumberAnimation(15, battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite)); - if (ac.Selection().TargetsEnemies()) { - numberPosition.push_back( - battle->MonsterPositions()[ac.Selection().SingleSelection()]); - } else { - numberPosition.push_back( - battle->HeroesPositions()[ac.Selection().SingleSelection()]); + targetAnimation = AnimationRunner(hero.MeleeAnimation()); } + 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(1500); - if (moveAnimation) moveAnimation->Start(*this); - if (attackAnimation) attackAnimation->Start(battle, this); + if (titleBarText) { + titleBarTimer = GraphicsTimers().StartCountdown(850); + textPosition = battle->ScreenOffset() + Vector( + (battle->Width() - std::strlen(titleBarText) * battle->Res().titleFont->CharWidth()) / 2, + battle->Res().titleFrame->BorderHeight()); + } + if (moveAnimation.Valid()) { + moveAnimation.Start(*this); + if (battle->CurrentAttack().IsMonster()) { + battle->MonsterAt(battle->CurrentAttack().index).SetAnimation(moveAnimation); + } else if (battle->CurrentAttack().IsHero()) { + battle->HeroAt(battle->CurrentAttack().index).SetAnimation(moveAnimation); + } else { + battle->GetCapsule().SetAnimation(moveAnimation); + } + } + if (targetAnimation.Valid()) { + targetAnimationTimer = GraphicsTimers().StartCountdown(150); + } else { + targetAnimationTimer.Clear(); + } +} + +void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) { + 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)); + numberPosition.push_back( + battle->MonsterAt(i).Position() + battle->ScreenOffset()); + } else if (ts.IsGood(i)) { + numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite)); + numberPosition.push_back( + battle->MonsterAt(i).Position() + battle->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)); + numberPosition.push_back( + battle->HeroAt(i).Position() + battle->ScreenOffset()); + } else if (ts.IsGood(i)) { + numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite)); + numberPosition.push_back( + battle->HeroAt(i).Position() + battle->ScreenOffset()); + } + } + } } -void PerformAttacks::CheckNumberAnimation() { - if (moveAnimation && moveAnimation->Running()) return; - if (attackAnimation && !attackAnimation->Finished()) return; - if (moveAnimation || attackAnimation) { - moveAnimation = 0; - attackAnimation = 0; +void PerformAttacks::CheckAnimations() { + if (targetAnimation.Valid() && targetAnimationTimer.JustHit()) { + targetAnimation.Start(*this); + } + 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); } @@ -153,7 +196,8 @@ void PerformAttacks::CheckNumberAnimation() { bool PerformAttacks::HasAnimationsRunning() const { if (titleBarTimer.Running()) return true; - if (moveAnimation && moveAnimation->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; } @@ -161,58 +205,67 @@ bool PerformAttacks::HasAnimationsRunning() const { } void PerformAttacks::ResetAnimation() { - if (moveAnimation) moveAnimation->Stop(); - if (attackAnimation) attackAnimation = 0; + if (moveAnimation.Valid()) { + moveAnimation.Clear(); + if (!battle->CurrentAttack().IsMonster()) { + battle->HeroAt(battle->CurrentAttack().index).GetAnimation().Clear(); + } + } + if (targetAnimation.Valid()) { + targetAnimation.Clear(); + } titleBarTimer.Clear(); numberAnimation.clear(); numberPosition.clear(); } -void PerformAttacks::AdvanceCursor() { - ++cursor; - while (cursor < int(order.size())) { - if (order[cursor].isMonster) { - if (battle->MonsterAt(order[cursor].index).Health() > 0) break; - } else { - if (battle->HeroAt(order[cursor].index).Health() > 0) break; - } - ++cursor; - } -} - -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); - if (attackAnimation) attackAnimation->Render(screen, offset); + battle->RenderBackground(screen); + battle->RenderMonsters(screen); + battle->RenderHeroes(screen); + battle->RenderCapsule(screen); + battle->RenderSmallHeroTags(screen); + RenderTitleBar(screen); + RenderNumbers(screen); + RenderTargetAnimation(screen); } -void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector &offset) { +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); + battle->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); + battle->Res().titleFont->DrawString(titleBarText, screen, textPosition); } -void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector &offset) { +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 { + if (!targetAnimation.Valid() || !targetAnimation.Running()) return; + const TargetSelection &ts(battle->CurrentAttackAttackChoice().Selection()); + 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() + battle->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() + battle->ScreenOffset()); + } } } }