X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fbattle%2FBattleState.cpp;h=cfac62d0b1fb0f655ffc33f5db678886990da882;hb=5d1a76ae7725af998c6ee46adfe492c68ee1d34f;hp=d9586636d9667ee2d97756e83c28fcc3913bc77a;hpb=d5959073b2c413ba1bd6f3d14bc8bcf59304e488;p=l2e.git diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index d958663..cfac62d 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -1,10 +1,3 @@ -/* - * BattleState.cpp - * - * Created on: Aug 5, 2012 - * Author: holy - */ - #include "BattleState.h" #include "PartyLayout.h" @@ -12,6 +5,7 @@ #include "states/PerformAttacks.h" #include "../app/Application.h" #include "../app/Input.h" +#include "../common/GameState.h" #include "../common/Ikari.h" #include "../common/Inventory.h" #include "../common/Item.h" @@ -29,7 +23,8 @@ using app::Input; using common::Inventory; using common::Item; using common::Spell; -using geometry::Vector; +using common::Stats; +using math::Vector; using graphics::Menu; using std::rand; @@ -52,6 +47,10 @@ void BattleState::AddHero(const Hero &h) { ++numHeroes; } +void BattleState::SetCapsule(const Capsule &c) { + capsule = c; +} + void BattleState::NextHero() { ++activeHero; while (activeHero < numHeroes && heroes[activeHero].Health() == 0) { @@ -72,12 +71,12 @@ void BattleState::SwapHeroes(int lhs, int rhs) { } -void BattleState::Resize(int w, int h) { +void BattleState::OnResize(int w, int h) { } -void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) { +void BattleState::OnEnterState(SDL_Surface *screen) { for (int i(0); i < 4; ++i) { heroes[i].Position() = heroesLayout->CalculatePosition(i, background->w, background->h); heroes[i].SpellMenu() = *res->spellMenuProperties; @@ -88,6 +87,8 @@ void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) { smallHeroTags[i] = SmallHeroTag(this, i); } + capsule.Position() = heroesLayout->CalculatePosition(4, background->w, background->h); + for (int i(0); i < int(monsters.size()); ++i) { monsters[i].Position() = monstersLayout->CalculatePosition(i, background->w, background->h); } @@ -111,10 +112,11 @@ void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) { itemMenu = *res->itemMenuProperties; LoadInventory(); + ClearAllAttacks(); } void BattleState::LoadInventory() { - const Inventory &inv(*res->inventory); + const Inventory &inv(game->state->inventory); itemMenu.Clear(); itemMenu.Reserve(inv.MaxItems()); for (int i(0); i < inv.MaxItems(); ++i) { @@ -125,31 +127,30 @@ void BattleState::LoadInventory() { itemMenu.AddEmptyEntry(); } } - ClearAllAttacks(); } -void BattleState::ExitState(Application &ctrl, SDL_Surface *screen) { +void BattleState::OnExitState(SDL_Surface *screen) { } -void BattleState::ResumeState(Application &ctrl, SDL_Surface *screen) { +void BattleState::OnResumeState(SDL_Surface *screen) { if (ranAway) { - ctrl.PopState(); // quit the battle scene + Ctrl().PopState(); // quit the battle scene return; } if (Victory()) { - ctrl.PopState(); + Ctrl().PopState(); return; } if (Defeat()) { - ctrl.PopState(); + Ctrl().PopState(); return; } // TODO: this should not push a state while quitting if (AttackSelectionDone()) { - ctrl.PushState(new PerformAttacks(this)); + Ctrl().PushState(new PerformAttacks(this)); } else { - ctrl.PushState(new SelectMoveAction(this)); + Ctrl().PushState(new SelectMoveAction(this)); } } @@ -167,7 +168,7 @@ bool BattleState::Defeat() const { return true; } -void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) { +void BattleState::OnPauseState(SDL_Surface *screen) { } @@ -176,9 +177,7 @@ class OrderCompare { public: OrderCompare(BattleState *battle) : battle(battle) { } bool operator ()(const BattleState::Order &lhs, const BattleState::Order &rhs) { - int lagl(lhs.isMonster ? battle->MonsterAt(lhs.index).GetStats().Agility() : battle->HeroAt(lhs.index).GetStats().Agility()); - int ragl(rhs.isMonster ? battle->MonsterAt(rhs.index).GetStats().Agility() : battle->HeroAt(rhs.index).GetStats().Agility()); - return lagl > ragl; + return lhs.GetStats(*battle).Agility() > rhs.GetStats(*battle).Agility(); } private: BattleState *battle; @@ -187,12 +186,15 @@ class OrderCompare { void BattleState::CalculateAttackOrder() { attackOrder.reserve(monsters.size() + NumHeroes()); for (int i(0); i < NumHeroes(); ++i) { - attackOrder.push_back(Order(i, false)); + attackOrder.push_back(Order(Order::HERO, i)); } for (vector::size_type i(0), end(monsters.size()); i < end; ++i) { - attackOrder.push_back(Order(i, true)); + attackOrder.push_back(Order(Order::MONSTER, i)); MonsterAt(i).GetAttackChoice() = AttackChoice(this); } + if (capsule.Active() && capsule.Health() > 0) { + attackOrder.push_back(Order(Order::CAPSULE)); + } std::sort(attackOrder.begin(), attackOrder.end(), OrderCompare(this)); } @@ -203,10 +205,12 @@ void BattleState::NextAttack() { } ++attackCursor; while (attackCursor < int(attackOrder.size())) { - if (attackOrder[attackCursor].isMonster) { + if (attackOrder[attackCursor].IsMonster()) { if (MonsterAt(attackOrder[attackCursor].index).Health() > 0) break; - } else { + } else if (attackOrder[attackCursor].IsHero()) { if (HeroAt(attackOrder[attackCursor].index).Health() > 0) break; + } else { + if (capsule.Active() && capsule.Health() > 0) break; } ++attackCursor; } @@ -218,23 +222,20 @@ bool BattleState::AttacksFinished() const { } void BattleState::CalculateDamage() { - if (CurrentAttack().isMonster) { + if (CurrentAttack().IsMonster()) { DecideMonsterAttack(MonsterAt(CurrentAttack().index)); + } else if (CurrentAttack().IsCapsule()) { + DecideCapsuleAttack(); } - AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice()); + AttackChoice &ac = CurrentAttack().GetAttackChoice(*this); if (ac.GetType() == AttackChoice::DEFEND) return; TargetSelection &ts(ac.Selection()); - if (CurrentAttack().isMonster) { - const Stats &attackerStats(MonsterAt(CurrentAttack().index).GetStats()); - CalculateDamage(attackerStats, ts); - } else { - const Stats &attackerStats(HeroAt(CurrentAttack().index).GetStats()); - CalculateDamage(attackerStats, ts); - } + const Stats &attackerStats = CurrentAttack().GetStats(*this); + CalculateDamage(attackerStats, ts); } -void BattleState::DecideMonsterAttack(Monster &m) const { +void BattleState::DecideMonsterAttack(Monster &m) { AttackChoice &ac(m.GetAttackChoice()); TargetSelection &ts(ac.Selection()); ac.Reset(); @@ -248,6 +249,46 @@ void BattleState::DecideMonsterAttack(Monster &m) const { ts.Select(target); } +void BattleState::DecideCapsuleAttack() { + AttackChoice &ac(capsule.GetAttackChoice()); + TargetSelection &ts(ac.Selection()); + ac.Reset(); + int target(rand() % monsters.size()); + while (!MonsterPositionOccupied(target)) { + target = rand() % monsters.size(); + } + ac.SetType(AttackChoice::SWORD); + ts.SelectMonsters(); + ts.SetSingle(); + ts.Select(target); +} + +AttackChoice &BattleState::Order::GetAttackChoice(BattleState &b) const { + switch (by) { + case HERO: + return b.HeroAt(index).GetAttackChoice(); + case CAPSULE: + return b.GetCapsule().GetAttackChoice(); + case MONSTER: + return b.MonsterAt(index).GetAttackChoice(); + default: + throw std::runtime_error("invalid case in BttleStats::Order::GetAttackChoice()"); + } +} + +Stats &BattleState::Order::GetStats(BattleState &b) const { + switch (by) { + case HERO: + return b.HeroAt(index).GetStats(); + case CAPSULE: + return b.GetCapsule().GetStats(); + case MONSTER: + return b.MonsterAt(index).GetStats(); + default: + throw std::runtime_error("invalid case in BttleStats::Order::GetAttackChoice()"); + } +} + void BattleState::CalculateDamage(const Stats &attackerStats, TargetSelection &ts) const { bool hitSome(false); if (ts.TargetsMonsters()) { @@ -303,7 +344,7 @@ Uint16 BattleState::CalculateDamage(const Stats &attacker, const Stats &defender void BattleState::ApplyDamage() { if (attackCursor < 0) return; - AttackChoice &ac(CurrentAttack().isMonster ? MonsterAt(CurrentAttack().index).GetAttackChoice() : HeroAt(CurrentAttack().index).GetAttackChoice()); + AttackChoice &ac = CurrentAttack().GetAttackChoice(*this); TargetSelection &ts(ac.Selection()); if (ts.TargetsMonsters()) { for (int i(0); i < MaxMonsters(); ++i) { @@ -327,11 +368,7 @@ void BattleState::ApplyDamage() { } AttackChoice &BattleState::CurrentAttackAttackChoice() { - if (CurrentAttack().isMonster) { - return MonsterAt(CurrentAttack().index).GetAttackChoice(); - } else { - return HeroAt(CurrentAttack().index).GetAttackChoice(); - } + return CurrentAttack().GetAttackChoice(*this); } void BattleState::ClearAllAttacks() { @@ -343,6 +380,7 @@ void BattleState::ClearAllAttacks() { for (int i(0); i < MaxMonsters(); ++i) { MonsterAt(i).GetAttackChoice() = AttackChoice(this); } + capsule.GetAttackChoice() = AttackChoice(this); attackOrder.clear(); } @@ -351,7 +389,7 @@ void BattleState::HandleEvents(const Input &input) { } -void BattleState::UpdateWorld(float deltaT) { +void BattleState::UpdateWorld(Uint32 deltaT) { } @@ -399,6 +437,15 @@ void BattleState::RenderHeroes(SDL_Surface *screen, const Vector &offset) { } } +void BattleState::RenderCapsule(SDL_Surface *screen, const Vector &offset) { + if (!capsule.Active() || capsule.Health() <= 0) return; + if (capsule.GetAnimation().Running()) { + capsule.GetAnimation().DrawCenter(screen, capsule.Position() + offset); + } else { + capsule.Sprite()->DrawCenter(screen, capsule.Position() + offset); + } +} + void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector &offset) { assert(screen); int tagHeight(attackTypeMenu.Height());