-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacters.cpp
More file actions
263 lines (217 loc) · 5.15 KB
/
Copy pathCharacters.cpp
File metadata and controls
263 lines (217 loc) · 5.15 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "Characters.h"
#include "Items.h"
#include <iostream>
#include <cmath>
extern int NUMBER_OF_COLUMNS;
extern int NUMBER_OF_ROWS;
extern Item* walls[200];
// =========================================================================== //
// Character Class Member Functions
Character::Character()
{
}
/// <summary>
/// Retrieves the amount of health the character currently has
/// </summary>
/// <returns></returns>
float Character::getHealth() const
{
return health;
}
/// <summary>
/// Updates the character's health when they regain health or recieve damage.
/// </summary>
/// <param name="amountToAdd"></param>
void Character::setHealth(float amountToAdd)
{
health += amountToAdd;
if (health > 100.0f)
{
health = 100.0f;
}
}
float Character::getDamage() const
{
return damage;
}
/// <summary>
/// Retrieves the x-coordinate of the character's position on the board.
/// </summary>
/// <returns></returns>
int Character::getXCoordinate() const
{
return xPosition;
}
/// <summary>
/// Retrieves the y-coordinate of the character's position on the board.
/// </summary>
/// <returns></returns>
int Character::getYCoordinate() const
{
return yPosition;
}
/// <summary>
/// Updates the character's current x-coordinate
/// </summary>
/// <param name="amountToAdd"></param>
void Character::setXCoordinate(int amountToAdd)
{
xPosition += amountToAdd;
}
/// <summary>
/// Updates the character's current y-coordinate
/// </summary>
/// <param name="amountToAdd"></param>
void Character::setYCoordinate(int amountToAdd)
{
yPosition += amountToAdd;
}
/// <summary>
/// Retrieves the symbol which represents the character
/// </summary>
/// <returns></returns>
char Character::getSymbol() const
{
return symbol;
}
// =========================================================================== //
// PlayerCharacter Class Member Functions
/// <summary>
/// Constructor
/// </summary>
PlayerCharacter::PlayerCharacter()
{
health = 0;
symbol = '^';
xPosition = 0;
yPosition = 0;
numberOfCoins = 0;
damage = 40.0f;
}
/// <summary>
/// Retrieves the number of coins that the PC has collected
/// </summary>
/// <returns></returns>
int PlayerCharacter::getNumberOfCoins() const
{
return numberOfCoins;
}
/// <summary>
/// Updates the number of coins the PC has collected
/// </summary>
/// <param name="amountToAdd"></param>
void PlayerCharacter::setNumberOfCoins(int amountToAdd)
{
numberOfCoins += amountToAdd;
}
void PlayerCharacter::movePC(int keycode)
{
int newXCoordinate = -1;
int newYCoordinate = -1;
switch (keycode)
{
case 87: // W
newXCoordinate = xPosition + 0;
newYCoordinate = yPosition - 1; // PC moves up
break;
case 83: // S
newXCoordinate = xPosition + 0;
newYCoordinate = yPosition + 1; // PC moves down
break;
case 65: // A
newXCoordinate = xPosition - 1; // PC moves left
newYCoordinate = yPosition + 0;
break;
case 68: // D
newXCoordinate = xPosition + 1; // PC moves right
newYCoordinate = yPosition + 0;
break;
default:
break;
}
if (newXCoordinate <= 0 || newXCoordinate >= NUMBER_OF_COLUMNS - 1 || newYCoordinate <= 0 || newYCoordinate >= NUMBER_OF_ROWS - 1)
{
return;
}
else
{
xPosition = newXCoordinate; // Updates the PC's x-coordinate
yPosition = newYCoordinate; // Updates the PC's y-coordinate
if (health < 100)
{
health += 2; // PC health recovers overtime
if (health >= 100)
{
health = 100;
}
}
}
}
// =========================================================================== //
// Enemy Class Member Functions
/// <summary>
/// Constructor
/// </summary>
/// <param name="PDamage"></param>
Enemy::Enemy(int pId)
{
symbol = 'W';
xPosition = 0;
yPosition = 0;
damage = 25.0f;
health = 0;
id = pId;
}
/// <summary>
/// Retrieves the enemy's ID, allowing PvE combat
/// </summary>
/// <returns></returns>
int Enemy::getID() const
{
return id;
}
/// <summary>
/// Moves an enemy randomly when in close proximity to the player
/// </summary>
/// <param name="pcXPosition"></param>
/// <param name="pcYPosition"></param>
void Enemy::moveEnemy(int pcXPosition, int pcYPosition)
{
if ((std::abs(pcXPosition - xPosition) <= 5) && (std::abs(pcYPosition - yPosition) <= 5))
{
srand(static_cast<unsigned int>(time(0)));
int randomNumber = (rand() % 4) + 1; // Generates random number between 1 and 4
int newXCoordinate = -1;
int newYCoordinate = -1;
switch (randomNumber)
{
case 1: // Upwards
newXCoordinate = xPosition + 0;
newYCoordinate = yPosition - 1; // Enemy moves up
break;
case 2: // Downwards
newXCoordinate = xPosition + 0;
newYCoordinate = yPosition + 1; // Enemy moves down
break;
case 3: // Left
newXCoordinate = xPosition - 1; // Enemy moves left
newYCoordinate = yPosition + 0;
break;
case 4: // Right
newXCoordinate = xPosition + 1; // Enemy moves right
newYCoordinate = yPosition + 0;
break;
default:
break;
}
if (newXCoordinate <= 0 || newXCoordinate >= NUMBER_OF_COLUMNS - 1 || newYCoordinate <= 0 || newYCoordinate >= NUMBER_OF_ROWS - 1)
{
return;
}
else
{
xPosition = newXCoordinate; // Updates the enemy's x-coordinate
yPosition = newYCoordinate; // Updates the enemy's y-coordinate
}
}
}