4 * Created on: Aug 5, 2012
8 #ifndef BATTLE_BATTLESTATE_H_
9 #define BATTLE_BATTLESTATE_H_
11 #include "AttackTypeMenu.h"
16 #include "Resources.h"
17 #include "SmallHeroTag.h"
18 #include "../app/State.h"
19 #include "../geometry/Vector.h"
20 #include "../graphics/Animation.h"
21 #include "../graphics/Menu.h"
27 namespace app { class Input; }
50 BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const Resources *res)
51 : background(background)
52 , monstersLayout(&monstersLayout)
53 , heroesLayout(&heroesLayout)
55 , attackTypeMenu(res->attackIcons)
56 , moveMenu(res->moveIcons)
62 , ranAway(false) { assert(background && res); }
65 void AddMonster(const Monster &);
66 void AddHero(const Hero &);
69 virtual void EnterState(app::Application &ctrl, SDL_Surface *screen);
70 virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
71 virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
72 virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
74 virtual void Resize(int width, int height);
76 virtual void HandleEvents(const app::Input &);
77 virtual void UpdateWorld(float deltaT);
78 virtual void Render(SDL_Surface *);
81 const Resources &Res() const { return *res; }
82 AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
83 MoveMenu &GetMoveMenu() { return moveMenu; }
85 graphics::Menu<const common::Item *> &ItemMenu() { return itemMenu; }
86 const graphics::Menu<const common::Item *> &ItemMenu() const { return itemMenu; }
89 bool BeforeFirstHero() const { return activeHero < 0; }
91 void SwapHeroes(int lhs, int rhs);
92 Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
93 const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
95 Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
96 const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
97 Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
98 const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
100 const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
101 const geometry::Vector<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
103 bool HasChosenAttackType() const { return ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
104 bool AttackSelectionDone() const { return activeHero >= numHeroes; }
106 int NumHeroes() const { return numHeroes; }
107 int MaxHeroes() const { return 4; }
108 int MaxMonsters() const { return monsters.size(); }
110 bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
111 bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
113 void SetRunaway() { ranAway = true; }
116 Order(int index, bool isMonster)
117 : index(index), isMonster(isMonster) { }
122 void CalculateAttackOrder();
124 bool AttacksFinished() const;
125 void CalculateDamage();
127 const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
128 AttackChoice &CurrentAttackAttackChoice();
129 void ClearAllAttacks();
131 bool Victory() const;
135 geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
136 return geometry::Vector<int>(
137 (screen->w - background->w) / 2,
138 (screen->h - background->h) / 2);
140 int Width() const { return background->w; }
141 int Height() const { return background->h; }
142 geometry::Vector<int> Size() const { return geometry::Vector<int>(Width(), Height()); }
144 void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
145 void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
146 void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
147 void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
148 void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
151 void LoadInventory();
153 void DecideMonsterAttack(Monster &) const;
154 void CalculateDamage(const Stats &attackerStats, TargetSelection &targets) const;
155 Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
158 SDL_Surface *background;
159 const PartyLayout *monstersLayout;
160 const PartyLayout *heroesLayout;
161 const Resources *res;
162 AttackTypeMenu attackTypeMenu;
164 std::vector<Monster> monsters;
165 std::vector<Order> attackOrder;
167 graphics::Menu<const common::Item *> itemMenu;
169 SmallHeroTag smallHeroTags[4];
170 geometry::Vector<int> heroTagPositions[4];
171 geometry::Vector<int> smallHeroTagPositions[4];
183 #endif /* BATTLE_BATTLESTATE_H_ */