4 * Created on: Aug 5, 2012
8 #ifndef BATTLE_BATTLESTATE_H_
9 #define BATTLE_BATTLESTATE_H_
12 #include "AttackTypeMenu.h"
17 #include "Resources.h"
18 #include "SmallHeroTag.h"
19 #include "../app/fwd.h"
20 #include "../app/State.h"
21 #include "../common/GameConfig.h"
22 #include "../common/fwd.h"
23 #include "../common/Stats.h"
24 #include "../geometry/Vector.h"
25 #include "../graphics/Animation.h"
26 #include "../graphics/fwd.h"
27 #include "../graphics/Menu.h"
39 BattleState(common::GameConfig *game, SDL_Surface *background, const PartyLayout *monstersLayout)
41 , background(background)
42 , monstersLayout(monstersLayout)
43 , heroesLayout(game->heroesLayout)
44 , res(game->battleResources)
45 , attackTypeMenu(res->attackIcons)
46 , moveMenu(res->moveIcons)
52 , ranAway(false) { assert(background && monstersLayout && game); }
55 void AddMonster(const Monster &);
56 void AddHero(const Hero &);
59 virtual void HandleEvents(const app::Input &);
60 virtual void UpdateWorld(float deltaT);
61 virtual void Render(SDL_Surface *);
64 const Resources &Res() const { return *res; }
65 AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
66 MoveMenu &GetMoveMenu() { return moveMenu; }
68 graphics::Menu<const common::Item *> &ItemMenu() { return itemMenu; }
69 const graphics::Menu<const common::Item *> &ItemMenu() const { return itemMenu; }
72 bool BeforeFirstHero() const { return activeHero < 0; }
74 void SwapHeroes(int lhs, int rhs);
75 Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
76 const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
78 Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
79 const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
80 Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
81 const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
83 const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
84 const geometry::Vector<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
86 bool HasChosenAttackType() const { return ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
87 bool AttackSelectionDone() const { return activeHero >= numHeroes; }
89 int NumHeroes() const { return numHeroes; }
90 int MaxHeroes() const { return 4; }
91 int MaxMonsters() const { return monsters.size(); }
93 bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
94 bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
96 void SetRunaway() { ranAway = true; }
99 Order(int index, bool isMonster)
100 : index(index), isMonster(isMonster) { }
105 void CalculateAttackOrder();
107 bool AttacksFinished() const;
108 void CalculateDamage();
110 const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
111 AttackChoice &CurrentAttackAttackChoice();
112 void ClearAllAttacks();
114 bool Victory() const;
118 geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
119 return geometry::Vector<int>(
120 (screen->w - background->w) / 2,
121 (screen->h - background->h) / 2);
123 int Width() const { return background->w; }
124 int Height() const { return background->h; }
125 geometry::Vector<int> Size() const { return geometry::Vector<int>(Width(), Height()); }
127 void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
128 void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
129 void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
130 void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
131 void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
134 virtual void OnEnterState(SDL_Surface *screen);
135 virtual void OnExitState(SDL_Surface *screen);
136 virtual void OnResumeState(SDL_Surface *screen);
137 virtual void OnPauseState(SDL_Surface *screen);
139 virtual void OnResize(int width, int height);
142 void LoadInventory();
144 void DecideMonsterAttack(Monster &) const;
145 void CalculateDamage(const common::Stats &attackerStats, TargetSelection &targets) const;
146 Uint16 CalculateDamage(const common::Stats &attacker, const common::Stats &defender) const;
149 common::GameConfig *game;
150 SDL_Surface *background;
151 const PartyLayout *monstersLayout;
152 const PartyLayout *heroesLayout;
153 const Resources *res;
154 AttackTypeMenu attackTypeMenu;
156 std::vector<Monster> monsters;
157 std::vector<Order> attackOrder;
159 graphics::Menu<const common::Item *> itemMenu;
161 SmallHeroTag smallHeroTags[4];
162 geometry::Vector<int> heroTagPositions[4];
163 geometry::Vector<int> smallHeroTagPositions[4];
175 #endif /* BATTLE_BATTLESTATE_H_ */