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"
28 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)
63 void AddMonster(const Monster &);
64 void AddHero(const Hero &);
67 virtual void EnterState(app::Application &ctrl, SDL_Surface *screen);
68 virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
69 virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
70 virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
72 virtual void Resize(int width, int height);
74 virtual void HandleEvents(const app::Input &);
75 virtual void UpdateWorld(float deltaT);
76 virtual void Render(SDL_Surface *);
79 const Resources &Res() const { return *res; }
80 AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
81 MoveMenu &GetMoveMenu() { return moveMenu; }
83 graphics::Menu<const common::Spell *> &GetSpellMenu() { return spellMenus[activeHero]; }
84 const graphics::Menu<const common::Spell *> &GetSpellMenu() const { return spellMenus[activeHero]; }
85 graphics::Menu<const common::Item *> &GetIkariMenu() { return ikariMenus[activeHero]; }
86 const graphics::Menu<const common::Item *> &GetIkariMenu() const { return ikariMenus[activeHero]; }
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; }
92 void PreviousHero() { --activeHero; }
93 void SwapHeroes(int lhs, int rhs);
94 Hero &ActiveHero() { return heroes[activeHero]; }
95 const Hero &ActiveHero() const { return heroes[activeHero]; }
97 Hero &HeroAt(int index) { return heroes[index]; }
98 const Hero &HeroAt(int index) const { return heroes[index]; }
99 Monster &MonsterAt(int index) { return monsters[index]; }
100 const Monster &MonsterAt(int index) const { return monsters[index]; }
102 const HeroTag &HeroTagAt(int index) const { return heroTags[index]; }
103 const geometry::Point<int> &HeroTagPositionAt(int index) const { return heroTagPositions[index]; }
105 bool HasChosenAttackType() const { return attackChoices[activeHero].GetType() != AttackChoice::UNDECIDED; }
106 AttackChoice &ActiveHeroAttackChoice() { return attackChoices[activeHero]; }
107 const AttackChoice &ActiveHeroAttackChoice() const { return attackChoices[activeHero]; }
108 AttackChoice &AttackChoiceAt(int index) { return attackChoices[index]; }
109 const AttackChoice &AttackChoiceAt(int index) const { return attackChoices[index]; }
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 { return attackCursor >= int(attackOrder.size()); }
133 void CalculateDamage();
135 const Order &CurrentAttack() const { return attackOrder[attackCursor]; };
136 void ClearAllAttacks();
138 bool Victory() const;
142 geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
143 return geometry::Vector<int>(
144 (screen->w - background->w) / 2,
145 (screen->h - background->h) / 2);
147 int Width() const { return background->w; }
148 int Height() const { return background->h; }
150 void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
151 void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
152 void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
153 void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
154 void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
157 void LoadSpellMenu(std::vector<Hero>::size_type heroIndex);
158 void LoadIkariMenu(std::vector<Hero>::size_type heroIndex);
159 void LoadInventory();
161 Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
164 SDL_Surface *background;
165 const PartyLayout *monstersLayout;
166 const PartyLayout *heroesLayout;
167 const Resources *res;
168 AttackTypeMenu attackTypeMenu;
170 // TODO: combine all data about heros or monsters
171 std::vector<geometry::Point<int> > monsterPositions;
172 std::vector<geometry::Point<int> > heroesPositions;
173 std::vector<Monster> monsters;
174 std::vector<AttackChoice> monsterAttacks;
175 std::vector<Order> attackOrder;
177 graphics::Menu<const common::Spell *> spellMenus[4];
178 graphics::Menu<const common::Item *> itemMenu;
179 graphics::Menu<const common::Item *> ikariMenus[4];
181 SmallHeroTag smallHeroTags[4];
182 geometry::Point<int> heroTagPositions[4];
183 geometry::Point<int> smallHeroTagPositions[4];
184 AttackChoice attackChoices[4];
194 #endif /* BATTLE_BATTLESTATE_H_ */