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/fwd.h"
22 #include "../common/Stats.h"
23 #include "../geometry/Vector.h"
24 #include "../graphics/Animation.h"
25 #include "../graphics/fwd.h"
26 #include "../graphics/Menu.h"
38 BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const Resources *res)
39 : background(background)
40 , monstersLayout(&monstersLayout)
41 , heroesLayout(&heroesLayout)
43 , attackTypeMenu(res->attackIcons)
44 , moveMenu(res->moveIcons)
50 , ranAway(false) { assert(background && res); }
53 void AddMonster(const Monster &);
54 void AddHero(const Hero &);
57 virtual void EnterState(app::Application &ctrl, SDL_Surface *screen);
58 virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
59 virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
60 virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
62 virtual void Resize(int width, int height);
64 virtual void HandleEvents(const app::Input &);
65 virtual void UpdateWorld(float deltaT);
66 virtual void Render(SDL_Surface *);
69 const Resources &Res() const { return *res; }
70 AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
71 MoveMenu &GetMoveMenu() { return moveMenu; }
73 graphics::Menu<const common::Item *> &ItemMenu() { return itemMenu; }
74 const graphics::Menu<const common::Item *> &ItemMenu() const { return itemMenu; }
77 bool BeforeFirstHero() const { return activeHero < 0; }
79 void SwapHeroes(int lhs, int rhs);
80 Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
81 const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
83 Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
84 const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
85 Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
86 const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
88 const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
89 const geometry::Vector<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
91 bool HasChosenAttackType() const { return ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
92 bool AttackSelectionDone() const { return activeHero >= numHeroes; }
94 int NumHeroes() const { return numHeroes; }
95 int MaxHeroes() const { return 4; }
96 int MaxMonsters() const { return monsters.size(); }
98 bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
99 bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
101 void SetRunaway() { ranAway = true; }
104 Order(int index, bool isMonster)
105 : index(index), isMonster(isMonster) { }
110 void CalculateAttackOrder();
112 bool AttacksFinished() const;
113 void CalculateDamage();
115 const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
116 AttackChoice &CurrentAttackAttackChoice();
117 void ClearAllAttacks();
119 bool Victory() const;
123 geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
124 return geometry::Vector<int>(
125 (screen->w - background->w) / 2,
126 (screen->h - background->h) / 2);
128 int Width() const { return background->w; }
129 int Height() const { return background->h; }
130 geometry::Vector<int> Size() const { return geometry::Vector<int>(Width(), Height()); }
132 void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
133 void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
134 void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
135 void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
136 void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
139 void LoadInventory();
141 void DecideMonsterAttack(Monster &) const;
142 void CalculateDamage(const common::Stats &attackerStats, TargetSelection &targets) const;
143 Uint16 CalculateDamage(const common::Stats &attacker, const common::Stats &defender) const;
146 SDL_Surface *background;
147 const PartyLayout *monstersLayout;
148 const PartyLayout *heroesLayout;
149 const Resources *res;
150 AttackTypeMenu attackTypeMenu;
152 std::vector<Monster> monsters;
153 std::vector<Order> attackOrder;
155 graphics::Menu<const common::Item *> itemMenu;
157 SmallHeroTag smallHeroTags[4];
158 geometry::Vector<int> heroTagPositions[4];
159 geometry::Vector<int> smallHeroTagPositions[4];
171 #endif /* BATTLE_BATTLESTATE_H_ */