-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
361 lines (307 loc) · 10.1 KB
/
Player.cpp
File metadata and controls
361 lines (307 loc) · 10.1 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "Player.h"
#include "ShopItem.h" // to prevent ciruclar dependencies
// Initialize the static member
Player* Player::instance = nullptr;
// Private default constructor
// Initializes both Player-specific attributes and those inherited from MainEntity
Player::Player()
: playerName(""),
playerReputation(0),
fSpeed(0.5f),
hasFortuneCat(false),
hasFortunePig(false),
hasFortuneMask(false),
bag() {
circle.setRadius(20); // Set the radius of the player
circle.setFillColor(sf::Color::Red); // Set the player's color
circle.setPosition(460, 150); // Set initial position
}
// Private overloaded constructor
// Initializes the player with a given name and reputation
Player::Player(std::string name, int reputation)
: playerName(name),
playerReputation(reputation),
fSpeed(0.5f),
hasFortuneCat(false),
hasFortunePig(false),
hasFortuneMask(false),
bag() {
circle.setRadius(20); // Set the radius of the player
circle.setFillColor(sf::Color::Red); // Set the player's color
circle.setPosition(460, 100); // Set initial position
}
// Static method to access the single instance of Player
Player* Player::getInstance() {
// Check if the instance is already created
if (instance == nullptr) {
instance = new Player(); // dynamic memory allocation (will remain in memory unless destructed)
}
// Log the address
std::cout << "Player instance address: " << instance << std::endl;
// Return the singleton instance
return instance;
}
// Getter for playerName
std::string Player::getPlayerID() {
return playerName;
}
// Setter for playerName
void Player::setPlayerID(const std::string& name) {
playerName = name;
}
// Getter for playerReputation
int Player::getPlayerReputation() {
return playerReputation;
}
// Setter for playerReputation
void Player::setPlayerReputation(int reputation) {
playerReputation = reputation;
}
// Get player's circle
sf::CircleShape Player::getCircle() {
return circle;
}
// Getter for player's position
sf::Vector2f Player::getPosition() {
return circle.getPosition();
}
// Method to set the player's position
void Player::setPosition(const sf::Vector2f& position) {
circle.setPosition(position); // Set the position of the circle
}
// Method to Draw the Player
void Player::draw(sf::RenderWindow& window) {
window.draw(circle);
}
// Method for Click Handling
bool Player::isClicked(float mouseX, float mouseY) {
sf::FloatRect bounds = circle.getGlobalBounds();
return bounds.contains(mouseX, mouseY);
}
// Method to get the global bounds of the player
sf::FloatRect Player::getGlobalBounds() const {
return circle.getGlobalBounds();
}
// Method to get the player's speed
float Player::getPlayerSpeed() const {
return fSpeed;
}
// Method to set the player speed
void Player::setPlayerSpeed(float speed) {
fSpeed = speed;
}
// Friend extraction operator
// Inputs data from an input stream (e.g., std::cin) to populate a Player object's fields.
std::istream& operator>>(std::istream& aIstream, Player& aPlayer) {
std::cout << "Enter Player Name: ";
aIstream >> aPlayer.playerName;
return aIstream;
}
// Setter for FortuneCat status
void Player::setHasFortuneCat(bool status) {
hasFortuneCat = status;
}
// Getter for FortuneCat status
bool Player::getHasFortuneCat() const {
return hasFortuneCat;
}
// Setter for FortunePig status
void Player::setHasFortunePig(bool status) {
hasFortunePig = status;
}
// Getter for FortunePig status
bool Player::getHasFortunePig() const {
return hasFortunePig;
}
// Setter for FortuneMask status
void Player::setHasFortuneMask(bool status) {
hasFortuneMask = status;
}
// Getter for FortuneMask status
bool Player::getHasFortuneMask() const {
return hasFortuneMask;
}
// Method to update reputation based on the Fortune Cat
void Player::updateReputationForFortuneCat() {
if (hasFortuneCat && IsItemInBag("Fortune Cat")) {
// Check if 10 seconds have passed
if (fortuneCatTimer.getElapsedTime().asSeconds() >= 10) {
playerReputation += 5; // Gain reputation
std::cout << "Fortune Cat: Gained +5 reputation points! Current reputation: " << playerReputation << "\n";
fortuneCatTimer.restart(); // Restart the timer
}
}
}
// Method to update reputation based on the Fortune Pig
void Player::updateReputationForFortunePig() {
if (hasFortunePig && IsItemInBag("Fortune Pig")) {
// Check if 10 seconds have passed
if (fortunePigTimer.getElapsedTime().asSeconds() >= 10) {
playerReputation += 7; // Gain reputation
std::cout << "Fortune Pig: Gained +7 reputation points! Current reputation: " << playerReputation << "\n";
fortunePigTimer.restart(); // Restart the timer
}
}
}
// Method to update reputation based on the Fortune Mask
void Player::updateReputationForFortuneMask() {
if (hasFortuneMask && IsItemInBag("Fortune Mask")) {
// Check if 10 seconds have passed
if (fortuneMaskTimer.getElapsedTime().asSeconds() >= 10) {
playerReputation += 6; // Gain reputation
std::cout << "Fortune Mask: Gained +6 reputation points! Current reputation: " << playerReputation << "\n";
fortuneMaskTimer.restart(); // Restart the timer
}
}
}
// Method to add an item to the Bag
void Player::addItemToBag(const std::string& itemName, int itemCost) {
bag.addItem(itemName, itemCost);
std::cout << "Added item '" << itemName << "' of cost " << itemCost << " to the bag.\n";
}
// Method to remove an item to the Bag by itemName
void Player::removeItemFromBag(const std::string& itemName) {
if (!bag.isEmpty()) {
bag.removeItem(itemName);
}
else {
std::cout << "Cannot remove item: The bag is already empty.\n";
}
}
// Method to check if Bag is empty
bool Player::isBagEmpty() const {
bool empty = bag.isEmpty();
if (empty) {
std::cout << "The bag is empty.\n";
}
else {
std::cout << "The bag contains items.\n";
}
return empty;
}
// Method to list all items in the Bag
void Player::listItemsInBag() const {
bag.listAllItems();
}
// Method to check if an item is in the Bag
bool Player::IsItemInBag(const std::string& itemName) {
return bag.findItem(itemName);
}
// Handles user input for interacting with the Bag
void Player::handleBagMenu() {
char choice; // Declare choice outside the loop
bool exitBag = false; // Flag to check if we should exit the bag
while (!exitBag) { // Loop until exit command is given
std::cout << "Welcome to your bag!\n";
std::cout << "Press 1 to list items in the bag.\n";
std::cout << "Press 2 to exit the bag.\n";
std::cout << "Press 3 to navigate left in the bag.\n";
std::cout << "Press 4 to navigate right in the bag.\n";
// Read the player's choice
std::cin >> choice;
if (choice == '1') {
listItemsInBag();
}
else if (choice == '2') {
std::cout << "Exiting the bag...\n";
exitBag = true; // Set the flag to exit the loop
}
else if (choice == '3') {
moveLeftInBag();
}
else if (choice == '4') {
moveRightInBag();
}
else {
std::cout << "Invalid choice. Please try again.\n"; // Handle invalid input
}
}
}
// Method to move left in the Bag
void Player::moveLeftInBag() {
bag.moveLeft();
}
// Method to move right in the Bag
void Player::moveRightInBag() {
bag.moveRight();
}
// Method to activate an item in the Bag
void Player::activateItem() {
bag.activateItem();
}
// Method to get the current item in the Bag
void Player::getCurrentItemInBag() {
bag.getCurrentItem();
}
// Method to check if an item is activated in the Bag
bool Player::isItemActivated(const std::string& itemName) const {
// Check if the item exists in the bag
if (bag.findItem(itemName)) {
// Check if the item is currently activated
return bag.getIsItemActivated();
}
return false; // Item not found or not activated
}
// Method to add a scrap to the apron
void Player::addScrapToApron(const std::string& scrapName) {
apron.addScrap(scrapName);
std::cout << "Added item '" << scrapName << "' to the apron.\n";
}
// Method to remove a scrap from the apron by scrapName
void Player::removeScrapFromApron(const std::string& scrapName) {
if (!apron.isEmpty()) {
apron.removeScrap(scrapName);
}
else {
std::cout << "Cannot remove item: The apron is already empty.\n";
}
}
// Method to check if the apron is empty
bool Player::isApronEmpty() const {
bool empty = apron.isEmpty();
if (empty) {
std::cout << "The apron is empty.\n";
}
else {
std::cout << "The apron contains items.\n";
}
return empty;
}
// Method to list all scraps in the apron
void Player::listScrapsInApron() const {
apron.listAllScraps();
}
// Method to find scrap in the apron
bool Player::findScrapFromApron(const std::string& scrapName) const {
return apron.findScrap(scrapName);
}
// Handles user input for interacting with the Apron
void Player::handleApronMenu() {
char choice; // Declare choice outside the loop
bool exitApron = false; // Flag to check if we should exit the apron menu
while (!exitApron) { // Loop until exit command is given
std::cout << "Welcome to your apron!\n";
std::cout << "Press 1 to list scraps in the apron.\n";
std::cout << "Press 2 to exit the apron menu.\n";
// Read the player's choice
std::cin >> choice;
if (choice == '1') {
listScrapsInApron(); // List all the scraps inside the apron
}
else if (choice == '2') {
std::cout << "Exiting the apron menu...\n";
exitApron = true; // Set the flag to exit the loop
}
else {
std::cout << "Invalid choice. Please try again.\n"; // Handle invalid input
}
}
}
// Destructor
Player::~Player() {
// Clean up the singleton instance
if (instance) {
delete instance;
instance = nullptr;
}
}