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