]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.h
added some assertions concerning BattleState::activeHero
[l2e.git] / src / battle / BattleState.h
1 /*
2  * BattleState.h
3  *
4  *  Created on: Aug 5, 2012
5  *      Author: holy
6  */
7
8 #ifndef BATTLE_BATTLESTATE_H_
9 #define BATTLE_BATTLESTATE_H_
10
11 #include "AttackTypeMenu.h"
12 #include "Hero.h"
13 #include "HeroTag.h"
14 #include "Monster.h"
15 #include "MoveMenu.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"
23
24 #include <cassert>
25 #include <vector>
26 #include <SDL.h>
27
28 namespace app { class Input; }
29 namespace common {
30         class Inventory;
31         class Item;
32         class Spell;
33 }
34 namespace graphics {
35         class Font;
36         class Frame;
37         class Gauge;
38         class Sprite;
39 }
40
41 namespace battle {
42
43 class AttackChoice;
44 class PartyLayout;
45 class Stats;
46
47 class BattleState
48 : public app::State {
49
50 public:
51         BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const Resources *res)
52         : background(background)
53         , monstersLayout(&monstersLayout)
54         , heroesLayout(&heroesLayout)
55         , res(res)
56         , attackTypeMenu(res->attackIcons)
57         , moveMenu(res->moveIcons)
58         , numHeroes(0)
59         , activeHero(-1)
60         , attackCursor(-1)
61         , expReward(0)
62         , goldReward(0)
63         , ranAway(false) { assert(background && res); }
64
65 public:
66         void AddMonster(const Monster &);
67         void AddHero(const Hero &);
68
69 public:
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);
74
75         virtual void Resize(int width, int height);
76
77         virtual void HandleEvents(const app::Input &);
78         virtual void UpdateWorld(float deltaT);
79         virtual void Render(SDL_Surface *);
80
81         // TODO: turn this mess into a well stuctured interface
82 public:
83         const Resources &Res() const { return *res; }
84         AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
85         MoveMenu &GetMoveMenu() { return moveMenu; }
86
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; }
93
94         void NextHero();
95         bool BeforeFirstHero() const { return activeHero < 0; }
96         void PreviousHero();
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]; }
100
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]; }
105
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]; }
108
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 heroes[index].GetAttackChoice(); }
113         const AttackChoice &AttackChoiceAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index].GetAttackChoice(); }
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; }
117
118         int NumHeroes() const { return numHeroes; }
119         int MaxHeroes() const { return 4; }
120         int MaxMonsters() const { return monsters.size(); }
121
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; }
126
127         void SetRunaway() { ranAway = true; }
128
129         struct Order {
130                 Order(int index, bool isMonster)
131                 : index(index), isMonster(isMonster) { }
132                 int index;
133                 bool isMonster;
134         };
135
136         void CalculateAttackOrder();
137         void NextAttack();
138         bool AttacksFinished() const;
139         void CalculateDamage();
140         void ApplyDamage();
141         const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
142         AttackChoice &CurrentAttackAttackChoice();
143         void ClearAllAttacks();
144
145         bool Victory() const;
146         bool Defeat() const;
147
148 public:
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);
153         }
154         int Width() const { return background->w; }
155         int Height() const { return background->h; }
156
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);
162
163 private:
164         void LoadInventory();
165
166         Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
167
168 private:
169         SDL_Surface *background;
170         const PartyLayout *monstersLayout;
171         const PartyLayout *heroesLayout;
172         const Resources *res;
173         AttackTypeMenu attackTypeMenu;
174         MoveMenu moveMenu;
175         // TODO: combine all data about heros or monsters
176         std::vector<geometry::Point<int> > monsterPositions;
177         std::vector<geometry::Point<int> > heroesPositions;
178         std::vector<Monster> monsters;
179         std::vector<AttackChoice> monsterAttacks;
180         std::vector<Order> attackOrder;
181         Hero heroes[4];
182         graphics::Menu<const common::Item *> itemMenu;
183         HeroTag heroTags[4];
184         SmallHeroTag smallHeroTags[4];
185         geometry::Point<int> heroTagPositions[4];
186         geometry::Point<int> smallHeroTagPositions[4];
187         int numHeroes;
188         int activeHero;
189         int attackCursor;
190         int expReward;
191         int goldReward;
192         bool ranAway;
193
194 };
195
196 }
197
198 #endif /* BATTLE_BATTLESTATE_H_ */