]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.h
moved monsters' attack choices to Monster
[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 "AttackTypeMenu.h"
12 #include "Hero.h"
13 #include "HeroTag.h"
14 #include "Monster.h"
15 #include "MoveMenu.h"
16 #include "Resources.h"
17 #include "SmallHeroTag.h"
18 #include "../app/State.h"
19 #include "../geometry/Point.h"
20 #include "../geometry/Vector.h"
21 #include "../graphics/Animation.h"
22 #include "../graphics/Menu.h"
23
24 #include <cassert>
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 AttackChoice;
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         , expReward(0)
62         , goldReward(0)
63         , ranAway(false) { assert(background && res); }
64
65 public:
66         void AddMonster(const Monster &);
67         void AddHero(const Hero &);
68
69 public:
70         virtual void EnterState(app::Application &ctrl, SDL_Surface *screen);
71         virtual void ExitState(app::Application &ctrl, SDL_Surface *screen);
72         virtual void ResumeState(app::Application &ctrl, SDL_Surface *screen);
73         virtual void PauseState(app::Application &ctrl, SDL_Surface *screen);
74
75         virtual void Resize(int width, int height);
76
77         virtual void HandleEvents(const app::Input &);
78         virtual void UpdateWorld(float deltaT);
79         virtual void Render(SDL_Surface *);
80
81         // TODO: turn this mess into a well stuctured interface
82 public:
83         const Resources &Res() const { return *res; }
84         AttackTypeMenu &GetAttackTypeMenu() { return attackTypeMenu; }
85         MoveMenu &GetMoveMenu() { return moveMenu; }
86
87         graphics::Menu<const common::Spell *> &GetSpellMenu() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero].SpellMenu(); }
88         const graphics::Menu<const common::Spell *> &GetSpellMenu() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero].SpellMenu(); }
89         graphics::Menu<const common::Item *> &GetIkariMenu() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero].IkariMenu(); }
90         const graphics::Menu<const common::Item *> &GetIkariMenu() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero].IkariMenu(); }
91         graphics::Menu<const common::Item *> &GetItemMenu() { return itemMenu; }
92         const graphics::Menu<const common::Item *> &GetItemMenu() const { return itemMenu; }
93
94         void NextHero();
95         bool BeforeFirstHero() const { return activeHero < 0; }
96         void PreviousHero();
97         void SwapHeroes(int lhs, int rhs);
98         Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
99         const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
100
101         Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
102         const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
103         Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
104         const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
105
106         const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
107         const geometry::Point<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
108
109         bool HasChosenAttackType() const { return ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
110         AttackChoice &MonsterAttackChoiceAt(int index) { assert(index >= 0 && index < MaxMonsters()); return monsters[index].GetAttackChoice(); }
111         const AttackChoice &MonsterAttackChoiceAt(int index) const { assert(index >= 0 && index < MaxMonsters()); return monsters[index].GetAttackChoice(); }
112         bool AttackSelectionDone() const { return activeHero >= numHeroes; }
113
114         int NumHeroes() const { return numHeroes; }
115         int MaxHeroes() const { return 4; }
116         int MaxMonsters() const { return monsters.size(); }
117
118         const std::vector<geometry::Point<int> > &MonsterPositions() const { return monsterPositions; }
119         bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
120         const std::vector<geometry::Point<int> > &HeroesPositions() const { return heroesPositions; }
121         bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
122
123         void SetRunaway() { ranAway = true; }
124
125         struct Order {
126                 Order(int index, bool isMonster)
127                 : index(index), isMonster(isMonster) { }
128                 int index;
129                 bool isMonster;
130         };
131
132         void CalculateAttackOrder();
133         void NextAttack();
134         bool AttacksFinished() const;
135         void CalculateDamage();
136         void ApplyDamage();
137         const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
138         AttackChoice &CurrentAttackAttackChoice();
139         void ClearAllAttacks();
140
141         bool Victory() const;
142         bool Defeat() const;
143
144 public:
145         geometry::Vector<int> CalculateScreenOffset(SDL_Surface *screen) const {
146                 return geometry::Vector<int>(
147                                 (screen->w - background->w) / 2,
148                                 (screen->h - background->h) / 2);
149         }
150         int Width() const { return background->w; }
151         int Height() const { return background->h; }
152
153         void RenderBackground(SDL_Surface *screen, const geometry::Vector<int> &offset);
154         void RenderMonsters(SDL_Surface *screen, const geometry::Vector<int> &offset);
155         void RenderHeroes(SDL_Surface *screen, const geometry::Vector<int> &offset);
156         void RenderHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
157         void RenderSmallHeroTags(SDL_Surface *screen, const geometry::Vector<int> &offset);
158
159 private:
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<Order> attackOrder;
176         Hero heroes[4];
177         graphics::Menu<const common::Item *> itemMenu;
178         HeroTag heroTags[4];
179         SmallHeroTag smallHeroTags[4];
180         geometry::Point<int> heroTagPositions[4];
181         geometry::Point<int> smallHeroTagPositions[4];
182         int numHeroes;
183         int activeHero;
184         int attackCursor;
185         int expReward;
186         int goldReward;
187         bool ranAway;
188
189 };
190
191 }
192
193 #endif /* BATTLE_BATTLESTATE_H_ */