-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
194 lines (146 loc) · 4.9 KB
/
Player.cpp
File metadata and controls
194 lines (146 loc) · 4.9 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
#include "Definitions.h"
//this is for the sprite where the player object facing upwards
//0xFFA500 - Orange
//0x00FF00 - Green
Player::Player(uint32_t* sprites) {
//initialize the sprite pointers
PLAYER_SPRITE_RIGHT = sprites + (BLOCK_WIDTH * 10);
PLAYER_SPRITE_RIGHT_1 = PLAYER_SPRITE_RIGHT + BLOCK_WIDTH;
PLAYER_SPRITE_LEFT = sprites + (BLOCK_WIDTH * 12);
PLAYER_SPRITE_LEFT_1 = PLAYER_SPRITE_LEFT + BLOCK_WIDTH;
//for the player with weapon
PLAYER_SPRITE_RIGHT_WEAPON = sprites + (BLOCK_WIDTH * 14);
PLAYER_SPRITE_RIGHT_1_WEAPON = PLAYER_SPRITE_RIGHT_WEAPON + BLOCK_WIDTH;
PLAYER_SPRITE_LEFT_WEAPON = sprites + (BLOCK_WIDTH * 16);
PLAYER_SPRITE_LEFT_1_WEAPON = PLAYER_SPRITE_LEFT_WEAPON + BLOCK_WIDTH;
hitpoints = 100;
attackDamage = 20;
//by default, the player object will face to the right
playerFacing = Direction::RIGHT;
sprite = PLAYER_SPRITE_RIGHT;
weapon = nullptr;
}
void Player::setNewDirectionFacing(Direction newDir) {
playerFacing = newDir;
//update the player sprite to be displayed accordingly
switch (newDir) {
case Direction::UP:
if (isEquipWeapon()) {
if (sprite == PLAYER_SPRITE_LEFT_WEAPON || sprite == PLAYER_SPRITE_LEFT)
sprite = PLAYER_SPRITE_LEFT_1_WEAPON;
else if (sprite == PLAYER_SPRITE_LEFT_1_WEAPON || sprite == PLAYER_SPRITE_LEFT_1)
sprite = PLAYER_SPRITE_LEFT_WEAPON;
else if (sprite == PLAYER_SPRITE_RIGHT_WEAPON || sprite == PLAYER_SPRITE_RIGHT)
sprite = PLAYER_SPRITE_RIGHT_1_WEAPON;
else
sprite = PLAYER_SPRITE_RIGHT_WEAPON;
}
else {
if (sprite == PLAYER_SPRITE_LEFT_WEAPON || sprite == PLAYER_SPRITE_LEFT)
sprite = PLAYER_SPRITE_LEFT_1;
else if (sprite == PLAYER_SPRITE_LEFT_1_WEAPON || sprite == PLAYER_SPRITE_LEFT_1)
sprite = PLAYER_SPRITE_LEFT;
else if (sprite == PLAYER_SPRITE_RIGHT_WEAPON || sprite == PLAYER_SPRITE_RIGHT)
sprite = PLAYER_SPRITE_RIGHT_1;
else
sprite = PLAYER_SPRITE_RIGHT;
}
break;
case Direction::DOWN:
if (isEquipWeapon()) {
if (sprite == PLAYER_SPRITE_LEFT_WEAPON || sprite == PLAYER_SPRITE_LEFT)
sprite = PLAYER_SPRITE_LEFT_1_WEAPON;
else if (sprite == PLAYER_SPRITE_LEFT_1_WEAPON || sprite == PLAYER_SPRITE_LEFT_1)
sprite = PLAYER_SPRITE_LEFT_WEAPON;
else if (sprite == PLAYER_SPRITE_RIGHT_WEAPON || sprite == PLAYER_SPRITE_RIGHT)
sprite = PLAYER_SPRITE_RIGHT_1_WEAPON;
else
sprite = PLAYER_SPRITE_RIGHT_WEAPON;
}
else {
if (sprite == PLAYER_SPRITE_LEFT_WEAPON || sprite == PLAYER_SPRITE_LEFT)
sprite = PLAYER_SPRITE_LEFT_1;
else if (sprite == PLAYER_SPRITE_LEFT_1_WEAPON || sprite == PLAYER_SPRITE_LEFT_1)
sprite = PLAYER_SPRITE_LEFT;
else if (sprite == PLAYER_SPRITE_RIGHT_WEAPON || sprite == PLAYER_SPRITE_RIGHT)
sprite = PLAYER_SPRITE_RIGHT_1;
else
sprite = PLAYER_SPRITE_RIGHT;
}
break;
case Direction::LEFT:
if (isEquipWeapon()) {
if (sprite == PLAYER_SPRITE_LEFT_WEAPON || sprite == PLAYER_SPRITE_LEFT)
sprite = PLAYER_SPRITE_LEFT_1_WEAPON;
else
sprite = PLAYER_SPRITE_LEFT_WEAPON;
}
else {
if (sprite == PLAYER_SPRITE_LEFT_WEAPON || sprite == PLAYER_SPRITE_LEFT)
sprite = PLAYER_SPRITE_LEFT_1;
else
sprite = PLAYER_SPRITE_LEFT;
}
break;
case Direction::RIGHT:
if (isEquipWeapon()) {
if (sprite == PLAYER_SPRITE_RIGHT_WEAPON || sprite == PLAYER_SPRITE_RIGHT)
sprite = PLAYER_SPRITE_RIGHT_1_WEAPON;
else
sprite = PLAYER_SPRITE_RIGHT_WEAPON;
}
else {
if (sprite == PLAYER_SPRITE_RIGHT_WEAPON || sprite == PLAYER_SPRITE_RIGHT)
sprite = PLAYER_SPRITE_RIGHT_1;
else
sprite = PLAYER_SPRITE_RIGHT;
}
break;
default:
sprite = PLAYER_SPRITE_RIGHT;
}
}
void Player::absorbDamage(int damage) {
hitpoints = hitpoints - damage;
}
Direction Player::getPlayerFacing() { return playerFacing; }
int Player::getAttackDamage() { return attackDamage; }
void Player::healUp(int amount) {
if(hitpoints < 140)
hitpoints = hitpoints + amount;
}
std::vector<Items*> Player::getCollectedItems() { return bag; }
void Player::addCollectedItem(Items* collectable) {
if (collectable->getType() == TypeItems::COLLECTABLE1 ||
collectable->getType() == TypeItems::COLLECTABLE2 ||
collectable->getType() == TypeItems::COLLECTABLE3) {
bag.push_back(collectable);
}
}
Items* Player::getWeapon() {
return weapon;
}
void Player::equipWeapon(Items* item) {
//equip only when the item is type of weapon
if (item->getType() == TypeItems::WEAPON) {
if (!isEquipWeapon()) { //for equiping if the player is not equipped
weapon = item;
}
else { //for equippng if the player is already equipped
//delete the current object of the weapon
delete weapon;
//and replace it with the new one
weapon = item;
}
}
}
bool Player::isEquipWeapon() {
return weapon != nullptr;
}
void Player::disarmWeapon() {
delete weapon;
weapon = nullptr;
}
Player::~Player() {
delete[] sprite;
}