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