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