X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fbattle%2Fstates%2FPerformAttacks.cpp;h=b5cd9bebec80754ee019001b972616b557829e34;hb=aedc31b88715246abc00a0ab333bea6e17bbb780;hp=e5bd7c98207da2c9b5bbc76f2a0713e01eca57b4;hpb=adb1e3d5eafba2ef3de1499be286be330afaefef;p=l2e.git diff --git a/src/battle/states/PerformAttacks.cpp b/src/battle/states/PerformAttacks.cpp index e5bd7c9..b5cd9be 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,12 +27,15 @@ 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->WriteOrder(order); + numberAnimation.reserve(battle->MaxMonsters() > battle->NumHeroes() + 1 ? battle->MaxMonsters() : battle->NumHeroes() + 1); + numberPosition.reserve(numberAnimation.size()); } void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) { @@ -39,7 +43,7 @@ void PerformAttacks::ExitState(Application &c, SDL_Surface *screen) { } void PerformAttacks::ResumeState(Application &ctrl, SDL_Surface *screen) { - fakeMoveTimer = GraphicsTimers().StartCountdown(850); + } void PerformAttacks::PauseState(Application &ctrl, SDL_Surface *screen) { @@ -53,53 +57,108 @@ 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 < (int)battle->Monsters().size() && !battle->MonsterPositionOccupied(cursor)) { - ++cursor; + CheckNumberAnimation(); + if (HasAnimationsRunning()) return; + ResetAnimation(); + AdvanceCursor(); + if (Finished()) { + battle->ClearAllAttacks(); + ctrl->PopState(); + return; + } + + if (order[cursor].isMonster) { + const Monster &monster(battle->MonsterAt(order[cursor].index)); + titleBarText = monster.Name(); + moveAnimation = 0; + } else { + Hero &hero(battle->HeroAt(order[cursor].index)); + const AttackChoice &ac(battle->AttackChoiceAt(order[cursor].index)); + + 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()]); + } else { + numberPosition.push_back( + battle->HeroesPositions()[ac.Selection().SingleSelection()]); } - if (cursor >= (int)battle->Monsters().size()) { - battle->ClearAllAttacks(); - ctrl->PopState(); + 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(1500); + if (moveAnimation) moveAnimation->Start(*this); +} + +void PerformAttacks::CheckNumberAnimation() { + if (moveAnimation && moveAnimation->Running()) return; + if (!moveAnimation || moveAnimation->JustFinished()) { + 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; + 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(); + 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 (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; - } - } + if (battle->HeroAt(order[cursor].index).Health() > 0) break; } + ++cursor; } } @@ -113,12 +172,13 @@ 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); } void PerformAttacks::RenderTitleBar(SDL_Surface *screen, const Vector &offset) { - if (!titleBarText) return; + 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); @@ -129,4 +189,13 @@ 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) { + 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); + } + } +} + }