X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fbattle%2Fstates%2FPerformAttacks.cpp;h=36b7b1630d41477fa23ba7055ed629ee39078a8c;hb=4c3d420d8fc933d334def034f5d76ba23934479d;hp=f5a8c87334af8e064170b6b7771cfb7aca47703e;hpb=35f8a25fc914f10a8e987357bab94584af0992fb;p=l2e.git diff --git a/src/battle/states/PerformAttacks.cpp b/src/battle/states/PerformAttacks.cpp index f5a8c87..36b7b16 100644 --- a/src/battle/states/PerformAttacks.cpp +++ b/src/battle/states/PerformAttacks.cpp @@ -17,6 +17,7 @@ #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,20 +27,24 @@ using app::Application; using app::Input; using geometry::Point; using geometry::Vector; +using std::vector; namespace battle { void PerformAttacks::EnterState(Application &c, SDL_Surface *screen) { ctrl = &c; - // TODO: push battle animation if enemy is faster + 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; } void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) { - fakeMoveTimer = GraphicsTimers().StartCountdown(850); + } void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) { @@ -53,56 +58,149 @@ void PerformAttacks::Resize(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; + 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(); + moveAnimation = monster.AttackAnimation(); + targetAnimation = monster.MeleeAnimation(); + AddNumberAnimations(battle->MonsterAttackChoiceAt(battle->CurrentAttack().index).Selection()); + } else { + Hero &hero(battle->HeroAt(battle->CurrentAttack().index)); + const AttackChoice &ac(battle->AttackChoiceAt(battle->CurrentAttack().index)); + + switch (ac.GetType()) { + case AttackChoice::SWORD: + if (hero.HasWeapon()) { + titleBarText = hero.Weapon()->Name(); + targetAnimation = hero.Weapon()->AttackAnimation(); + } else { + titleBarText = "Melee attack!"; + targetAnimation = hero.MeleeAnimation(); } - if (cursor >= battle->MaxMonsters()) { - battle->ClearAllAttacks(); - ctrl->PopState(); + moveAnimation = hero.AttackAnimation(); + AddNumberAnimations(ac.Selection()); + break; + case AttackChoice::MAGIC: + titleBarText = ac.GetSpell()->Name(); + moveAnimation = hero.SpellAnimation(); + break; + case AttackChoice::DEFEND: + titleBarText = "Defends."; + moveAnimation = 0; + break; + case AttackChoice::IKARI: + if (ac.GetItem()->HasIkari()) { + titleBarText = ac.GetItem()->GetIkari()->Name(); + if (ac.GetItem()->GetIkari()->IsMagical()) { + moveAnimation = hero.SpellAnimation(); + } else { + moveAnimation = hero.AttackAnimation(); + } } - } else { - titleBarText = battle->MonsterAt(cursor).Name(); + break; + case AttackChoice::ITEM: + titleBarText = ac.GetItem()->Name(); + moveAnimation = 0; + break; + case AttackChoice::UNDECIDED: + titleBarText = "UNDECIDED"; + moveAnimation = 0; + break; + } + } + + if (titleBarText) titleBarTimer = GraphicsTimers().StartCountdown(850); + if (moveAnimation) moveAnimation->Start(*this); + if (targetAnimation) { + targetAnimationTimer = GraphicsTimers().StartCountdown(150); + } else { + targetAnimationTimer.Clear(); + } +} + +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 { - if (titleBarText) { - titleBarText = 0; - ++cursor; - if (cursor == battle->NumHeroes()) { - cursor = 0; - monsters = true; - } - } else { - const AttackChoice &ac(battle->AttackChoiceAt(cursor)); - 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; - } + } + } 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 && targetAnimationTimer.JustHit()) { + targetAnimation->Start(*this); + } + if (moveAnimation && !moveAnimation->Finished()) return; + if (targetAnimation && !targetAnimation->Finished()) return; + if (moveAnimation || targetAnimation) { + moveAnimation = 0; + targetAnimation = 0; + 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 && moveAnimation->Running()) return true; + if (targetAnimation && 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) { + moveAnimation->Stop(); + moveAnimation = 0; + } + if (targetAnimation) { + targetAnimation->Stop(); + targetAnimation = 0; + } + titleBarTimer.Clear(); + numberAnimation.clear(); + numberPosition.clear(); +} + void PerformAttacks::UpdateWorld(float deltaT) { @@ -113,20 +211,42 @@ void PerformAttacks::Render(SDL_Surface *screen) { battle->RenderBackground(screen, offset); battle->RenderMonsters(screen, offset); battle->RenderHeroes(screen, offset); - // render small tags + 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, Point(offset.X(), offset.Y()), battle->Width(), height); Point textPosition( - (battle->BackgroundWidth() - (std::strlen(titleBarText) * battle->Res().titleFont->CharWidth())) / 2, + (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 || !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); + } + } +} + }