-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChest.cpp
More file actions
121 lines (107 loc) · 2.99 KB
/
Copy pathChest.cpp
File metadata and controls
121 lines (107 loc) · 2.99 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
#include <iostream>
#include <string>
#include <sstream>
#include "Chest.h"
#include "Player.h"
Chest::Chest(const string& name, const string& description, bool isLocked, int id) :
Item(name, ItemTypes::CHEST, description),
isLocked(isLocked),
id(id) { }
Chest::~Chest() {}
Item* Chest::getItem(const string& name) {
for (const auto& i : getContains()) {
if (i->getName() == name) {
// return a pointer to the item with the given name if it exists in the chest
return dynamic_cast<Item*>(i);
}
}
// return nullptr if not found or if entity is not an item
return nullptr;
}
int Chest::getId() const { return this->id; }
bool Chest::getIsLocked() const { return this->isLocked; }
void Chest::placeItem(Item* item) {
// if item is not nullptr -> add it to chest list
if (item) {
cout << item->getName() << " placed into the " << getName() << endl;
add(item);
return;
}
cout << "You can't place that" << endl;
}
void Chest::useChest(Player* player) {
// parses and executes the player's input command
string input;
cout << ">>>> ";
getline(cin, input);
istringstream iss(input);
string command, arg;
iss >> command;
bool bFirst = true;
string res = "";
// extracts and returns the remainig arguments from a command input stream | Example: "take wooden sword" - returns "wooden sword"
while (iss >> arg) {
if (bFirst) {
res += arg;
bFirst = false;
}
else {
res += ' ' + arg;
}
}
if (command == "take") {
Item* item = getItem(res);
if (!item) {
cout << "Item not in " << getName() << endl;
return;
}
// if item is nullptr print and do nothing
cout << item->getName() << " added to inventory!" << endl;
player->add(item);
remove(item);
// if item is found in the chest - add to player inventory and remove from chest
}
else if (command == "place") {
// get item from player inventory
Item* item = dynamic_cast<Item*>(player->getItem(res));
if (item->getEquipped()) {
player->unequip(item->getName());
}
// if item is equipped - unequip it
// if item is not nullptr - add it to the chest and remove from player inventory
if (item != nullptr) {
placeItem(item);
player->remove(item);
}
else {
cout << "You don't have that item." << endl;
}
}
else {
cout << "Invalid command." << endl;
}
// after each command - player closes the chest
cout << "You close the " << getName() << endl;
return;
}
void Chest::unlock(int keyId) {
if (!getIsLocked()) {
cout << getName() << " is not locked!" << endl;
return;
}
// if chest is not locked print and do nothing | if locked - unlock
this->isLocked = false;
cout << getName() << " successfully unlocked!" << endl;
}
void Chest::lock(int keyId) {
if (getIsLocked()) {
cout << getName() << " is already locked!" << endl;
return;
}
// if chest is locked print and do nothing | if unlocked - lock
this->isLocked = true;
cout << getName() << " successfully locked!" << endl;
}
string Chest::getInfo() const {
return "[" + string(getIsLocked() ? "Locked" : "Unlocked") + "]";
}