X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fbattle%2Fstates%2FPerformAttacks.cpp;h=fac7cdf855aa42a4c8422f38e5ebd715afd52608;hb=e1edc92c4fb834c8061118e89c0d7e239742b030;hp=93a09ef77d8baa79726a59149880a1518364451f;hpb=854e5229c3f30bf88aa08d7f0aff56a1411c3367;p=l2e.git diff --git a/src/battle/states/PerformAttacks.cpp b/src/battle/states/PerformAttacks.cpp index 93a09ef..fac7cdf 100644 --- a/src/battle/states/PerformAttacks.cpp +++ b/src/battle/states/PerformAttacks.cpp @@ -10,13 +10,12 @@ #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,131 +24,187 @@ using app::Application; using app::Input; -using geometry::Point; using geometry::Vector; +using graphics::AnimationRunner; +using std::vector; namespace battle { -void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) { - ctrl = &c; +void PerformAttacks::OnEnterState(SDL_Surface *screen) { + battle->CalculateAttackOrder(); + numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1); + numberPosition.reserve(numberAnimation.size()); } -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) { - fakeMoveTimer = GraphicsTimers().StartCountdown(850); +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) { } void PerformAttacks::HandleEvents(const Input &input) { - if (fakeMoveTimer.JustHit()) { - if (monsters) { - if (titleBarText) { - titleBarText = 0; - ++cursor; - while (cursor < battle->MaxMonsters() && !battle->MonsterPositionOccupied(cursor)) { - ++cursor; - } - if (cursor >= battle->MaxMonsters()) { - battle->ClearAllAttacks(); - ctrl->PopState(); + CheckAnimations(); + if (HasAnimationsRunning()) return; + ResetAnimation(); + battle->ApplyDamage(); + battle->NextAttack(); + if (battle->AttacksFinished()) { + Ctrl().PopState(); + return; + } + + battle->CalculateDamage(); + + if (battle->CurrentAttack().isMonster) { + Monster &monster(battle->MonsterAt(battle->CurrentAttack().index)); + titleBarText = monster.Name(); + targetAnimation = AnimationRunner(monster.MeleeAnimation()); + moveAnimation = AnimationRunner(monster.AttackAnimation()); + monster.SetAnimation(moveAnimation); + AddNumberAnimations(battle->MonsterAt(battle->CurrentAttack().index).GetAttackChoice().Selection()); + } else { + 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(); + targetAnimation = AnimationRunner(hero.Weapon()->AttackAnimation()); + } else { + titleBarText = "Melee attack!"; + targetAnimation = AnimationRunner(hero.MeleeAnimation()); } - } else { - if (cursor == 0) { - if (battle->HeroAt(battle->NumHeroes() - 1).AttackAnimation()) { - battle->HeroAt(battle->NumHeroes() - 1).AttackAnimation()->Stop(); - } - if (battle->HeroAt(battle->NumHeroes() - 1).SpellAnimation()) { - battle->HeroAt(battle->NumHeroes() - 1).SpellAnimation()->Stop(); + moveAnimation = AnimationRunner(hero.AttackAnimation()); + AddNumberAnimations(ac.Selection()); + break; + case AttackChoice::MAGIC: + titleBarText = ac.GetSpell()->Name(); + moveAnimation = AnimationRunner(hero.SpellAnimation()); + break; + case AttackChoice::DEFEND: + titleBarText = "Defends."; + moveAnimation.Clear(); + break; + case AttackChoice::IKARI: + if (ac.GetItem()->HasIkari()) { + titleBarText = ac.GetItem()->GetIkari()->Name(); + if (ac.GetItem()->GetIkari()->IsMagical()) { + moveAnimation = AnimationRunner(hero.SpellAnimation()); + } else { + moveAnimation = AnimationRunner(hero.AttackAnimation()); } } - titleBarText = battle->MonsterAt(cursor).Name(); - } + break; + case AttackChoice::ITEM: + titleBarText = ac.GetItem()->Name(); + moveAnimation.Clear(); + break; + case AttackChoice::UNDECIDED: + titleBarText = "UNDECIDED"; + moveAnimation.Clear(); + break; + } + } + + if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850); + if (moveAnimation.Valid()) { + moveAnimation.Start(*this); + if (battle->CurrentAttack().isMonster) { + battle->MonsterAt(battle->CurrentAttack().index).SetAnimation(moveAnimation); } else { - const AttackChoice &ac(battle->AttackChoiceAt(cursor)); - if (titleBarText) { - titleBarText = 0; - - switch (ac.GetType()) { - case AttackChoice::SWORD: - if (battle->HeroAt(cursor).AttackAnimation()) { - battle->HeroAt(cursor).AttackAnimation()->Start(*this); - } - break; - case AttackChoice::MAGIC: - if (battle->HeroAt(cursor).SpellAnimation()) { - battle->HeroAt(cursor).SpellAnimation()->Start(*this); - } - break; - case AttackChoice::DEFEND: - break; - case AttackChoice::IKARI: - if (ac.GetItem()->HasIkari()) { - if (ac.GetItem()->GetIkari()->IsMagical()) { - if (battle->HeroAt(cursor).SpellAnimation()) { - battle->HeroAt(cursor).SpellAnimation()->Start(*this); - } - } else { - if (battle->HeroAt(cursor).AttackAnimation()) { - battle->HeroAt(cursor).AttackAnimation()->Start(*this); - } - } - } - break; - case AttackChoice::ITEM: - break; - case AttackChoice::UNDECIDED: - break; - } + battle->HeroAt(battle->CurrentAttack().index).SetAnimation(moveAnimation); + } + } + if (targetAnimation.Valid()) { + targetAnimationTimer = GraphicsTimers().StartCountdown(150); + } else { + targetAnimationTimer.Clear(); + } +} - ++cursor; - if (cursor == battle->NumHeroes()) { - cursor = 0; - monsters = true; - } - } else { - if (cursor > 0) { - if (battle->HeroAt(cursor - 1).AttackAnimation()) { - battle->HeroAt(cursor - 1).AttackAnimation()->Stop(); - } - if (battle->HeroAt(cursor - 1).SpellAnimation()) { - battle->HeroAt(cursor - 1).SpellAnimation()->Stop(); - } - } - switch (ac.GetType()) { - case AttackChoice::SWORD: - titleBarText = battle->HeroAt(cursor).HasWeapon() ? battle->HeroAt(cursor).Weapon()->Name() : "Gauntlet"; - break; - case AttackChoice::MAGIC: - titleBarText = ac.GetSpell()->Name(); - break; - case AttackChoice::DEFEND: - titleBarText = "Defends."; - break; - case AttackChoice::IKARI: - titleBarText = ac.GetItem()->HasIkari() ? ac.GetItem()->GetIkari()->Name() : "No Ikari?"; - break; - case AttackChoice::ITEM: - titleBarText = ac.GetItem()->Name(); - break; - case AttackChoice::UNDECIDED: - titleBarText = "WTF???"; - break; - } +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()); + } 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()); } } + } 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()); + } 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()); + } + } + } +} + +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); + } + } else { + for (vector::iterator i(numberAnimation.begin()), end(numberAnimation.end()); i != end; ++i) { + i->CheckTimers(*this); + } + } +} + +bool PerformAttacks::HasAnimationsRunning() const { + if (titleBarTimer.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; + } + return false; +} + +void PerformAttacks::ResetAnimation() { + 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(); } @@ -162,20 +217,50 @@ void PerformAttacks::Render(SDL_Surface *screen) { battle->RenderBackground(screen, offset); battle->RenderMonsters(screen, offset); battle->RenderHeroes(screen, offset); + battle->RenderCapsule(screen, offset); battle->RenderSmallHeroTags(screen, offset); RenderTitleBar(screen, offset); + RenderNumbers(screen, offset); + RenderTargetAnimation(screen, offset); } -void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector &offset) { - if (!titleBarText) return; +void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector &offset) 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->BackgroundWidth(), height); + battle->Res().titleFrame->Draw(screen, offset, battle->Width(), height); - Point textPosition( - (battle->BackgroundWidth() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2, + Vector textPosition( + (battle->Width() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2, battle->Res().titleFrame->BorderHeight()); battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset); } +void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector &offset) 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); + } + } +} + +void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector &offset) 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() + offset); + } + } + } else { + for (vector >::size_type i(0), end(battle->MaxMonsters()); i < end; ++i) { + if (ts.IsSelected(i)) { + targetAnimation.DrawCenter(screen, battle->MonsterAt(i).Position() + offset); + } + } + } +} + }