]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.h
added melee animation of monsters
[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         AttackChoice &CurrentAttackAttackChoice();
138         void ClearAllAttacks();
139
140         bool Victory() const;
141         bool Defeat() const;
142
143 public:
144         geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
145                 return geometry::Vector<int>(
146                                 (screen->w - background->w) / 2,
147                                 (screen->h - background->h) / 2);
148         }
149         int Width() const { return background->w; }
150         int Height() const { return background->h; }
151
152         void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
153         void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
154         void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
155         void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
156         void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
157
158 private:
159         void LoadSpellMenu(std::vector<Hero>::size_type heroIndex);
160         void LoadIkariMenu(std::vector<Hero>::size_type heroIndex);
161         void LoadInventory();
162
163         Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
164
165 private:
166         SDL_Surface *background;
167         const PartyLayout *monstersLayout;
168         const PartyLayout *heroesLayout;
169         const Resources *res;
170         AttackTypeMenu attackTypeMenu;
171         MoveMenu moveMenu;
172         // TODO: combine all data about heros or monsters
173         std::vector<geometry::Point<int> > monsterPositions;
174         std::vector<geometry::Point<int> > heroesPositions;
175         std::vector<Monster> monsters;
176         std::vector<AttackChoice> monsterAttacks;
177         std::vector<Order> attackOrder;
178         Hero heroes[4];
179         graphics::Menu<const common::Spell *> spellMenus[4];
180         graphics::Menu<const common::Item *> itemMenu;
181         graphics::Menu<const common::Item *> ikariMenus[4];
182         HeroTag heroTags[4];
183         SmallHeroTag smallHeroTags[4];
184         geometry::Point<int> heroTagPositions[4];
185         geometry::Point<int> smallHeroTagPositions[4];
186         AttackChoice attackChoices[4];
187         int numHeroes;
188         int activeHero;
189         int attackCursor;
190         bool ranAway;
191
192 };
193
194 }
195
196 #endif /* BATTLE_BATTLESTATE_H_ */