]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.h
removed redundant BattleState::ActiveHeroAttackChoice()
[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 ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
110         AttackChoice &AttackChoiceAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index].GetAttackChoice(); }
111         const AttackChoice &AttackChoiceAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index].GetAttackChoice(); }
112         AttackChoice &MonsterAttackChoiceAt(int index) { assert(index >= 0 && index < MaxMonsters()); return monsterAttacks[index]; }
113         const AttackChoice &MonsterAttackChoiceAt(int index) const { assert(index >= 0 && index < MaxMonsters()); return monsterAttacks[index]; }
114         bool AttackSelectionDone() const { return activeHero >= numHeroes; }
115
116         int NumHeroes() const { return numHeroes; }
117         int MaxHeroes() const { return 4; }
118         int MaxMonsters() const { return monsters.size(); }
119
120         const std::vector<geometry::Point<int> > &MonsterPositions() const { return monsterPositions; }
121         bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
122         const std::vector<geometry::Point<int> > &HeroesPositions() const { return heroesPositions; }
123         bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
124
125         void SetRunaway() { ranAway = true; }
126
127         struct Order {
128                 Order(int index, bool isMonster)
129                 : index(index), isMonster(isMonster) { }
130                 int index;
131                 bool isMonster;
132         };
133
134         void CalculateAttackOrder();
135         void NextAttack();
136         bool AttacksFinished() const;
137         void CalculateDamage();
138         void ApplyDamage();
139         const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
140         AttackChoice &CurrentAttackAttackChoice();
141         void ClearAllAttacks();
142
143         bool Victory() const;
144         bool Defeat() const;
145
146 public:
147         geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
148                 return geometry::Vector<int>(
149                                 (screen->w - background->w) / 2,
150                                 (screen->h - background->h) / 2);
151         }
152         int Width() const { return background->w; }
153         int Height() const { return background->h; }
154
155         void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
156         void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
157         void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
158         void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
159         void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
160
161 private:
162         void LoadInventory();
163
164         Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
165
166 private:
167         SDL_Surface *background;
168         const PartyLayout *monstersLayout;
169         const PartyLayout *heroesLayout;
170         const Resources *res;
171         AttackTypeMenu attackTypeMenu;
172         MoveMenu moveMenu;
173         // TODO: combine all data about heros or monsters
174         std::vector<geometry::Point<int> > monsterPositions;
175         std::vector<geometry::Point<int> > heroesPositions;
176         std::vector<Monster> monsters;
177         std::vector<AttackChoice> monsterAttacks;
178         std::vector<Order> attackOrder;
179         Hero heroes[4];
180         graphics::Menu<const common::Item *> itemMenu;
181         HeroTag heroTags[4];
182         SmallHeroTag smallHeroTags[4];
183         geometry::Point<int> heroTagPositions[4];
184         geometry::Point<int> smallHeroTagPositions[4];
185         int numHeroes;
186         int activeHero;
187         int attackCursor;
188         int expReward;
189         int goldReward;
190         bool ranAway;
191
192 };
193
194 }
195
196 #endif /* BATTLE_BATTLESTATE_H_ */