-
Notifications
You must be signed in to change notification settings - Fork 0
/
character.cpp
72 lines (63 loc) · 1.7 KB
/
character.cpp
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
#include <string>
#include <iostream>
#include "character.h"
#include "room.h"
#include "door.h"
#include "game.h"
#include "player.h"
#include "container.h"
using std::string;
using std::cerr;
using std::endl;
bool Character::enter(std::string dir) {
Door* exit = location->getExit(dir);
if (exit == NULL) {
return false;
} else {
Room* newLocation = exit->getConnectedRoom(location);
if (newLocation != NULL) {
location->removeActor(this);
location = newLocation;
location->addActor(this);
if (g->player->getLocation() == location) {
addFlag("playerishere");
} else if(checkFlag("playerishere")){
removeFlag("playerishere");
}
triggerEnter(newLocation);
return true;
} else {
g->push(exit->getLockedMessage());
return true;
}
}
}
//number of ';'-separated words used by the constructor
const int Character::C_USED_WORDS = Actor::C_USED_WORDS + 1;
Character::Character(Game* instance, Room* l, std::string data) : Actor(instance,data), location(l) {
_inventory = new Container(instance);
int i = Actor::C_USED_WORDS;
health = stoi(split(data, ';')[i]);
}
Character::~Character(){
/* for(auto it = _inventory.begin(); it != _inventory.end(); ++it){
delete *it;
}*/
delete _inventory;
}
void Character::triggerEnter(Room* r) {
doEvent("onEnter#"+r->getName(),"");
}
string Character::serialize() const{
string result = Actor::serialize();
result += ";";
result += std::to_string(health);
result += "\n";
auto items = getInventory()->getItems();
for (auto it = items.begin(); it != items.end();++it) {
result += (*it)->serialize();
result += "\n";
}
result += "ENDCHAR";
return result;
}