]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.h
handle dead hero in BattleState
[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 "SmallHeroTag.h"
19 #include "../app/State.h"
20 #include "../geometry/Point.h"
21 #include "../geometry/Vector.h"
22 #include "../graphics/Menu.h"
23
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 PartyLayout;
43
44 class BattleState
45 : public app::State {
46
47 public:
48         BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const Resources *res)
49         : background(background)
50         , monstersLayout(&monstersLayout)
51         , heroesLayout(&heroesLayout)
52         , res(res)
53         , attackTypeMenu(res->attackIcons)
54         , moveMenu(res->moveIcons)
55         , numHeroes(0)
56         , activeHero(-1)
57         , ranAway(false) { }
58
59 public:
60         void AddMonster(const Monster &);
61         void AddHero(const Hero &);
62
63 public:
64         virtual void EnterState(app::Application &ctrl, SDL_Surface *screen);
65         virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
66         virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
67         virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
68
69         virtual void Resize(int width, int height);
70
71         virtual void HandleEvents(const app::Input &);
72         virtual void UpdateWorld(float deltaT);
73         virtual void Render(SDL_Surface *);
74
75 public:
76         const Resources &Res() const { return *res; }
77         AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
78         MoveMenu &GetMoveMenu() { return moveMenu; }
79
80         graphics::Menu<const common::Spell *> &GetSpellMenu() { return spellMenus[activeHero]; }
81         const graphics::Menu<const common::Spell *> &GetSpellMenu() const { return spellMenus[activeHero]; }
82         graphics::Menu<const common::Item *> &GetIkariMenu() { return ikariMenus[activeHero]; }
83         const graphics::Menu<const common::Item *> &GetIkariMenu() const { return ikariMenus[activeHero]; }
84         graphics::Menu<const common::Item *> &GetItemMenu() { return itemMenu; }
85         const graphics::Menu<const common::Item *> &GetItemMenu() const { return itemMenu; }
86
87         void NextHero();
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(int index) { return monsters[index]; }
97         const Monster &MonsterAt(int 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         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         void ClearAllAttacks();
119
120 public:
121         geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
122                 return geometry::Vector<int>(
123                                 (screen->w - background->w) / 2,
124                                 (screen->h - background->h) / 2);
125         }
126         int BackgroundWidth() const { return background->w; }
127         int BackgroundHeight() const { return background->h; }
128
129         void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
130         void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
131         void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
132         void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
133         void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
134
135 private:
136         void LoadSpellMenu(std::vector<Hero>::size_type heroIndex);
137         void LoadIkariMenu(std::vector<Hero>::size_type heroIndex);
138         void LoadInventory();
139
140 private:
141         SDL_Surface *background;
142         const PartyLayout *monstersLayout;
143         const PartyLayout *heroesLayout;
144         const Resources *res;
145         AttackTypeMenu attackTypeMenu;
146         MoveMenu moveMenu;
147         // TODO: combine all data about heros or monsters
148         std::vector<geometry::Point<int> > monsterPositions;
149         std::vector<geometry::Point<int> > heroesPositions;
150         std::vector<Monster> monsters;
151         Hero heroes[4];
152         graphics::Menu<const common::Spell *> spellMenus[4];
153         graphics::Menu<const common::Item *> itemMenu;
154         graphics::Menu<const common::Item *> ikariMenus[4];
155         HeroTag heroTags[4];
156         SmallHeroTag smallHeroTags[4];
157         geometry::Point<int> heroTagPositions[4];
158         geometry::Point<int> smallHeroTagPositions[4];
159         AttackChoice attackChoices[4];
160         int numHeroes;
161         int activeHero;
162         bool ranAway;
163
164 };
165
166 }
167
168 #endif /* BATTLE_BATTLESTATE_H_ */