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