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 EnterState(app::Application &ctrl, SDL_Surface *screen);
60 virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
61 virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
62 virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
64 virtual void Resize(int width, int height);
66 virtual void HandleEvents(const app::Input &);
67 virtual void UpdateWorld(float deltaT);
68 virtual void Render(SDL_Surface *);
71 const Resources &Res() const { return *res; }
72 AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
73 MoveMenu &GetMoveMenu() { return moveMenu; }
75 graphics::Menu<const common::Item *> &ItemMenu() { return itemMenu; }
76 const graphics::Menu<const common::Item *> &ItemMenu() const { return itemMenu; }
79 bool BeforeFirstHero() const { return activeHero < 0; }
81 void SwapHeroes(int lhs, int rhs);
82 Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
83 const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
85 Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
86 const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
87 Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
88 const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
90 const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
91 const geometry::Vector<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
93 bool HasChosenAttackType() const { return ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
94 bool AttackSelectionDone() const { return activeHero >= numHeroes; }
96 int NumHeroes() const { return numHeroes; }
97 int MaxHeroes() const { return 4; }
98 int MaxMonsters() const { return monsters.size(); }
100 bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
101 bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
103 void SetRunaway() { ranAway = true; }
106 Order(int index, bool isMonster)
107 : index(index), isMonster(isMonster) { }
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 RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
138 void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
141 void LoadInventory();
143 void DecideMonsterAttack(Monster &) const;
144 void CalculateDamage(const common::Stats &attackerStats, TargetSelection &targets) const;
145 Uint16 CalculateDamage(const common::Stats &attacker, const common::Stats &defender) const;
148 common::GameConfig *game;
149 SDL_Surface *background;
150 const PartyLayout *monstersLayout;
151 const PartyLayout *heroesLayout;
152 const Resources *res;
153 AttackTypeMenu attackTypeMenu;
155 std::vector<Monster> monsters;
156 std::vector<Order> attackOrder;
158 graphics::Menu<const common::Item *> itemMenu;
160 SmallHeroTag smallHeroTags[4];
161 geometry::Vector<int> heroTagPositions[4];
162 geometry::Vector<int> smallHeroTagPositions[4];
174 #endif /* BATTLE_BATTLESTATE_H_ */