-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCharacter.cpp
More file actions
172 lines (146 loc) · 4.11 KB
/
Character.cpp
File metadata and controls
172 lines (146 loc) · 4.11 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
#include "Character.h"
Character::Character(){
setName(" ");
setHealth(0);
setPosition(actualRoom);
setAttack(0);
setMoney(0);
setProtection(0);
setHealthC(0);
}
Character::Character(std::string _name, int _health, Room* _actualRoom, int _attack, int _money, int _protection, int healthC){
setName(_name);
setHealth(_health);
setPosition(_actualRoom);
setAttack(_attack);
setMoney(_money);
setProtection(_protection);
setHealthC(healthC);
}
std::string Character::getName() const{
return name;
}
int Character::getHealth(){
return health;
}
Room* Character::getPosition(){
return actualRoom;
}
std::vector <Item*> Character::getInventory() const{
return inventory;
}
int Character::getAttack() const{
return attack;
}
int Character::getMoney() const{
return money;
}
int Character::getProtection() const{
return protection;
}
void Character::setName(std::string _name){
name = _name;
}
void Character::setHealth(int _health){
health = _health;
}
void Character::setPosition(Room* _actualRoom){
actualRoom = _actualRoom;
}
void Character::setInventory(std::vector <Item*> _inventory){
inventory = _inventory;
}
void Character::setAttack(int _attack){
attack = _attack;
}
void Character::setMoney(int _money){
money = _money;
}
void Character::setProtection(int _protection){
protection = _protection;
}
void Character::addItem(Item* _item){
int totalWeight = 0;
for(int i =0; i < inventory.size(); i++){
totalWeight += inventory[i]->getWeight();
};
totalWeight += _item->getWeight();
if (totalWeight <= 20){
inventory.push_back(_item);
} else {
std::cout << "The inventory is full, you can add an object of this weight" << std::endl;
}
}
void Character::itemDescription(std::string item){
for(int i =0; i < inventory.size(); i++){
if (inventory[i]->getName() == item){
inventory[i]->printItem();
}
}
}
void Character::printInventory(){
for(int i =0; i < inventory.size(); i++){
inventory[i]->printItem();
std::cout << "\n<---------------------------->\n" << std::endl;
}
}
void Character::print(){
std::cout << "Name: " << name << std::endl;
std::cout << "Health: " << health << std::endl;
std::cout << "Attack: " << attack << std::endl;
std::cout << "Protection: " << protection << std::endl;
std::cout << "Actual Room: " << actualRoom << std::endl;
std::cout << "Inventory: \n" << std::endl;
Character::printInventory();
}
bool Character::searchItem(std::string item){
for(int i=0; i< inventory.size(); i++){
if (inventory[i]->getName()==item){
return true;
}
}
return false;
}
bool Character::move(std::string direction){
Room* exit = actualRoom->getExit(direction);
if (exit !=nullptr && !exit ->keyRequired()){
setPosition(exit );
return true;
}
else if (exit !=nullptr && exit ->keyRequired()){
if (searchItem("Key")){
setPosition(exit);
return true;
}
}
return false;
}
Item* Character::getItem(std::string name){
for (int i = 0; i < inventory.size(); i++){
if (inventory[i]->getName() == name){
return inventory[i];
} else{
std::cout << "You don't have an Item called " << name << std::endl;
}
}
}
void Character::deleteItem(std::string item){
for (int i = 0; i < inventory.size(); i++){
if (inventory[i]->getName() == item){
auto elem_to_remove = inventory.begin() + i;
if (elem_to_remove != inventory.end()) {
inventory.erase(elem_to_remove);
}
}
}
}
void Character::setHealthC(int _healthc){
HealthC = _healthc;
}
int Character::getHealthC(){
return HealthC;
}
int Character::operator + (Item* ItemAttack){
int newAttack = attack + ItemAttack->execute();
return newAttack;
}