1 #ifndef BATTLE_BATTLESTATE_H_
2 #define BATTLE_BATTLESTATE_H_
5 #include "AttackTypeMenu.h"
11 #include "Resources.h"
12 #include "SmallHeroTag.h"
13 #include "../app/fwd.h"
14 #include "../app/State.h"
15 #include "../common/GameConfig.h"
16 #include "../common/fwd.h"
17 #include "../common/Stats.h"
18 #include "../geometry/Vector.h"
19 #include "../graphics/Animation.h"
20 #include "../graphics/fwd.h"
21 #include "../graphics/Menu.h"
33 BattleState(common::GameConfig *game, SDL_Surface *background, const PartyLayout *monstersLayout)
35 , background(background)
36 , monstersLayout(monstersLayout)
37 , heroesLayout(game->heroesLayout)
38 , res(game->battleResources)
39 , attackTypeMenu(res->attackIcons)
40 , moveMenu(res->moveIcons)
46 , ranAway(false) { assert(background && monstersLayout && game); }
49 void AddMonster(const Monster &);
50 void AddHero(const Hero &);
51 void SetCapsule(const Capsule &);
54 virtual void HandleEvents(const app::Input &);
55 virtual void UpdateWorld(float deltaT);
56 virtual void Render(SDL_Surface *);
59 const Resources &Res() const { return *res; }
60 AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
61 MoveMenu &GetMoveMenu() { return moveMenu; }
63 graphics::Menu<const common::Item *> &ItemMenu() { return itemMenu; }
64 const graphics::Menu<const common::Item *> &ItemMenu() const { return itemMenu; }
67 bool BeforeFirstHero() const { return activeHero < 0; }
69 void SwapHeroes(int lhs, int rhs);
70 Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
71 const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
73 Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
74 const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
75 Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
76 const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
78 const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
79 const geometry::Vector<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
81 Capsule &GetCapsule() { return capsule; }
83 bool HasChosenAttackType() const { return ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
84 bool AttackSelectionDone() const { return activeHero >= numHeroes; }
86 int NumHeroes() const { return numHeroes; }
87 int MaxHeroes() const { return 4; }
88 int MaxMonsters() const { return monsters.size(); }
90 bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
91 bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
93 void SetRunaway() { ranAway = true; }
101 Order(Performer by, int index = 0)
102 : index(index), by(by) { }
103 AttackChoice &GetAttackChoice(BattleState &) const;
104 common::Stats &GetStats(BattleState &) const;
105 bool IsHero() const { return by == HERO; }
106 bool IsCapsule() const { return by == CAPSULE; }
107 bool IsMonster() const { return by == MONSTER; }
112 void CalculateAttackOrder();
114 bool AttacksFinished() const;
115 void CalculateDamage();
117 const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
118 AttackChoice &CurrentAttackAttackChoice();
119 void ClearAllAttacks();
121 bool Victory() const;
125 geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
126 return geometry::Vector<int>(
127 (screen->w - background->w) / 2,
128 (screen->h - background->h) / 2);
130 int Width() const { return background->w; }
131 int Height() const { return background->h; }
132 geometry::Vector<int> Size() const { return geometry::Vector<int>(Width(), Height()); }
134 void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
135 void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
136 void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
137 void RenderCapsule(SDL_Surface *screen, const geometry::Vector<int> &offset);
138 void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
139 void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
142 virtual void OnEnterState(SDL_Surface *screen);
143 virtual void OnExitState(SDL_Surface *screen);
144 virtual void OnResumeState(SDL_Surface *screen);
145 virtual void OnPauseState(SDL_Surface *screen);
147 virtual void OnResize(int width, int height);
150 void LoadInventory();
152 void DecideMonsterAttack(Monster &);
153 void DecideCapsuleAttack();
154 void CalculateDamage(const common::Stats &attackerStats, TargetSelection &targets) const;
155 Uint16 CalculateDamage(const common::Stats &attacker, const common::Stats &defender) const;
158 common::GameConfig *game;
159 SDL_Surface *background;
160 const PartyLayout *monstersLayout;
161 const PartyLayout *heroesLayout;
162 const Resources *res;
163 AttackTypeMenu attackTypeMenu;
165 std::vector<Monster> monsters;
166 std::vector<Order> attackOrder;
168 graphics::Menu<const common::Item *> itemMenu;
170 SmallHeroTag smallHeroTags[4];
171 geometry::Vector<int> heroTagPositions[4];
172 geometry::Vector<int> smallHeroTagPositions[4];
185 #endif /* BATTLE_BATTLESTATE_H_ */