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