#ifndef BATTLE_BATTLESTATE_H_
#define BATTLE_BATTLESTATE_H_
+#include "fwd.h"
+#include "AttackTypeMenu.h"
#include "Hero.h"
+#include "HeroTag.h"
#include "Monster.h"
+#include "MoveMenu.h"
+#include "Resources.h"
+#include "SmallHeroTag.h"
+#include "../app/fwd.h"
#include "../app/State.h"
-#include "../geometry/Point.h"
+#include "../common/GameConfig.h"
+#include "../common/fwd.h"
+#include "../common/Stats.h"
+#include "../geometry/Vector.h"
+#include "../graphics/Animation.h"
+#include "../graphics/fwd.h"
+#include "../graphics/Menu.h"
+#include <cassert>
#include <vector>
#include <SDL.h>
namespace battle {
-class PartyLayout;
-
class BattleState
: public app::State {
public:
- BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout)
- : background(background)
- , monstersLayout(&monstersLayout)
- , heroesLayout(&heroesLayout) { }
+ BattleState(common::GameConfig *game, SDL_Surface *background, const PartyLayout *monstersLayout)
+ : game(game)
+ , background(background)
+ , monstersLayout(monstersLayout)
+ , heroesLayout(game->heroesLayout)
+ , res(game->battleResources)
+ , attackTypeMenu(res->attackIcons)
+ , moveMenu(res->moveIcons)
+ , numHeroes(0)
+ , activeHero(-1)
+ , attackCursor(-1)
+ , expReward(0)
+ , goldReward(0)
+ , ranAway(false) { assert(background && monstersLayout && game); }
public:
void AddMonster(const Monster &);
public:
virtual void EnterState(app::Application &ctrl, SDL_Surface *screen);
- virtual void ExitState();
+ virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
+ virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
+ virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
virtual void Resize(int width, int height);
- virtual void HandleEvent(const SDL_Event &);
+ virtual void HandleEvents(const app::Input &);
virtual void UpdateWorld(float deltaT);
virtual void Render(SDL_Surface *);
+public:
+ const Resources &Res() const { return *res; }
+ AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
+ MoveMenu &GetMoveMenu() { return moveMenu; }
+
+ graphics::Menu<const common::Item *> &ItemMenu() { return itemMenu; }
+ const graphics::Menu<const common::Item *> &ItemMenu() const { return itemMenu; }
+
+ void NextHero();
+ bool BeforeFirstHero() const { return activeHero < 0; }
+ void PreviousHero();
+ void SwapHeroes(int lhs, int rhs);
+ Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
+ const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
+
+ Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
+ const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
+ Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
+ const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
+
+ const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
+ const geometry::Vector<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
+
+ bool HasChosenAttackType() const { return ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
+ bool AttackSelectionDone() const { return activeHero >= numHeroes; }
+
+ int NumHeroes() const { return numHeroes; }
+ int MaxHeroes() const { return 4; }
+ int MaxMonsters() const { return monsters.size(); }
+
+ bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
+ bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
+
+ void SetRunaway() { ranAway = true; }
+
+ struct Order {
+ Order(int index, bool isMonster)
+ : index(index), isMonster(isMonster) { }
+ int index;
+ bool isMonster;
+ };
+
+ void CalculateAttackOrder();
+ void NextAttack();
+ bool AttacksFinished() const;
+ void CalculateDamage();
+ void ApplyDamage();
+ const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
+ AttackChoice &CurrentAttackAttackChoice();
+ void ClearAllAttacks();
+
+ bool Victory() const;
+ bool Defeat() const;
+
+public:
+ geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
+ return geometry::Vector<int>(
+ (screen->w - background->w) / 2,
+ (screen->h - background->h) / 2);
+ }
+ int Width() const { return background->w; }
+ int Height() const { return background->h; }
+ geometry::Vector<int> Size() const { return geometry::Vector<int>(Width(), Height()); }
+
+ void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
+ void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
+ void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
+ void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
+ void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
+
+private:
+ void LoadInventory();
+
+ void DecideMonsterAttack(Monster &) const;
+ void CalculateDamage(const common::Stats &attackerStats, TargetSelection &targets) const;
+ Uint16 CalculateDamage(const common::Stats &attacker, const common::Stats &defender) const;
+
private:
+ common::GameConfig *game;
SDL_Surface *background;
const PartyLayout *monstersLayout;
const PartyLayout *heroesLayout;
- std::vector<geometry::Point<int> > monsterPositions;
- std::vector<geometry::Point<int> > heroesPositions;
+ const Resources *res;
+ AttackTypeMenu attackTypeMenu;
+ MoveMenu moveMenu;
std::vector<Monster> monsters;
- std::vector<Hero> heroes;
+ std::vector<Order> attackOrder;
+ Hero heroes[4];
+ graphics::Menu<const common::Item *> itemMenu;
+ HeroTag heroTags[4];
+ SmallHeroTag smallHeroTags[4];
+ geometry::Vector<int> heroTagPositions[4];
+ geometry::Vector<int> smallHeroTagPositions[4];
+ int numHeroes;
+ int activeHero;
+ int attackCursor;
+ int expReward;
+ int goldReward;
+ bool ranAway;
};