]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.h
added capsule attack/animation handling
[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         Capsule &GetCapsule() { return capsule; }
82
83         bool HasChosenAttackType() const { return ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
84         bool AttackSelectionDone() const { return activeHero >= numHeroes; }
85
86         int NumHeroes() const { return numHeroes; }
87         int MaxHeroes() const { return 4; }
88         int MaxMonsters() const { return monsters.size(); }
89
90         bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
91         bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
92
93         void SetRunaway() { ranAway = true; }
94
95         struct Order {
96                 enum Performer {
97                         HERO,
98                         CAPSULE,
99                         MONSTER,
100                 };
101                 Order(Performer by, int index = 0)
102                 : index(index), by(by) { }
103                 AttackChoice &GetAttackChoice(BattleState &) const;
104                 common::Stats &GetStats(BattleState &) const;
105                 bool IsHero() const { return by == HERO; }
106                 bool IsCapsule() const { return by == CAPSULE; }
107                 bool IsMonster() const { return by == MONSTER; }
108                 int index;
109                 Performer by;
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 RenderCapsule(SDL_Surface *screen, const geometry::Vector<int> &offset);
138         void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
139         void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
140
141 private:
142         virtual void OnEnterState(SDL_Surface *screen);
143         virtual void OnExitState(SDL_Surface *screen);
144         virtual void OnResumeState(SDL_Surface *screen);
145         virtual void OnPauseState(SDL_Surface *screen);
146
147         virtual void OnResize(int width, int height);
148
149 private:
150         void LoadInventory();
151
152         void DecideMonsterAttack(Monster &);
153         void DecideCapsuleAttack();
154         void CalculateDamage(const common::Stats &attackerStats, TargetSelection &targets) const;
155         Uint16 CalculateDamage(const common::Stats &attacker, const common::Stats &defender) const;
156
157 private:
158         common::GameConfig *game;
159         SDL_Surface *background;
160         const PartyLayout *monstersLayout;
161         const PartyLayout *heroesLayout;
162         const Resources *res;
163         AttackTypeMenu attackTypeMenu;
164         MoveMenu moveMenu;
165         std::vector<Monster> monsters;
166         std::vector<Order> attackOrder;
167         Hero heroes[4];
168         graphics::Menu<const common::Item *> itemMenu;
169         HeroTag heroTags[4];
170         SmallHeroTag smallHeroTags[4];
171         geometry::Vector<int> heroTagPositions[4];
172         geometry::Vector<int> smallHeroTagPositions[4];
173         Capsule capsule;
174         int numHeroes;
175         int activeHero;
176         int attackCursor;
177         int expReward;
178         int goldReward;
179         bool ranAway;
180
181 };
182
183 }
184
185 #endif /* BATTLE_BATTLESTATE_H_ */