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 "../geometry/Vector.h"
23 #include "../graphics/Animation.h"
24 #include "../graphics/fwd.h"
25 #include "../graphics/Menu.h"
37 BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const Resources *res)
38 : background(background)
39 , monstersLayout(&monstersLayout)
40 , heroesLayout(&heroesLayout)
42 , attackTypeMenu(res->attackIcons)
43 , moveMenu(res->moveIcons)
49 , ranAway(false) { assert(background && res); }
52 void AddMonster(const Monster &);
53 void AddHero(const Hero &);
56 virtual void EnterState(app::Application &ctrl, SDL_Surface *screen);
57 virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
58 virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
59 virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
61 virtual void Resize(int width, int height);
63 virtual void HandleEvents(const app::Input &);
64 virtual void UpdateWorld(float deltaT);
65 virtual void Render(SDL_Surface *);
68 const Resources &Res() const { return *res; }
69 AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
70 MoveMenu &GetMoveMenu() { return moveMenu; }
72 graphics::Menu<const common::Item *> &ItemMenu() { return itemMenu; }
73 const graphics::Menu<const common::Item *> &ItemMenu() const { return itemMenu; }
76 bool BeforeFirstHero() const { return activeHero < 0; }
78 void SwapHeroes(int lhs, int rhs);
79 Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
80 const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
82 Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
83 const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
84 Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
85 const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
87 const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
88 const geometry::Vector<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
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; }
103 Order(int index, bool isMonster)
104 : index(index), isMonster(isMonster) { }
109 void CalculateAttackOrder();
111 bool AttacksFinished() const;
112 void CalculateDamage();
114 const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
115 AttackChoice &CurrentAttackAttackChoice();
116 void ClearAllAttacks();
118 bool Victory() const;
122 geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
123 return geometry::Vector<int>(
124 (screen->w - background->w) / 2,
125 (screen->h - background->h) / 2);
127 int Width() const { return background->w; }
128 int Height() const { return background->h; }
129 geometry::Vector<int> Size() const { return geometry::Vector<int>(Width(), Height()); }
131 void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
132 void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
133 void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
134 void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
135 void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
138 void LoadInventory();
140 void DecideMonsterAttack(Monster &) const;
141 void CalculateDamage(const Stats &attackerStats, TargetSelection &targets) const;
142 Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
145 SDL_Surface *background;
146 const PartyLayout *monstersLayout;
147 const PartyLayout *heroesLayout;
148 const Resources *res;
149 AttackTypeMenu attackTypeMenu;
151 std::vector<Monster> monsters;
152 std::vector<Order> attackOrder;
154 graphics::Menu<const common::Item *> itemMenu;
156 SmallHeroTag smallHeroTags[4];
157 geometry::Vector<int> heroTagPositions[4];
158 geometry::Vector<int> smallHeroTagPositions[4];
170 #endif /* BATTLE_BATTLESTATE_H_ */