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::Item *> &GetItemMenu() { return itemMenu; }
88 const graphics::Menu<const common::Item *> &GetItemMenu() const { return itemMenu; }
91 bool BeforeFirstHero() const { return activeHero < 0; }
93 void SwapHeroes(int lhs, int rhs);
94 Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
95 const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
97 Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
98 const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
99 Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
100 const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
102 const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
103 const geometry::Point<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
105 bool HasChosenAttackType() const { return ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
106 bool AttackSelectionDone() const { return activeHero >= numHeroes; }
108 int NumHeroes() const { return numHeroes; }
109 int MaxHeroes() const { return 4; }
110 int MaxMonsters() const { return monsters.size(); }
112 const std::vector<geometry::Point<int> > &MonsterPositions() const { return monsterPositions; }
113 bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
114 const std::vector<geometry::Point<int> > &HeroesPositions() const { return heroesPositions; }
115 bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
117 void SetRunaway() { ranAway = true; }
120 Order(int index, bool isMonster)
121 : index(index), isMonster(isMonster) { }
126 void CalculateAttackOrder();
128 bool AttacksFinished() const;
129 void CalculateDamage();
131 const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
132 AttackChoice &CurrentAttackAttackChoice();
133 void ClearAllAttacks();
135 bool Victory() const;
139 geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
140 return geometry::Vector<int>(
141 (screen->w - background->w) / 2,
142 (screen->h - background->h) / 2);
144 int Width() const { return background->w; }
145 int Height() const { return background->h; }
147 void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
148 void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
149 void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
150 void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
151 void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
154 void LoadInventory();
156 Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
159 SDL_Surface *background;
160 const PartyLayout *monstersLayout;
161 const PartyLayout *heroesLayout;
162 const Resources *res;
163 AttackTypeMenu attackTypeMenu;
165 // TODO: combine all data about heros or monsters
166 std::vector<geometry::Point<int> > monsterPositions;
167 std::vector<geometry::Point<int> > heroesPositions;
168 std::vector<Monster> monsters;
169 std::vector<Order> attackOrder;
171 graphics::Menu<const common::Item *> itemMenu;
173 SmallHeroTag smallHeroTags[4];
174 geometry::Point<int> heroTagPositions[4];
175 geometry::Point<int> smallHeroTagPositions[4];
187 #endif /* BATTLE_BATTLESTATE_H_ */