4 * Created on: Aug 5, 2012
8 #ifndef BATTLE_BATTLESTATE_H_
9 #define BATTLE_BATTLESTATE_H_
11 #include "AttackChoice.h"
12 #include "AttackTypeMenu.h"
17 #include "Resources.h"
18 #include "SmallHeroTag.h"
19 #include "../app/State.h"
20 #include "../geometry/Point.h"
21 #include "../geometry/Vector.h"
22 #include "../graphics/Animation.h"
23 #include "../graphics/Menu.h"
29 namespace app { class Input; }
51 BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const Resources *res)
52 : background(background)
53 , monstersLayout(&monstersLayout)
54 , heroesLayout(&heroesLayout)
56 , attackTypeMenu(res->attackIcons)
57 , moveMenu(res->moveIcons)
63 , ranAway(false) { assert(background && res); }
66 void AddMonster(const Monster &);
67 void AddHero(const Hero &);
70 virtual void EnterState(app::Application &ctrl, SDL_Surface *screen);
71 virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
72 virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
73 virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
75 virtual void Resize(int width, int height);
77 virtual void HandleEvents(const app::Input &);
78 virtual void UpdateWorld(float deltaT);
79 virtual void Render(SDL_Surface *);
81 // TODO: turn this mess into a well stuctured interface
83 const Resources &Res() const { return *res; }
84 AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
85 MoveMenu &GetMoveMenu() { return moveMenu; }
87 graphics::Menu<const common::Spell *> &GetSpellMenu() { return spellMenus[activeHero]; }
88 const graphics::Menu<const common::Spell *> &GetSpellMenu() const { return spellMenus[activeHero]; }
89 graphics::Menu<const common::Item *> &GetIkariMenu() { return ikariMenus[activeHero]; }
90 const graphics::Menu<const common::Item *> &GetIkariMenu() const { return ikariMenus[activeHero]; }
91 graphics::Menu<const common::Item *> &GetItemMenu() { return itemMenu; }
92 const graphics::Menu<const common::Item *> &GetItemMenu() const { return itemMenu; }
95 bool BeforeFirstHero() const { return activeHero < 0; }
96 void PreviousHero() { --activeHero; }
97 void SwapHeroes(int lhs, int rhs);
98 Hero &ActiveHero() { return heroes[activeHero]; }
99 const Hero &ActiveHero() const { return heroes[activeHero]; }
101 Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
102 const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
103 Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
104 const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
106 const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
107 const geometry::Point<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
109 bool HasChosenAttackType() const { return ActiveHeroAttackChoice().GetType() != AttackChoice::UNDECIDED; }
110 AttackChoice &ActiveHeroAttackChoice() { return AttackChoiceAt(activeHero); }
111 const AttackChoice &ActiveHeroAttackChoice() const { return AttackChoiceAt(activeHero); }
112 AttackChoice &AttackChoiceAt(int index) { assert(index >= 0 && index < NumHeroes()); return attackChoices[index]; }
113 const AttackChoice &AttackChoiceAt(int index) const { assert(index >= 0 && index < NumHeroes()); return attackChoices[index]; }
114 AttackChoice &MonsterAttackChoiceAt(int index) { assert(index >= 0 && index < MaxMonsters()); return monsterAttacks[index]; }
115 const AttackChoice &MonsterAttackChoiceAt(int index) const { assert(index >= 0 && index < MaxMonsters()); return monsterAttacks[index]; }
116 bool AttackSelectionDone() const { return activeHero >= numHeroes; }
118 int NumHeroes() const { return numHeroes; }
119 int MaxHeroes() const { return 4; }
120 int MaxMonsters() const { return monsters.size(); }
122 const std::vector<geometry::Point<int> > &MonsterPositions() const { return monsterPositions; }
123 bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
124 const std::vector<geometry::Point<int> > &HeroesPositions() const { return heroesPositions; }
125 bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
127 void SetRunaway() { ranAway = true; }
130 Order(int index, bool isMonster)
131 : index(index), isMonster(isMonster) { }
136 void CalculateAttackOrder();
138 bool AttacksFinished() const;
139 void CalculateDamage();
141 const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
142 AttackChoice &CurrentAttackAttackChoice();
143 void ClearAllAttacks();
145 bool Victory() const;
149 geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
150 return geometry::Vector<int>(
151 (screen->w - background->w) / 2,
152 (screen->h - background->h) / 2);
154 int Width() const { return background->w; }
155 int Height() const { return background->h; }
157 void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
158 void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
159 void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
160 void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
161 void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
164 void LoadSpellMenu(std::vector<Hero>::size_type heroIndex);
165 void LoadIkariMenu(std::vector<Hero>::size_type heroIndex);
166 void LoadInventory();
168 Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
171 SDL_Surface *background;
172 const PartyLayout *monstersLayout;
173 const PartyLayout *heroesLayout;
174 const Resources *res;
175 AttackTypeMenu attackTypeMenu;
177 // TODO: combine all data about heros or monsters
178 std::vector<geometry::Point<int> > monsterPositions;
179 std::vector<geometry::Point<int> > heroesPositions;
180 std::vector<Monster> monsters;
181 std::vector<AttackChoice> monsterAttacks;
182 std::vector<Order> attackOrder;
184 graphics::Menu<const common::Spell *> spellMenus[4];
185 graphics::Menu<const common::Item *> itemMenu;
186 graphics::Menu<const common::Item *> ikariMenus[4];
188 SmallHeroTag smallHeroTags[4];
189 geometry::Point<int> heroTagPositions[4];
190 geometry::Point<int> smallHeroTagPositions[4];
191 AttackChoice attackChoices[4];
203 #endif /* BATTLE_BATTLESTATE_H_ */