]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.h
moved attack order to battle state
[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/Animation.h"
23 #include "../graphics/Menu.h"
24
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 PartyLayout;
44
45 class BattleState
46 : public app::State {
47
48 public:
49         BattleState(SDL_Surface *background, const PartyLayout &monstersLayout, const PartyLayout &heroesLayout, const Resources *res)
50         : background(background)
51         , monstersLayout(&monstersLayout)
52         , heroesLayout(&heroesLayout)
53         , res(res)
54         , attackTypeMenu(res->attackIcons)
55         , moveMenu(res->moveIcons)
56         , numHeroes(0)
57         , activeHero(-1)
58         , attackCursor(-1)
59         , ranAway(false) { }
60
61 public:
62         void AddMonster(const Monster &);
63         void AddHero(const Hero &);
64
65 public:
66         virtual void EnterState(app::Application &ctrl, SDL_Surface *screen);
67         virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
68         virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
69         virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
70
71         virtual void Resize(int width, int height);
72
73         virtual void HandleEvents(const app::Input &);
74         virtual void UpdateWorld(float deltaT);
75         virtual void Render(SDL_Surface *);
76
77 public:
78         const Resources &Res() const { return *res; }
79         AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
80         MoveMenu &GetMoveMenu() { return moveMenu; }
81
82         graphics::Menu<const common::Spell *> &GetSpellMenu() { return spellMenus[activeHero]; }
83         const graphics::Menu<const common::Spell *> &GetSpellMenu() const { return spellMenus[activeHero]; }
84         graphics::Menu<const common::Item *> &GetIkariMenu() { return ikariMenus[activeHero]; }
85         const graphics::Menu<const common::Item *> &GetIkariMenu() const { return ikariMenus[activeHero]; }
86         graphics::Menu<const common::Item *> &GetItemMenu() { return itemMenu; }
87         const graphics::Menu<const common::Item *> &GetItemMenu() const { return itemMenu; }
88
89         void NextHero();
90         bool BeforeFirstHero() const { return activeHero < 0; }
91         void PreviousHero() { --activeHero; }
92         void SwapHeroes(int lhs, int rhs);
93         Hero &ActiveHero() { return heroes[activeHero]; }
94         const Hero &ActiveHero() const { return heroes[activeHero]; }
95
96         Hero &HeroAt(int index) { return heroes[index]; }
97         const Hero &HeroAt(int index) const { return heroes[index]; }
98         Monster &MonsterAt(int index) { return monsters[index]; }
99         const Monster &MonsterAt(int index) const { return monsters[index]; }
100
101         const HeroTag &HeroTagAt(int index) const { return heroTags[index]; }
102         const geometry::Point<int> &HeroTagPositionAt(int index) const { return heroTagPositions[index]; }
103
104         bool HasChosenAttackType() const { return attackChoices[activeHero].GetType() != AttackChoice::UNDECIDED; }
105         AttackChoice &ActiveHeroAttackChoice() { return attackChoices[activeHero]; }
106         const AttackChoice &ActiveHeroAttackChoice() const { return attackChoices[activeHero]; }
107         AttackChoice &AttackChoiceAt(int index) { return attackChoices[index]; }
108         const AttackChoice &AttackChoiceAt(int index) const { return attackChoices[index]; }
109         bool AttackSelectionDone() const { return activeHero >= numHeroes; }
110
111         int NumHeroes() const { return numHeroes; }
112         int MaxHeroes() const { return 4; }
113         int MaxMonsters() const { return monsters.size(); }
114
115         const std::vector<geometry::Point<int> > &MonsterPositions() const { return monsterPositions; }
116         bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
117         const std::vector<geometry::Point<int> > &HeroesPositions() const { return heroesPositions; }
118         bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
119
120         void SetRunaway() { ranAway = true; }
121
122         struct Order {
123                 Order(int index, bool isMonster)
124                 : index(index), isMonster(isMonster) { }
125                 int index;
126                 bool isMonster;
127         };
128
129         void CalculateAttackOrder();
130         void NextAttack();
131         bool AttacksFinished() const { return attackCursor >= int(attackOrder.size()); }
132         void CalculateDamage();
133         const Order &CurrentAttack() const { return attackOrder[attackCursor]; };
134         void ClearAllAttacks();
135
136 public:
137         geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
138                 return geometry::Vector<int>(
139                                 (screen->w - background->w) / 2,
140                                 (screen->h - background->h) / 2);
141         }
142         int Width() const { return background->w; }
143         int Height() const { return background->h; }
144
145         void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
146         void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
147         void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
148         void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
149         void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
150
151 private:
152         void LoadSpellMenu(std::vector<Hero>::size_type heroIndex);
153         void LoadIkariMenu(std::vector<Hero>::size_type heroIndex);
154         void LoadInventory();
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         // TODO: combine all data about heros or monsters
164         std::vector<geometry::Point<int> > monsterPositions;
165         std::vector<geometry::Point<int> > heroesPositions;
166         std::vector<Monster> monsters;
167         std::vector<AttackChoice> monsterAttacks;
168         std::vector<Order> attackOrder;
169         Hero heroes[4];
170         graphics::Menu<const common::Spell *> spellMenus[4];
171         graphics::Menu<const common::Item *> itemMenu;
172         graphics::Menu<const common::Item *> ikariMenus[4];
173         HeroTag heroTags[4];
174         SmallHeroTag smallHeroTags[4];
175         geometry::Point<int> heroTagPositions[4];
176         geometry::Point<int> smallHeroTagPositions[4];
177         AttackChoice attackChoices[4];
178         int numHeroes;
179         int activeHero;
180         int attackCursor;
181         bool ranAway;
182
183 };
184
185 }
186
187 #endif /* BATTLE_BATTLESTATE_H_ */