-
Notifications
You must be signed in to change notification settings - Fork 1
/
Entity.cpp
219 lines (184 loc) · 4.69 KB
/
Entity.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
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
//Entity.cpp: implements functions declared in Entity.h
#include "Entity.h"
#include <iostream>
#include <string>
#include <vector>
#include <ostream>
#include <ncurses.h>
using namespace std;
extern int g_iID;
//Constructor
Entity::Entity()
{
g_iID++;
m_iID = g_iID;
// addstr("Entity constructed! Heres the ID: ");
// addstr(to_string(m_iID).c_str());
// addch('\n');
m_sName = "No-Name Entity!";
m_vProperties; //do something later w this
m_cDisplayChar = 'E';
m_pLocation = new Point(0, 0);
}
//Destructor
Entity::~Entity()
{
//Deallocate memory
delete m_pLocation;
m_pLocation = NULL;
}
//Copy constructor. what a horrible thing
Entity::Entity(const Entity& anEntity)
{
m_iID = g_iID++;
m_sName = anEntity.getName();
m_vProperties = anEntity.getProperties();
m_cDisplayChar = anEntity.getDisplayChar();
m_pLocation = new Point(*(anEntity.getLocation()));
}
//Getters
string Entity::getName() const
{
return m_sName;
}
vector<string> Entity::getProperties() const
{
return m_vProperties;
}
char Entity::getDisplayChar() const
{
// cerr << "Called getDisplayChar()" << endl; //debug
return m_cDisplayChar;
}
Point* Entity::getLocation() const
{
return m_pLocation;
}
int Entity::getID() const
{
return m_iID;
}
//Setters
void Entity::setName(string sName)
{
m_sName = sName;
}
void Entity::setProperties(vector<string> vProperties)
{
m_vProperties = vProperties;
}
void Entity::setDisplayChar(char cDisplayChar)
{
m_cDisplayChar = cDisplayChar;
}
void Entity::setLocation(Point* pLocation)
{
m_pLocation = pLocation;
}
//Other methods
void Entity::addProperty(string sProperty)
{
m_vProperties.push_back(sProperty);
}
void Entity::changeLocation(DungeonLevel* dlLevel, Point* pNewLocation)
{
// cout << "Entities change location was called. this is bad." << endl;
}
/*
void Entity::changeLocation(DungeonLevel* dlLevel, Point* pNewLocation)
{
// cout << "changeLocation(): called" << endl; //debug
Creature* pImACreature = dynamic_cast<Creature*>(this);
Item* pImAnItem = dynamic_cast<Item*>(this);
//When an entity changes location, 3 things need to happen.
//1. Tell the old tile we're moving.
//See note 6
dlLevel->getTileAt(getLocation())->setCreature(NULL);
//2. Delete the old location, and move
delete getLocation();
setLocation(pNewLocation);
//3. Tell the new tile we're here
if (pImACreature != NULL)
dlLevel->getTileAt(pNewLocation)->setCreature(pImACreature);
else
dlLevel->getTileAt(pNewLocation)->add(pImAnItem);
// cout << "changeLocation(): completed" << endl;
}
*/
bool Entity::hasProperty(string sProperty)
{
for(vector<string>::iterator itr = m_vProperties.begin(); itr != m_vProperties.end(); itr++) //search the vector
{
string sCurrentProperty = *itr;
if(sCurrentProperty == sProperty) //for sProperty
{
return true; //if you find it, return true
}
}
return false; //if you don't, return false
}
string Entity::getDescription()
{
return "This is supposed to print a description eventually!";
}
void Entity::dumpObject()
{
cout << "OBJECT TYPE: Entity" << endl;
dumpObjectData();
}
void Entity::dumpObjectData()
{
cout << "\t - m_sName: " << getName() << endl;
cout << "\t - m_cDisplayChar: " << getDisplayChar() << endl;
cout << "\t - m_vProperties: ";
for (vector<string>::iterator itr = m_vProperties.begin(); itr != m_vProperties.end(); itr++)
{
cout << *itr << " " << endl;
}
cout << endl;
}
void Entity::writeFragment(ostream & output, int iTabs)
{
insertTabs(output, iTabs);
output << "<Entity>" << endl;
writeDataAsFragment(output, iTabs + 1);
insertTabs(output, iTabs);
output << "</Entity>" << endl;
}
void Entity::writeDataAsFragment(ostream & output, int iTabs)
{
insertTabs(output, iTabs);
output << "<name>" << getName() << "</name>" << endl;
if (m_vProperties.size() != 0)
{
insertTabs(output, iTabs);
output << "<properties>" << endl;
for (vector<string>::iterator itr = m_vProperties.begin(); itr != m_vProperties.end(); itr++)
{
insertTabs(output, iTabs);
cout << "<property>" << *itr << "</property>" << endl;
}
insertTabs(output, iTabs);
output << "</properties>" << endl;
}
insertTabs(output, iTabs);
output << "<displayChar>" << getDisplayChar() << "</displayChar>" << endl;
//See notes.txt, note 1
//output << "<location>(" << getLocation().getX() << ", " << getLocation().getY() << ")</location>" << endl;
}
void Entity::setElementData(string sElementName, string sValue)
{
if (sElementName == "name")
setName(sValue);
else if (sElementName == "displayChar")
setDisplayChar(sValue.at(0));
else if (sElementName == "property")
addProperty(sValue);
}
void Entity::insertTabs(ostream & output, int iTabs)
{
for (int i = 0; i < iTabs; i++)
{
output << "\t";
}
}