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