X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fbattle%2FBattleState.cpp;h=1f68acfa8bf05576202233639e008bc0d31d0c43;hb=e1dab8a680a76f8621e967a693dbf2b481ba8f75;hp=9e84206b3908dde82be2eba8fec5da82bf4e8661;hpb=4956380d5109fe57722a968ad1ffbf2fa0a5ed2e;p=l2e.git diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index 9e84206..1f68acf 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -1,25 +1,23 @@ -/* - * BattleState.cpp - * - * Created on: Aug 5, 2012 - * Author: holy - */ - #include "BattleState.h" #include "PartyLayout.h" #include "states/SelectMoveAction.h" #include "states/PerformAttacks.h" +#include "states/VictoryState.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" #include "../common/Spell.h" -#include "../geometry/operators.h" +#include "../graphics/Frame.h" #include "../graphics/Sprite.h" +#include "../math/Vector.h" #include +#include +#include #include using app::Application; @@ -27,150 +25,79 @@ using app::Input; using common::Inventory; using common::Item; using common::Spell; -using geometry::Point; -using geometry::Vector; +using common::Stats; +using math::Vector; using graphics::Menu; +using std::rand; using std::vector; namespace battle { void BattleState::AddMonster(const Monster &m) { - if (monsters.size() >= monstersLayout->NumPositions()) { - throw std::overflow_error("too many monsters for layout"); - } - monsters.push_back(m); + battle.AddMonster(m); } void BattleState::AddHero(const Hero &h) { - if (heroes.size() >= heroesLayout->NumPositions()) { - throw std::overflow_error("too many heroes for layout"); - } - heroes.push_back(h); + battle.AddHero(h); } -void BattleState::SwapHeroes(std::vector::size_type lhs, std::vector::size_type rhs) { - if (lhs < 0 || lhs >= heroes.size() || rhs < 0 || rhs >= heroes.size() || lhs == rhs) return; - std::swap(heroes[lhs], heroes[rhs]); +void BattleState::SetCapsule(const Capsule &c) { + battle.SetCapsule(c); } -void BattleState::Resize(int w, int h) { - +void BattleState::OnResize(int w, int h) { + offset = Vector( + (w - background->w) / 2, + (h - background->h) / 2); } -void BattleState::EnterState(Application &ctrl, SDL_Surface *screen) { - monstersLayout->CalculatePositions(background->w, background->h, monsterPositions); - heroesLayout->CalculatePositions(background->w, background->h, heroesPositions); - for (vector::size_type i(0), end(heroes.size()); i < end; ++i) { - spellMenus[i] = res->spellMenuPrototype; - LoadSpellMenu(i); - ikariMenus[i] = res->ikariMenuPrototype; - LoadIkariMenu(i); +void BattleState::OnEnterState(SDL_Surface *screen) { + for (int i(0); i < 4; ++i) { + Hero &hero = HeroAt(i); + hero.Position() = battle.HeroesLayout().CalculatePosition(i, background->w, background->h); + hero.SpellMenu() = *res->spellMenuProperties; + hero.UpdateSpellMenu(); + hero.IkariMenu() = *res->ikariMenuProperties; + hero.UpdateIkariMenu(res); heroTags[i] = HeroTag(this, i); + smallHeroTags[i] = SmallHeroTag(this, i); } - int tagHeight(attackTypeMenu.Height()); - int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2); - int xOffset((BackgroundWidth() - 2 * tagWidth) / 2); - heroTagPositions[0] = Point(xOffset, BackgroundHeight() - 2 * tagHeight); - heroTagPositions[1] = Point(xOffset + tagWidth, BackgroundHeight() - 2 * tagHeight); - heroTagPositions[2] = Point(xOffset, BackgroundHeight() - tagHeight); - heroTagPositions[3] = Point(xOffset + tagWidth, BackgroundHeight() - tagHeight); - - itemMenu = res->itemMenuPrototype; - LoadInventory(); -} + battle.GetCapsule().Position() = battle.HeroesLayout().CalculatePosition(4, background->w, background->h); -void BattleState::LoadSpellMenu(vector::size_type index) { - spellMenus[index].Clear(); - spellMenus[index].Reserve(HeroAt(index).Spells().size()); - for (vector::const_iterator i(HeroAt(index).Spells().begin()), end(HeroAt(index).Spells().end()); i != end; ++i) { - bool enabled((*i)->CanUseInBattle() && (*i)->Cost() <= HeroAt(index).Mana()); - spellMenus[index].Add((*i)->Name(), *i, enabled, 0, (*i)->Cost()); + for (int i(0); i < battle.NumMonsters(); ++i) { + MonsterAt(i).Position() = battle.MonstersLayout().CalculatePosition(i, background->w, background->h); } -} -void BattleState::LoadIkariMenu(vector::size_type index) { - ikariMenus[index].Clear(); - ikariMenus[index].Reserve(6); - - if (HeroAt(index).HasWeapon()) { - ikariMenus[index].Add( - HeroAt(index).Weapon()->Name(), - HeroAt(index).Weapon(), - HeroAt(index).Weapon()->HasIkari() && HeroAt(index).Weapon()->GetIkari()->Cost() <= HeroAt(index).IP(), - res->weaponMenuIcon, - 0, - HeroAt(index).Weapon()->HasIkari() ? HeroAt(index).Weapon()->GetIkari()->Name() : ""); - } else { - ikariMenus[index].Add(res->noEquipmentText, 0, false, res->weaponMenuIcon); - } - - if (HeroAt(index).HasArmor()) { - ikariMenus[index].Add( - HeroAt(index).Armor()->Name(), - HeroAt(index).Armor(), - HeroAt(index).Armor()->HasIkari() && HeroAt(index).Armor()->GetIkari()->Cost() <= HeroAt(index).IP(), - res->armorMenuIcon, - 0, - HeroAt(index).Armor()->HasIkari() ? HeroAt(index).Armor()->GetIkari()->Name() : ""); - } else { - ikariMenus[index].Add(res->noEquipmentText, 0, false, res->armorMenuIcon); - } - - if (HeroAt(index).HasShield()) { - ikariMenus[index].Add( - HeroAt(index).Shield()->Name(), - HeroAt(index).Shield(), - HeroAt(index).Shield()->HasIkari() && HeroAt(index).Shield()->GetIkari()->Cost() <= HeroAt(index).IP(), - res->shieldMenuIcon, - 0, - HeroAt(index).Shield()->HasIkari() ? HeroAt(index).Shield()->GetIkari()->Name() : ""); - } else { - ikariMenus[index].Add(res->noEquipmentText, 0, false, res->shieldMenuIcon); - } - - if (HeroAt(index).HasHelmet()) { - ikariMenus[index].Add( - HeroAt(index).Helmet()->Name(), - HeroAt(index).Helmet(), - HeroAt(index).Helmet()->HasIkari() && HeroAt(index).Helmet()->GetIkari()->Cost() <= HeroAt(index).IP(), - res->helmetMenuIcon, - 0, - HeroAt(index).Helmet()->HasIkari() ? HeroAt(index).Helmet()->GetIkari()->Name() : ""); - } else { - ikariMenus[index].Add(res->noEquipmentText, 0, false, res->helmetMenuIcon); - } - - if (HeroAt(index).HasRing()) { - ikariMenus[index].Add( - HeroAt(index).Ring()->Name(), - HeroAt(index).Ring(), - HeroAt(index).Ring()->HasIkari() && HeroAt(index).Ring()->GetIkari()->Cost() <= HeroAt(index).IP(), - res->ringMenuIcon, - 0, - HeroAt(index).Ring()->HasIkari() ? HeroAt(index).Ring()->GetIkari()->Name() : ""); - } else { - ikariMenus[index].Add(res->noEquipmentText, 0, false, res->ringMenuIcon); - } - - if (HeroAt(index).HasJewel()) { - ikariMenus[index].Add( - HeroAt(index).Jewel()->Name(), - HeroAt(index).Jewel(), - HeroAt(index).Jewel()->HasIkari() && HeroAt(index).Jewel()->GetIkari()->Cost() <= HeroAt(index).IP(), - res->jewelMenuIcon, - 0, - HeroAt(index).Jewel()->HasIkari() ? HeroAt(index).Jewel()->GetIkari()->Name() : ""); - } else { - ikariMenus[index].Add(res->noEquipmentText, 0, false, res->jewelMenuIcon); - } + int tagHeight(attackTypeMenu.Height()); + int tagWidth(attackTypeMenu.Width() * 2 + attackTypeMenu.Width() / 2); + int xOffset((Width() - 2 * tagWidth) / 2); + heroTagPositions[0] = Vector(xOffset, Height() - 2 * tagHeight); + heroTagPositions[1] = Vector(xOffset + tagWidth, Height() - 2 * tagHeight); + heroTagPositions[2] = Vector(xOffset, Height() - tagHeight); + heroTagPositions[3] = Vector(xOffset + tagWidth, Height() - tagHeight); + + tagHeight = res->normalFont->CharHeight() * 4 + res->smallHeroTagFrame->BorderHeight() * 2; + tagWidth = res->normalFont->CharWidth() * 6 + res->smallHeroTagFrame->BorderWidth() * 2; + xOffset = (Width() - 4 * tagWidth) / 2; + int yOffset(Height() - tagHeight); + smallHeroTagPositions[0] = Vector(xOffset, yOffset); + smallHeroTagPositions[1] = Vector(xOffset + 2 * tagWidth, yOffset); + smallHeroTagPositions[2] = Vector(xOffset + tagWidth, yOffset); + smallHeroTagPositions[3] = Vector(xOffset + 3 * tagWidth, yOffset); + + OnResize(screen->w, screen->h); + + itemMenu = *res->itemMenuProperties; + LoadInventory(); + battle.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) { @@ -181,57 +108,58 @@ 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) { - // TODO: check for victory or defeat +void BattleState::OnResumeState(SDL_Surface *screen) { if (ranAway) { - ctrl.PopState(); // quit the battle scene + Ctrl().PopState(); // quit the battle scene + return; + } + if (battle.Victory()) { + if (alreadyPushed) { + Ctrl().PopState(); + } else { + Ctrl().PushState(new VictoryState(&battle, this)); + alreadyPushed = true; + } return; } - if (AttackSelectionDone()) { - ctrl.PushState(new PerformAttacks(this)); + if (battle.Defeat()) { + Ctrl().PopState(); + return; + } + // TODO: this should not push a state while quitting + if (battle.AttackSelectionDone()) { + Ctrl().PushState(new PerformAttacks(&battle, this)); } else { - ctrl.PushState(new SelectMoveAction(this)); + Ctrl().PushState(new SelectMoveAction(&battle, this)); } } -void BattleState::PauseState(Application &ctrl, SDL_Surface *screen) { +void BattleState::OnPauseState(SDL_Surface *screen) { } - -void BattleState::ClearAllAttacks() { - activeHero = -1; - for (vector::size_type i(0), end(heroes.size()); i < end; ++i) { - attackChoices[i] = AttackChoice(this); - } -} - - void BattleState::HandleEvents(const Input &input) { } -void BattleState::UpdateWorld(float deltaT) { +void BattleState::UpdateWorld(Uint32 deltaT) { } void BattleState::Render(SDL_Surface *screen) { - Vector offset(CalculateScreenOffset(screen)); - - RenderBackground(screen, offset); - RenderMonsters(screen, offset); -// RenderHeroes(screen, offset); - RenderHeroTags(screen, offset); + assert(screen); + 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)); SDL_Rect destRect; @@ -242,24 +170,70 @@ void BattleState::RenderBackground(SDL_Surface *screen, const Vector &offse SDL_BlitSurface(background, 0, screen, &destRect); } -void BattleState::RenderMonsters(SDL_Surface *screen, const Vector &offset) { - for (vector::size_type i(0), end(monsters.size()); i < end; ++i) { - monsters[i].Sprite()->DrawCenterBottom(screen, monsterPositions[i] + offset); +void BattleState::RenderMonsters(SDL_Surface *screen) { + assert(screen); + for (vector::size_type i(0), end(battle.NumMonsters()); i < end; ++i) { + if (battle.MonsterPositionOccupied(i)) { + Monster &monster(battle.MonsterAt(i)); + if (monster.GetAnimation().Running()) { + monster.GetAnimation().DrawCenter(screen, monster.Position() + offset); + } else { + monster.Sprite()->DrawCenter(screen, monster.Position() + offset); + } + } } } -void BattleState::RenderHeroes(SDL_Surface *screen, const Vector &offset) { - for (vector::size_type i(0), end(heroes.size()); i < end; ++i) { - heroes[i].Sprite()->DrawCenterBottom(screen, heroesPositions[i] + offset, 0, 1); +void BattleState::RenderHeroes(SDL_Surface *screen) { + assert(screen); + for (int i(0); i < NumHeroes(); ++i) { + Hero &hero(battle.HeroAt(i)); + if (hero.GetAnimation().Running()) { + hero.GetAnimation().DrawCenter(screen, hero.Position() + offset); + } else { + int row(hero.Health() > 0 ? 0 : 2); + hero.Sprite()->DrawCenter(screen, hero.Position() + offset, 1, row); + } } } -void BattleState::RenderHeroTags(SDL_Surface *screen, const Vector &offset) { +void BattleState::RenderCapsule(SDL_Surface *screen) { + const Capsule &capsule(battle.GetCapsule()); + 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); - for (vector::size_type i(0), end(heroes.size()); i < end; ++i) { - heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, (int)i == activeHero); + for (int i(0); i < battle.NumHeroes(); ++i) { + heroTags[i].Render(screen, tagWidth, tagHeight, heroTagPositions[i] + offset, battle.IsActiveHero(i)); + } +} + +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); + + SDL_Rect rect; + rect.x = offset.X(); + rect.y = offset.Y() + Height() - tagHeight; + rect.w = Width(); + rect.h = tagHeight; + SDL_FillRect(screen, &rect, SDL_MapRGB(screen->format, 0, 0, 0)); + rect.y += res->normalFont->CharHeight() / 8; + rect.h -= res->normalFont->CharHeight() / 4; + SDL_FillRect(screen, &rect, res->heroesBgColor.MapRGB(screen->format)); + + for (int i(0); i < battle.NumHeroes(); ++i) { + smallHeroTags[i].Render(screen, tagWidth, tagHeight, smallHeroTagPositions[i] + offset); } }