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/Point.h"
20 #include "../geometry/Vector.h"
21 #include "../graphics/Animation.h"
22 #include "../graphics/Menu.h"
28 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() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero].SpellMenu(); }
88 const graphics::Menu<const common::Spell *> &GetSpellMenu() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero].SpellMenu(); }
89 graphics::Menu<const common::Item *> &GetIkariMenu() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero].IkariMenu(); }
90 const graphics::Menu<const common::Item *> &GetIkariMenu() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero].IkariMenu(); }
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; }
97 void SwapHeroes(int lhs, int rhs);
98 Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
99 const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); 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 ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
110 bool AttackSelectionDone() const { return activeHero >= numHeroes; }
112 int NumHeroes() const { return numHeroes; }
113 int MaxHeroes() const { return 4; }
114 int MaxMonsters() const { return monsters.size(); }
116 const std::vector<geometry::Point<int> > &MonsterPositions() const { return monsterPositions; }
117 bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
118 const std::vector<geometry::Point<int> > &HeroesPositions() const { return heroesPositions; }
119 bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
121 void SetRunaway() { ranAway = true; }
124 Order(int index, bool isMonster)
125 : index(index), isMonster(isMonster) { }
130 void CalculateAttackOrder();
132 bool AttacksFinished() const;
133 void CalculateDamage();
135 const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
136 AttackChoice &CurrentAttackAttackChoice();
137 void ClearAllAttacks();
139 bool Victory() const;
143 geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
144 return geometry::Vector<int>(
145 (screen->w - background->w) / 2,
146 (screen->h - background->h) / 2);
148 int Width() const { return background->w; }
149 int Height() const { return background->h; }
151 void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
152 void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
153 void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
154 void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
155 void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
158 void LoadInventory();
160 Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
163 SDL_Surface *background;
164 const PartyLayout *monstersLayout;
165 const PartyLayout *heroesLayout;
166 const Resources *res;
167 AttackTypeMenu attackTypeMenu;
169 // TODO: combine all data about heros or monsters
170 std::vector<geometry::Point<int> > monsterPositions;
171 std::vector<geometry::Point<int> > heroesPositions;
172 std::vector<Monster> monsters;
173 std::vector<Order> attackOrder;
175 graphics::Menu<const common::Item *> itemMenu;
177 SmallHeroTag smallHeroTags[4];
178 geometry::Point<int> heroTagPositions[4];
179 geometry::Point<int> smallHeroTagPositions[4];
191 #endif /* BATTLE_BATTLESTATE_H_ */