]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.h
added numbers animation for monsters' attacks
[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         // TODO: turn this mess into a well stuctured interface
80 public:
81         const Resources &Res() const { return *res; }
82         AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
83         MoveMenu &GetMoveMenu() { return moveMenu; }
84
85         graphics::Menu<const common::Spell *> &GetSpellMenu() { return spellMenus[activeHero]; }
86         const graphics::Menu<const common::Spell *> &GetSpellMenu() const { return spellMenus[activeHero]; }
87         graphics::Menu<const common::Item *> &GetIkariMenu() { return ikariMenus[activeHero]; }
88         const graphics::Menu<const common::Item *> &GetIkariMenu() const { return ikariMenus[activeHero]; }
89         graphics::Menu<const common::Item *> &GetItemMenu() { return itemMenu; }
90         const graphics::Menu<const common::Item *> &GetItemMenu() const { return itemMenu; }
91
92         void NextHero();
93         bool BeforeFirstHero() const { return activeHero < 0; }
94         void PreviousHero() { --activeHero; }
95         void SwapHeroes(int lhs, int rhs);
96         Hero &ActiveHero() { return heroes[activeHero]; }
97         const Hero &ActiveHero() const { return heroes[activeHero]; }
98
99         Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
100         const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
101         Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
102         const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
103
104         const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
105         const geometry::Point<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
106
107         bool HasChosenAttackType() const { return ActiveHeroAttackChoice().GetType() != AttackChoice::UNDECIDED; }
108         AttackChoice &ActiveHeroAttackChoice() { return AttackChoiceAt(activeHero); }
109         const AttackChoice &ActiveHeroAttackChoice() const { return AttackChoiceAt(activeHero); }
110         AttackChoice &AttackChoiceAt(int index) { assert(index >= 0 && index < NumHeroes()); return attackChoices[index]; }
111         const AttackChoice &AttackChoiceAt(int index) const { assert(index >= 0 && index < NumHeroes()); return attackChoices[index]; }
112         AttackChoice &MonsterAttackChoiceAt(int index) { assert(index >= 0 && index < MaxMonsters()); return monsterAttacks[index]; }
113         const AttackChoice &MonsterAttackChoiceAt(int index) const { assert(index >= 0 && index < MaxMonsters()); return monsterAttacks[index]; }
114         bool AttackSelectionDone() const { return activeHero >= numHeroes; }
115
116         int NumHeroes() const { return numHeroes; }
117         int MaxHeroes() const { return 4; }
118         int MaxMonsters() const { return monsters.size(); }
119
120         const std::vector<geometry::Point<int> > &MonsterPositions() const { return monsterPositions; }
121         bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
122         const std::vector<geometry::Point<int> > &HeroesPositions() const { return heroesPositions; }
123         bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
124
125         void SetRunaway() { ranAway = true; }
126
127         struct Order {
128                 Order(int index, bool isMonster)
129                 : index(index), isMonster(isMonster) { }
130                 int index;
131                 bool isMonster;
132         };
133
134         void CalculateAttackOrder();
135         void NextAttack();
136         bool AttacksFinished() const;
137         void CalculateDamage();
138         void ApplyDamage();
139         const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
140         AttackChoice &CurrentAttackAttackChoice();
141         void ClearAllAttacks();
142
143         bool Victory() const;
144         bool Defeat() const;
145
146 public:
147         geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
148                 return geometry::Vector<int>(
149                                 (screen->w - background->w) / 2,
150                                 (screen->h - background->h) / 2);
151         }
152         int Width() const { return background->w; }
153         int Height() const { return background->h; }
154
155         void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
156         void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
157         void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
158         void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
159         void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
160
161 private:
162         void LoadSpellMenu(std::vector<Hero>::size_type heroIndex);
163         void LoadIkariMenu(std::vector<Hero>::size_type heroIndex);
164         void LoadInventory();
165
166         Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
167
168 private:
169         SDL_Surface *background;
170         const PartyLayout *monstersLayout;
171         const PartyLayout *heroesLayout;
172         const Resources *res;
173         AttackTypeMenu attackTypeMenu;
174         MoveMenu moveMenu;
175         // TODO: combine all data about heros or monsters
176         std::vector<geometry::Point<int> > monsterPositions;
177         std::vector<geometry::Point<int> > heroesPositions;
178         std::vector<Monster> monsters;
179         std::vector<AttackChoice> monsterAttacks;
180         std::vector<Order> attackOrder;
181         Hero heroes[4];
182         graphics::Menu<const common::Spell *> spellMenus[4];
183         graphics::Menu<const common::Item *> itemMenu;
184         graphics::Menu<const common::Item *> ikariMenus[4];
185         HeroTag heroTags[4];
186         SmallHeroTag smallHeroTags[4];
187         geometry::Point<int> heroTagPositions[4];
188         geometry::Point<int> smallHeroTagPositions[4];
189         AttackChoice attackChoices[4];
190         int numHeroes;
191         int activeHero;
192         int attackCursor;
193         bool ranAway;
194
195 };
196
197 }
198
199 #endif /* BATTLE_BATTLESTATE_H_ */