1 #ifndef BATTLE_BATTLESTATE_H_
2 #define BATTLE_BATTLESTATE_H_
16 #include "AttackTypeMenu.h"
22 #include "Resources.h"
23 #include "SmallHeroTag.h"
24 #include "../app/State.h"
25 #include "../common/GameConfig.h"
26 #include "../common/Stats.h"
27 #include "../graphics/Animation.h"
28 #include "../graphics/Menu.h"
40 BattleState(common::GameConfig *game, SDL_Surface *background, const PartyLayout *monstersLayout)
42 , background(background)
43 , monstersLayout(monstersLayout)
44 , heroesLayout(game->heroesLayout)
45 , res(game->battleResources)
46 , attackTypeMenu(res->attackIcons)
47 , moveMenu(res->moveIcons)
53 , ranAway(false) { assert(background && monstersLayout && game); }
56 void AddMonster(const Monster &);
57 void AddHero(const Hero &);
58 void SetCapsule(const Capsule &);
61 virtual void HandleEvents(const app::Input &);
62 virtual void UpdateWorld(Uint32 deltaT);
63 virtual void Render(SDL_Surface *);
66 const Resources &Res() const { return *res; }
67 AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
68 MoveMenu &GetMoveMenu() { return moveMenu; }
70 graphics::Menu<const common::Item *> &ItemMenu() { return itemMenu; }
71 const graphics::Menu<const common::Item *> &ItemMenu() const { return itemMenu; }
74 bool BeforeFirstHero() const { return activeHero < 0; }
76 void SwapHeroes(int lhs, int rhs);
77 Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
78 const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
80 Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
81 const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
82 Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
83 const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
85 const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
86 const math::Vector<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
88 Capsule &GetCapsule() { return capsule; }
90 bool HasChosenAttackType() const { return ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
91 bool AttackSelectionDone() const { return activeHero >= numHeroes; }
93 int NumHeroes() const { return numHeroes; }
94 int MaxHeroes() const { return 4; }
95 int MaxMonsters() const { return monsters.size(); }
97 bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
98 bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
100 void SetRunaway() { ranAway = true; }
108 Order(Performer by, int index = 0)
109 : index(index), by(by) { }
110 AttackChoice &GetAttackChoice(BattleState &) const;
111 common::Stats &GetStats(BattleState &) const;
112 bool IsHero() const { return by == HERO; }
113 bool IsCapsule() const { return by == CAPSULE; }
114 bool IsMonster() const { return by == MONSTER; }
119 void CalculateAttackOrder();
121 bool AttacksFinished() const;
122 void CalculateDamage();
124 const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
125 AttackChoice &CurrentAttackAttackChoice();
126 void ClearAllAttacks();
128 bool Victory() const;
132 math::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
133 return math::Vector<int>(
134 (screen->w - background->w) / 2,
135 (screen->h - background->h) / 2);
137 int Width() const { return background->w; }
138 int Height() const { return background->h; }
139 math::Vector<int> Size() const { return math::Vector<int>(Width(), Height()); }
141 void RenderBackground(SDL_Surface *screen, const math::Vector<int> &offset);
142 void RenderMonsters(SDL_Surface *screen, const math::Vector<int> &offset);
143 void RenderHeroes(SDL_Surface *screen, const math::Vector<int> &offset);
144 void RenderCapsule(SDL_Surface *screen, const math::Vector<int> &offset);
145 void RenderHeroTags(SDL_Surface *screen, const math::Vector<int> &offset);
146 void RenderSmallHeroTags(SDL_Surface *screen, const math::Vector<int> &offset);
149 virtual void OnEnterState(SDL_Surface *screen);
150 virtual void OnExitState(SDL_Surface *screen);
151 virtual void OnResumeState(SDL_Surface *screen);
152 virtual void OnPauseState(SDL_Surface *screen);
154 virtual void OnResize(int width, int height);
157 void LoadInventory();
159 void DecideMonsterAttack(Monster &);
160 void DecideCapsuleAttack();
161 void CalculateDamage(const common::Stats &attackerStats, TargetSelection &targets) const;
162 Uint16 CalculateDamage(const common::Stats &attacker, const common::Stats &defender) const;
165 common::GameConfig *game;
166 SDL_Surface *background;
167 const PartyLayout *monstersLayout;
168 const PartyLayout *heroesLayout;
169 const Resources *res;
170 AttackTypeMenu attackTypeMenu;
172 std::vector<Monster> monsters;
173 std::vector<Order> attackOrder;
175 graphics::Menu<const common::Item *> itemMenu;
177 SmallHeroTag smallHeroTags[4];
178 math::Vector<int> heroTagPositions[4];
179 math::Vector<int> smallHeroTagPositions[4];