]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.h
added forwarding headers
[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 "fwd.h"
12 #include "AttackTypeMenu.h"
13 #include "Hero.h"
14 #include "HeroTag.h"
15 #include "Monster.h"
16 #include "MoveMenu.h"
17 #include "Resources.h"
18 #include "SmallHeroTag.h"
19 #include "../app/fwd.h"
20 #include "../app/State.h"
21 #include "../common/fwd.h"
22 #include "../geometry/Vector.h"
23 #include "../graphics/Animation.h"
24 #include "../graphics/fwd.h"
25 #include "../graphics/Menu.h"
26
27 #include <cassert>
28 #include <vector>
29 #include <SDL.h>
30
31 namespace battle {
32
33 class BattleState
34 : public app::State {
35
36 public:
37         BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const Resources *res)
38         : background(background)
39         , monstersLayout(&monstersLayout)
40         , heroesLayout(&heroesLayout)
41         , res(res)
42         , attackTypeMenu(res->attackIcons)
43         , moveMenu(res->moveIcons)
44         , numHeroes(0)
45         , activeHero(-1)
46         , attackCursor(-1)
47         , expReward(0)
48         , goldReward(0)
49         , ranAway(false) { assert(background && res); }
50
51 public:
52         void AddMonster(const Monster &);
53         void AddHero(const Hero &);
54
55 public:
56         virtual void EnterState(app::Application &ctrl, SDL_Surface *screen);
57         virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
58         virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
59         virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
60
61         virtual void Resize(int width, int height);
62
63         virtual void HandleEvents(const app::Input &);
64         virtual void UpdateWorld(float deltaT);
65         virtual void Render(SDL_Surface *);
66
67 public:
68         const Resources &Res() const { return *res; }
69         AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
70         MoveMenu &GetMoveMenu() { return moveMenu; }
71
72         graphics::Menu<const common::Item *> &ItemMenu() { return itemMenu; }
73         const graphics::Menu<const common::Item *> &ItemMenu() const { return itemMenu; }
74
75         void NextHero();
76         bool BeforeFirstHero() const { return activeHero < 0; }
77         void PreviousHero();
78         void SwapHeroes(int lhs, int rhs);
79         Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
80         const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
81
82         Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
83         const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
84         Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
85         const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
86
87         const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
88         const geometry::Vector<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
89
90         bool HasChosenAttackType() const { return ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
91         bool AttackSelectionDone() const { return activeHero >= numHeroes; }
92
93         int NumHeroes() const { return numHeroes; }
94         int MaxHeroes() const { return 4; }
95         int MaxMonsters() const { return monsters.size(); }
96
97         bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
98         bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
99
100         void SetRunaway() { ranAway = true; }
101
102         struct Order {
103                 Order(int index, bool isMonster)
104                 : index(index), isMonster(isMonster) { }
105                 int index;
106                 bool isMonster;
107         };
108
109         void CalculateAttackOrder();
110         void NextAttack();
111         bool AttacksFinished() const;
112         void CalculateDamage();
113         void ApplyDamage();
114         const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
115         AttackChoice &CurrentAttackAttackChoice();
116         void ClearAllAttacks();
117
118         bool Victory() const;
119         bool Defeat() const;
120
121 public:
122         geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
123                 return geometry::Vector<int>(
124                                 (screen->w - background->w) / 2,
125                                 (screen->h - background->h) / 2);
126         }
127         int Width() const { return background->w; }
128         int Height() const { return background->h; }
129         geometry::Vector<int> Size() const { return geometry::Vector<int>(Width(), Height()); }
130
131         void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
132         void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
133         void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
134         void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
135         void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
136
137 private:
138         void LoadInventory();
139
140         void DecideMonsterAttack(Monster &) const;
141         void CalculateDamage(const Stats &attackerStats, TargetSelection &targets) const;
142         Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
143
144 private:
145         SDL_Surface *background;
146         const PartyLayout *monstersLayout;
147         const PartyLayout *heroesLayout;
148         const Resources *res;
149         AttackTypeMenu attackTypeMenu;
150         MoveMenu moveMenu;
151         std::vector<Monster> monsters;
152         std::vector<Order> attackOrder;
153         Hero heroes[4];
154         graphics::Menu<const common::Item *> itemMenu;
155         HeroTag heroTags[4];
156         SmallHeroTag smallHeroTags[4];
157         geometry::Vector<int> heroTagPositions[4];
158         geometry::Vector<int> smallHeroTagPositions[4];
159         int numHeroes;
160         int activeHero;
161         int attackCursor;
162         int expReward;
163         int goldReward;
164         bool ranAway;
165
166 };
167
168 }
169
170 #endif /* BATTLE_BATTLESTATE_H_ */