]> git.localhorst.tv Git - l2e.git/blob - src/battle/BattleState.h
moved monster's position 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::Item *> &ItemMenu() { return itemMenu; }
88         const graphics::Menu<const common::Item *> &ItemMenu() const { return itemMenu; }
89
90         void NextHero();
91         bool BeforeFirstHero() const { return activeHero < 0; }
92         void PreviousHero();
93         void SwapHeroes(int lhs, int rhs);
94         Hero &ActiveHero() { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
95         const Hero &ActiveHero() const { assert(activeHero >= 0 && activeHero < NumHeroes()); return heroes[activeHero]; }
96
97         Hero &HeroAt(int index) { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
98         const Hero &HeroAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroes[index]; }
99         Monster &MonsterAt(int index) { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
100         const Monster &MonsterAt(int index) const { assert(index >= 0 && index < NumHeroes()); return monsters[index]; }
101
102         const HeroTag &HeroTagAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTags[index]; }
103         const geometry::Point<int> &HeroTagPositionAt(int index) const { assert(index >= 0 && index < NumHeroes()); return heroTagPositions[index]; }
104
105         bool HasChosenAttackType() const { return ActiveHero().GetAttackChoice().GetType() != AttackChoice::UNDECIDED; }
106         bool AttackSelectionDone() const { return activeHero >= numHeroes; }
107
108         int NumHeroes() const { return numHeroes; }
109         int MaxHeroes() const { return 4; }
110         int MaxMonsters() const { return monsters.size(); }
111
112         bool MonsterPositionOccupied(int index) { return index >= 0 && index < int(monsters.size()) && monsters[index].Health() > 0; }
113         bool HeroPositionOccupied(int index) const { return index >= 0 && index < numHeroes; }
114
115         void SetRunaway() { ranAway = true; }
116
117         struct Order {
118                 Order(int index, bool isMonster)
119                 : index(index), isMonster(isMonster) { }
120                 int index;
121                 bool isMonster;
122         };
123
124         void CalculateAttackOrder();
125         void NextAttack();
126         bool AttacksFinished() const;
127         void CalculateDamage();
128         void ApplyDamage();
129         const Order &CurrentAttack() const { assert(attackCursor >= 0 && attackCursor < int(attackOrder.size())); return attackOrder[attackCursor]; };
130         AttackChoice &CurrentAttackAttackChoice();
131         void ClearAllAttacks();
132
133         bool Victory() const;
134         bool Defeat() const;
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 LoadInventory();
153
154         Uint16 CalculateDamage(const Stats &attacker, const Stats &defender) const;
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<Monster> monsters;
165         std::vector<Order> attackOrder;
166         Hero heroes[4];
167         graphics::Menu<const common::Item *> itemMenu;
168         HeroTag heroTags[4];
169         SmallHeroTag smallHeroTags[4];
170         geometry::Point<int> heroTagPositions[4];
171         geometry::Point<int> smallHeroTagPositions[4];
172         int numHeroes;
173         int activeHero;
174         int attackCursor;
175         int expReward;
176         int goldReward;
177         bool ranAway;
178
179 };
180
181 }
182
183 #endif /* BATTLE_BATTLESTATE_H_ */