X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fbattle%2FBattleState.cpp;h=57ff3270d170daab546e8c51c461bcb6195b5ba3;hb=0b11a24a8b08c49d6e4301573602fb6d01e7a8c8;hp=d442f2289becfefbbf4432ac37e637e292001eb3;hpb=2ccc2369d32fb680a3047519d79c17de34c4e10a;p=l2e.git diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index d442f22..57ff327 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" @@ -19,6 +12,7 @@ #include "../common/Spell.h" #include "../graphics/Frame.h" #include "../graphics/Sprite.h" +#include "../math/Vector.h" #include #include @@ -31,7 +25,7 @@ using common::Inventory; using common::Item; using common::Spell; using common::Stats; -using geometry::Vector; +using math::Vector; using graphics::Menu; using std::rand; @@ -54,6 +48,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) { @@ -75,11 +73,13 @@ void BattleState::SwapHeroes(int lhs, int rhs) { void BattleState::OnResize(int w, int h) { - + offset = Vector( + (w - background->w) / 2, + (h - background->h) / 2); } -void BattleState::OnEnterState(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; @@ -90,6 +90,8 @@ void BattleState::OnEnterState(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,8 +113,11 @@ void BattleState::OnEnterState(Application &ctrl, SDL_Surface *screen) { smallHeroTagPositions[2] = Vector(xOffset + tagWidth, yOffset); smallHeroTagPositions[3] = Vector(xOffset + 3 * tagWidth, yOffset); + OnResize(screen->w, screen->h); + itemMenu = *res->itemMenuProperties; LoadInventory(); + ClearAllAttacks(); } void BattleState::LoadInventory() { @@ -127,31 +132,30 @@ void BattleState::LoadInventory() { itemMenu.AddEmptyEntry(); } } - ClearAllAttacks(); } -void BattleState::OnExitState(Application &ctrl, SDL_Surface *screen) { +void BattleState::OnExitState(SDL_Surface *screen) { } -void BattleState::OnResumeState(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)); } } @@ -169,7 +173,7 @@ bool BattleState::Defeat() const { return true; } -void BattleState::OnPauseState(Application &ctrl, SDL_Surface *screen) { +void BattleState::OnPauseState(SDL_Surface *screen) { } @@ -178,9 +182,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; @@ -189,12 +191,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)); } @@ -205,10 +210,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; } @@ -220,23 +227,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(); @@ -250,6 +254,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()) { @@ -305,7 +349,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) { @@ -329,11 +373,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() { @@ -345,6 +385,7 @@ void BattleState::ClearAllAttacks() { for (int i(0); i < MaxMonsters(); ++i) { MonsterAt(i).GetAttackChoice() = AttackChoice(this); } + capsule.GetAttackChoice() = AttackChoice(this); attackOrder.clear(); } @@ -353,18 +394,17 @@ void BattleState::HandleEvents(const Input &input) { } -void BattleState::UpdateWorld(float deltaT) { +void BattleState::UpdateWorld(Uint32 deltaT) { } void BattleState::Render(SDL_Surface *screen) { assert(screen); - Vector offset(CalculateScreenOffset(screen)); - RenderBackground(screen, offset); - RenderMonsters(screen, offset); + RenderBackground(screen); + RenderMonsters(screen); } -void BattleState::RenderBackground(SDL_Surface *screen, const Vector &offset) { +void BattleState::RenderBackground(SDL_Surface *screen) { assert(screen); // black for now SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0)); @@ -376,7 +416,7 @@ void BattleState::RenderBackground(SDL_Surface *screen, const Vector &offse SDL_BlitSurface(background, 0, screen, &destRect); } -void BattleState::RenderMonsters(SDL_Surface *screen, const Vector &offset) { +void BattleState::RenderMonsters(SDL_Surface *screen) { assert(screen); for (vector::size_type i(0), end(monsters.size()); i < end; ++i) { if (MonsterPositionOccupied(i)) { @@ -389,7 +429,7 @@ void BattleState::RenderMonsters(SDL_Surface *screen, const Vector &offset) } } -void BattleState::RenderHeroes(SDL_Surface *screen, const Vector &offset) { +void BattleState::RenderHeroes(SDL_Surface *screen) { assert(screen); for (int i(0); i < numHeroes; ++i) { if (heroes[i].GetAnimation().Running()) { @@ -401,7 +441,16 @@ void BattleState::RenderHeroes(SDL_Surface *screen, const Vector &offset) { } } -void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector &offset) { +void BattleState::RenderCapsule(SDL_Surface *screen) { + 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) { assert(screen); int tagHeight(attackTypeMenu.Height()); int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2); @@ -411,7 +460,7 @@ void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector &offset) } } -void BattleState::RenderSmallHeroTags(SDL_Surface *screen, const Vector &offset) { +void BattleState::RenderSmallHeroTags(SDL_Surface *screen) { assert(screen); int tagHeight(res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2); int tagWidth(res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2);