X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fbattle%2Fstates%2FPerformAttacks.cpp;h=0faecf9b5ead48f334e663c0817cb185abb1311f;hb=c68b073db752dcdcf8c900efba7d8a5da091751c;hp=dd0e0df1cd0a27de4102081e4b8a6e470e945f01;hpb=8f6a2452408f4ae35396cd7008a6fab0ecb03a46;p=l2e.git diff --git a/src/battle/states/PerformAttacks.cpp b/src/battle/states/PerformAttacks.cpp index dd0e0df..0faecf9 100644 --- a/src/battle/states/PerformAttacks.cpp +++ b/src/battle/states/PerformAttacks.cpp @@ -27,18 +27,20 @@ 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; - battle->WriteOrder(order); + 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) { + battle->ClearAllAttacks(); ctrl = 0; } @@ -57,73 +59,123 @@ void PerformAttacks::Resize(int width, int height) { void PerformAttacks::HandleEvents(const Input &input) { - CheckNumberAnimation(); + CheckAnimations(); if (HasAnimationsRunning()) return; ResetAnimation(); - AdvanceCursor(); - if (Finished()) { - battle->ClearAllAttacks(); + 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->MonsterAttackChoiceAt(battle->CurrentAttack().index).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: - titleBarText = hero.HasWeapon() ? hero.Weapon()->Name() : "Melee attack!"; - moveAnimation = hero.AttackAnimation(); - numberAnimation.push_back(NumberAnimation(15, battle->Res().numberAnimationPrototype, battle->Res().bigNumberSprite)); - if (ac.Selection().TargetsEnemies()) { - numberPosition.push_back( - battle->MonsterPositions()[ac.Selection().SingleSelection()]); + if (hero.HasWeapon()) { + titleBarText = hero.Weapon()->Name(); + targetAnimation = AnimationRunner(hero.Weapon()->AttackAnimation()); } else { - numberPosition.push_back( - battle->HeroesPositions()[ac.Selection().SingleSelection()]); + titleBarText = "Melee attack!"; + 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 (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850); + if (moveAnimation.Valid()) { + moveAnimation.Start(*this); + if (battle->CurrentAttack().isMonster) { + battle->MonsterAt(battle->CurrentAttack().index).SetAnimation(moveAnimation); + } else { + battle->HeroAt(battle->CurrentAttack().index).SetAnimation(moveAnimation); + } + } + if (targetAnimation.Valid()) { + targetAnimationTimer = GraphicsTimers().StartCountdown(150); + } else { + targetAnimationTimer.Clear(); + } } -void PerformAttacks::CheckNumberAnimation() { - if (moveAnimation && moveAnimation->Running()) return; - if (!moveAnimation || moveAnimation->JustFinished()) { +void PerformAttacks::AddNumberAnimations(const TargetSelection &ts) { + if (ts.TargetsEnemies()) { + 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->MonsterPositions()[i]); + } else if (ts.IsGood(i)) { + numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite)); + numberPosition.push_back( + battle->MonsterPositions()[i]); + } + } + } 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->HeroesPositions()[i]); + } else if (ts.IsGood(i)) { + numberAnimation.push_back(NumberAnimation(ts.GetAmount(i), battle->Res().numberAnimationPrototype, battle->Res().greenNumberSprite)); + numberPosition.push_back( + battle->HeroesPositions()[i]); + } + } + } +} + +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); } @@ -136,7 +188,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; } @@ -144,24 +197,20 @@ bool PerformAttacks::HasAnimationsRunning() const { } void PerformAttacks::ResetAnimation() { - if (moveAnimation) moveAnimation->Stop(); + 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) { @@ -175,9 +224,10 @@ void PerformAttacks::Render(SDL_Surface *screen) { battle->RenderSmallHeroTags(screen, offset); RenderTitleBar(screen, offset); RenderNumbers(screen, offset); + RenderTargetAnimation(screen, offset); } -void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector &offset) { +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()); @@ -189,7 +239,7 @@ void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector &offs battle->Res().titleFont->DrawString(titleBarText, screen, textPosition + offset); } -void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector &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); @@ -198,4 +248,15 @@ void PerformAttacks::RenderNumbers(SDL_Surface *screen, const Vector &offse } } +void PerformAttacks::RenderTargetAnimation(SDL_Surface *screen, const geometry::Vector &offset) 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); + } + } +} + }