]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.h
reorganized BattleState's interface
[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         , numHeroes(0)
55         , activeHero(-1)
56         , ranAway(false) { }
57
58 public:
59         void AddMonster(const Monster &);
60         void AddHero(const Hero &);
61
62 public:
63         virtual void EnterState(app::Application &ctrl, SDL_Surface *screen);
64         virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
65         virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
66         virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
67
68         virtual void Resize(int width, int height);
69
70         virtual void HandleEvents(const app::Input &);
71         virtual void UpdateWorld(float deltaT);
72         virtual void Render(SDL_Surface *);
73
74 public:
75         const Resources &Res() const { return *res; }
76         AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
77         MoveMenu &GetMoveMenu() { return moveMenu; }
78
79         graphics::Menu<const common::Spell *> &GetSpellMenu() { return spellMenus[activeHero]; }
80         const graphics::Menu<const common::Spell *> &GetSpellMenu() const { return spellMenus[activeHero]; }
81         graphics::Menu<const common::Item *> &GetIkariMenu() { return ikariMenus[activeHero]; }
82         const graphics::Menu<const common::Item *> &GetIkariMenu() const { return ikariMenus[activeHero]; }
83         graphics::Menu<const common::Item *> &GetItemMenu() { return itemMenu; }
84         const graphics::Menu<const common::Item *> &GetItemMenu() const { return itemMenu; }
85
86         bool HasMoreHeroes() const { return activeHero < numHeroes; }
87         void NextHero() { ++activeHero; }
88         bool BeforeFirstHero() const { return activeHero < 0; }
89         void PreviousHero() { --activeHero; }
90         void SwapHeroes(int lhs, int rhs);
91         Hero &ActiveHero() { return heroes[activeHero]; }
92         const Hero &ActiveHero() const { return heroes[activeHero]; }
93
94         Hero &HeroAt(int index) { return heroes[index]; }
95         const Hero &HeroAt(int index) const { return heroes[index]; }
96         Monster &MonsterAt(std::vector<Monster>::size_type index) { return monsters[index]; }
97         const Monster &MonsterAt(std::vector<Monster>::size_type index) const { return monsters[index]; }
98
99         const HeroTag &HeroTagAt(int index) const { return heroTags[index]; }
100         const geometry::Point<int> &HeroTagPositionAt(int index) const { return heroTagPositions[index]; }
101
102         bool HasChosenAttackType() const { return attackChoices[activeHero].GetType() != AttackChoice::UNDECIDED; }
103         AttackChoice &ActiveHeroAttackChoice() { return attackChoices[activeHero]; }
104         const AttackChoice &ActiveHeroAttackChoice() const { return attackChoices[activeHero]; }
105         const AttackChoice &AttackChoiceAt(int index) const { return attackChoices[index]; }
106         bool AttackSelectionDone() const { return activeHero >= numHeroes; }
107
108         const std::vector<geometry::Point<int> > &MonsterPositions() const { return monsterPositions; }
109         bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
110         const std::vector<geometry::Point<int> > &HeroesPositions() const { return heroesPositions; }
111         bool HeroPositionOccupied(int index) { return index >= 0 && index < numHeroes; }
112         int NumHeroes() const { return numHeroes; }
113         std::vector<Monster> &Monsters() { return monsters; }
114         const std::vector<Monster> &Monsters() const { return monsters; }
115
116         void SetRunaway() { ranAway = true; }
117         void ClearAllAttacks();
118
119 public:
120         geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
121                 return geometry::Vector<int>(
122                                 (screen->w - background->w) / 2,
123                                 (screen->h - background->h) / 2);
124         }
125         int BackgroundWidth() const { return background->w; }
126         int BackgroundHeight() const { return background->h; }
127
128         void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
129         void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
130         void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
131         void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
132
133 private:
134         void LoadSpellMenu(std::vector<Hero>::size_type heroIndex);
135         void LoadIkariMenu(std::vector<Hero>::size_type heroIndex);
136         void LoadInventory();
137
138 private:
139         SDL_Surface *background;
140         const PartyLayout *monstersLayout;
141         const PartyLayout *heroesLayout;
142         const Resources *res;
143         AttackTypeMenu attackTypeMenu;
144         MoveMenu moveMenu;
145         // TODO: combine all data about heros or monsters
146         std::vector<geometry::Point<int> > monsterPositions;
147         std::vector<geometry::Point<int> > heroesPositions;
148         std::vector<Monster> monsters;
149         Hero heroes[4];
150         graphics::Menu<const common::Spell *> spellMenus[4];
151         graphics::Menu<const common::Item *> itemMenu;
152         graphics::Menu<const common::Item *> ikariMenus[4];
153         HeroTag heroTags[4];
154         geometry::Point<int> heroTagPositions[4];
155         AttackChoice attackChoices[4];
156         int numHeroes;
157         int activeHero;
158         bool ranAway;
159
160 };
161
162 }
163
164 #endif /* BATTLE_BATTLESTATE_H_ */