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