]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.h
e94ed417b689807e580bf20a6925d0371327ef59
[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 "AttackChoice.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 "../app/State.h"
19 #include "../geometry/Point.h"
20 #include "../geometry/Vector.h"
21 #include "../graphics/Menu.h"
22
23 #include <vector>
24 #include <SDL.h>
25
26 namespace app { class Input; }
27 namespace common {
28         class Inventory;
29         class Item;
30         class Spell;
31 }
32 namespace graphics {
33         class Font;
34         class Frame;
35         class Gauge;
36         class Sprite;
37 }
38
39 namespace battle {
40
41 class PartyLayout;
42
43 class BattleState
44 : public app::State {
45
46 public:
47         BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const Resources *res)
48         : background(background)
49         , monstersLayout(&monstersLayout)
50         , heroesLayout(&heroesLayout)
51         , res(res)
52         , attackTypeMenu(res->attackIcons)
53         , moveMenu(res->moveIcons)
54         , activeHero(-1)
55         , ranAway(false) { }
56
57 public:
58         void AddMonster(const Monster &);
59         void AddHero(const Hero &);
60
61 public:
62         virtual void EnterState(app::Application &ctrl, SDL_Surface *screen);
63         virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
64         virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
65         virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
66
67         virtual void Resize(int width, int height);
68
69         virtual void HandleEvents(const app::Input &);
70         virtual void UpdateWorld(float deltaT);
71         virtual void Render(SDL_Surface *);
72
73 public:
74         const Resources &Res() const { return *res; }
75         AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
76         MoveMenu &GetMoveMenu() { return moveMenu; }
77
78         bool HasMoreHeroes() const { return activeHero < (int) heroes.size(); }
79         void NextHero() { ++activeHero; }
80         bool BeforeFirstHero() const { return activeHero < 0; }
81         void PreviousHero() { --activeHero; }
82         Hero &ActiveHero() { return heroes[activeHero]; }
83         const Hero &ActiveHero() const { return heroes[activeHero]; }
84         Hero &HeroAt(std::vector<Hero>::size_type index) { return heroes[index]; }
85         const Hero &HeroAt(std::vector<Hero>::size_type index) const { return heroes[index]; }
86         void SwapHeroes(std::vector<Hero>::size_type lhs, std::vector<Hero>::size_type rhs);
87         const HeroTag &ActiveHeroTag() const { return heroTags[activeHero]; }
88         const HeroTag &HeroTagAt(std::vector<Hero>::size_type index) const { return heroTags[index]; }
89         const geometry::Point<int> &HeroTagPositionAt(std::vector<Hero>::size_type index) const { return heroTagPositions[index]; }
90         bool HasChosenAttackType() const { return attackChoices[activeHero].GetType() != AttackChoice::UNDECIDED; }
91         void SetAttackType(AttackChoice::Type t) { attackChoices[activeHero].SetType(t); }
92         AttackChoice &ActiveHeroAttackChoice() { return attackChoices[activeHero]; }
93         const AttackChoice &ActiveHeroAttackChoice() const { return attackChoices[activeHero]; }
94         const AttackChoice &AttackChoiceAt(std::vector<Hero>::size_type index) const { return attackChoices[index]; }
95         TargetSelection &ActiveHeroTargets() { return attackChoices[activeHero].Selection(); }
96         const TargetSelection &ActiveHeroTargets() const { return attackChoices[activeHero].Selection(); }
97         bool AttackSelectionDone() const { return activeHero >= (int) heroes.size(); }
98
99         graphics::Menu<const common::Spell *> &GetSpellMenu() { return spellMenus[activeHero]; }
100         const graphics::Menu<const common::Spell *> &GetSpellMenu() const { return spellMenus[activeHero]; }
101         graphics::Menu<const common::Item *> &GetIkariMenu() { return ikariMenus[activeHero]; }
102         const graphics::Menu<const common::Item *> &GetIkariMenu() const { return ikariMenus[activeHero]; }
103         graphics::Menu<const common::Item *> &GetItemMenu() { return itemMenu; }
104         const graphics::Menu<const common::Item *> &GetItemMenu() const { return itemMenu; }
105
106         const std::vector<geometry::Point<int> > &MonsterPositions() const { return monsterPositions; }
107         bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
108         const std::vector<geometry::Point<int> > &HeroesPositions() const { return heroesPositions; }
109         bool HeroPositionOccupied(int index) { return index >= 0 && index < int(heroes.size()); }
110         std::vector<Hero> &Heroes() { return heroes; }
111         const std::vector<Hero> &Heroes() const { return heroes; }
112
113         void SetRunaway() { ranAway = true; }
114
115 public:
116         geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
117                 return geometry::Vector<int>(
118                                 (screen->w - background->w) / 2,
119                                 (screen->h - background->h) / 2);
120         }
121         int BackgroundWidth() const { return background->w; }
122         int BackgroundHeight() const { return background->h; }
123
124         void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
125         void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
126         void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
127         void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
128
129 private:
130         void LoadSpellMenu(std::vector<Hero>::size_type heroIndex);
131         void LoadIkariMenu(std::vector<Hero>::size_type heroIndex);
132         void LoadInventory();
133
134 private:
135         SDL_Surface *background;
136         const PartyLayout *monstersLayout;
137         const PartyLayout *heroesLayout;
138         const Resources *res;
139         AttackTypeMenu attackTypeMenu;
140         MoveMenu moveMenu;
141         // TODO: combine all data about heros or monsters
142         std::vector<geometry::Point<int> > monsterPositions;
143         std::vector<geometry::Point<int> > heroesPositions;
144         std::vector<Monster> monsters;
145         std::vector<Hero> heroes;
146         std::vector<graphics::Menu<const common::Spell *> > spellMenus;
147         graphics::Menu<const common::Item *> itemMenu;
148         std::vector<graphics::Menu<const common::Item *> > ikariMenus;
149         HeroTag heroTags[4];
150         geometry::Point<int> heroTagPositions[4];
151         AttackChoice attackChoices[4];
152         int activeHero;
153         bool ranAway;
154
155 };
156
157 }
158
159 #endif /* BATTLE_BATTLESTATE_H_ */