-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.hpp
130 lines (115 loc) · 3.13 KB
/
Player.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#ifndef __PLAYER_HPP
#define __PLAYER_HPP
#include <conio.h>
#include <iostream>
#include "Inventar.hpp"
#include "Dragon.hpp"
/// enum storing keyboard keys used for playing
enum class KEYS
{
NONE = 0,
UP = 'w',
DOWN = 's',
LEFT = 'a',
RIGHT = 'd',
EXIT = '`'
};
bool operator==(char c, const KEYS &k);
/// class representing player moving on the board and interacting with event generators on it
class Player
{
public:
/// ID's of different hero types
enum class HERO_TYPE
{
HUMAN = 0,
MAGE = 1,
WARRIOR = 2,
ERROR = 3
};
private:
static const char PLAYER_CHAR = (char)207;
unsigned y, x;
Inventar *inv;
String name;
HeroEquipment *&getMatching(const HeroEquipment *ptr);
void printInventar() const;
void inventar();
virtual bool payCost(float points) = 0;
virtual void saveStats(std::ofstream &ofs) const = 0;
virtual HERO_TYPE getID() const = 0;
protected:
/// equipment worn by the player
HeroEquipment *equip[Constants::EQUIPMENT_COUNT];
/// readind player from text file
Player(std::ifstream &ifs);
public:
/// construct player by its position on the board and name
Player(unsigned posY, unsigned posX, const String &n);
virtual ~Player();
/// move the player on the map with keyboard keys if new position is allowed
template <typename ALLOWED>
bool move(bool &run, bool &pause, ALLOWED f);
unsigned getY() const { return y; }
unsigned getX() const { return x; }
virtual char getChar() const { return PLAYER_CHAR; }
/// player tries to take equipment (false if not enough space in the inventar)
bool take(const HeroEquipment &) const;
const String &getName() const { return name; }
virtual void printStats() const = 0;
/// print equipment of the hero
void printItems() const;
/// print player's info, battle icon and dragon's info
void printBattleState(Dragon &) const;
virtual bool alive() const = 0;
/// deal damage to a dragon
void hit(Dragon &d);
virtual void takeDamage(float dmg) = 0;
virtual void printBrief() const = 0;
virtual float getAttack() const = 0;
virtual float getHealth() const = 0;
virtual float getMana() const = 0;
virtual void regenerate(float percentage = 0) = 0;
/// reset position on the board (back to top-left corner)
void reset() { y = x = 0; }
/// write info into text file
void save(const String &game) const;
virtual void gainXP(unsigned gain) = 0;
};
template <typename ALLOWED>
bool Player::move(bool &run, bool &pause, ALLOWED f)
{
char c = getch();
if (c == KEYS::UP && f(y - 1, x))
{
--y;
return true;
}
else if (c == KEYS::DOWN && f(y + 1, x))
{
++y;
return true;
}
else if (c == KEYS::LEFT && f(y, x - 1))
{
--x;
return true;
}
else if (c == KEYS::RIGHT && f(y, x + 1))
{
++x;
return true;
}
else if (c == 'i')
{
inventar();
return true;
}
else if (c == 'p')
{
pause = true;
return false;
}
return false;
}
#endif