-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.cpp
190 lines (166 loc) · 5.05 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
/**
* Implementation of the entity class declared in Entity.h
*
* Author: Skylar Payne
* Date: 6/8/2013
* File: Entity.cpp
**/
#include "Entity.h"
#include <cstring>
#include "Logger.h"
/**
* @brief Entity::~Entity Deallocates all components and behaviors
*/
Entity::~Entity()
{
std::list<IComponent*>::iterator cit;
for(cit = _Components.begin(); cit != _Components.end(); cit++)
{
if(*cit)
{
delete (*cit);
(*cit) = nullptr;
}
}
_Components.clear();
std::list<IBehavior*>::iterator bit;
for(bit = _Behaviors.begin(); bit != _Behaviors.end(); bit++)
{
if(*bit)
{
delete (*bit);
(*bit) = nullptr;
}
}
_Behaviors.clear();
}
/**
* @brief Entity::FindComponent finds a specified component
* @param type the type of component to look for
* @param loc the location of the component if found
* @return true if the component was found, false otherwise
*/
bool Entity::FindComponent(const char* type, std::list<IComponent*>::iterator &loc)
{
for(loc = _Components.begin(); loc != _Components.end(); loc++)
{
if(strcmp((*loc)->GetType(), type) == 0)
{
return true;
}
}
return false;
}
/**
* @brief Entity::FindBehavior finds a particular behavior
* @param type the type of behavior to find
* @param loc the location of the behavior, if found
* @return true if the behavior was found, false otherwise
*/
bool Entity::FindBehavior(const char* type, std::list<IBehavior*>::iterator& loc)
{
for(loc = _Behaviors.begin(); loc != _Behaviors.end(); loc++)
{
if(strcmp((*loc)->GetType(), type) == 0)
{
return true;
}
}
return false;
}
/**
* @brief Entity::AttachComponent Attaches a component to the entity if a component of that type is not already attached
* @param comp The component to attach
* @return true if the new component was attached, false otherwise.
**/
bool Entity::AttachComponent(IComponent* comp)
{
if(comp == nullptr)
{
g_Logger << __FILE__ << ": " << __LINE__ << "-Error: component pointer was nullptr\n";
return false;
}
std::list<IComponent*>::iterator it;
if(this->FindComponent(comp->GetType(), it))
{
g_Logger << __FILE__ << ": " << __LINE__ << "-Error: " << comp->GetType() << " already attached to Entity" << this->_ID << "\n";
return false;
}
_Components.push_back(comp);
comp->_Parent = this;
g_Logger << __FILE__ << ": " << __LINE__ << "-" << comp->GetType() << " was added to Entity " << this->_ID << "\n";
return true;
}
/**
* @brief Entity::RemoveComponent Removes a type component from an entity if that entity has that component
* @param type The component type to remove.
**/
void Entity::RemoveComponent(const char* type)
{
std::list<IComponent*>::iterator it;
if(this->FindComponent(type, it))
{
_Components.erase(it);
g_Logger << __FILE__ << ": " << __LINE__ << "-" << type << " was removed from Entity " << this->_ID << "\n";
}
}
/**
* @brief Entity::HasComponent Finds a particular component
* @param type The type of component to find
* @return true if the component was found, false otherwise
**/
bool Entity::HasComponent(const char* type)
{
std::list<IComponent*>::iterator it;
return this->FindComponent(type, it);
}
/**
* @brief Entity::AttachBehavior attaches a behavior to an entity
* @param beh the behavior to attach
* @return true if the behavior was attached, false otherwise
*/
bool Entity::AttachBehavior(IBehavior* beh)
{
if(beh == nullptr)
{
g_Logger << __FILE__ << ": " << __LINE__ << "-Error: behavior pointer was nullptr\n";
return false;
}
std::list<IBehavior*>::iterator it;
if(this->FindBehavior(beh->GetType(), it))
{
g_Logger << __FILE__ << ": " << __LINE__ << "-Error: " << beh->GetType() << " already attached to Entity" << this->_ID << "\n";
return false;
}
_Behaviors.push_back(beh);
beh->_Parent = this;
g_Logger << __FILE__ << ": " << __LINE__ << "-" << beh->GetType() << " was added to Entity " << this->_ID << "\n";
return true;
}
/**
* @brief Entity::RemoveBehavior removes a behavior
* @param type the type of behavior to remove
*/
void Entity::RemoveBehavior(const char* type)
{
std::list<IBehavior*>::iterator it;
if(this->FindBehavior(type, it))
{
_Behaviors.erase(it);
g_Logger << __FILE__ << ": " << __LINE__ << "-" << type << " was removed from Entity " << this->_ID << "\n";
}
}
/**
* @brief Entity::GetBehavior Gets a specified behavior from the entity
* @param type the type of behavior
* @return the behavior, if found, nullptr otherwise.
*/
IBehavior* Entity::GetBehavior(const char *type)
{
std::list<IBehavior*>::iterator it;
if(this->FindBehavior(type, it))
{
return (*it);
}
return nullptr;
}