-
Notifications
You must be signed in to change notification settings - Fork 1
/
entity.cpp
224 lines (187 loc) · 5.76 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
220
221
222
223
224
#include "entity.h"
#include "entitycontainer.h"
#include <math.h>
#include <iostream>
Entity::Entity()
:Detectable()
{
resourceMgr = ResourceMgr::getInstance();
entityContainer = EntityContainer::getInstance();
}
Entity::Entity( EntityType eType, string name, int health, bool destructible, Point upperLeft, Point lowerRight,
Substance substance, DetectableType dType, bool fixed, bool moves )
:Detectable( upperLeft, lowerRight, substance, dType, fixed, moves )
{
this->eType = eType;
this->name = name;
this->health = health;
this->destructible = destructible;
resourceMgr = ResourceMgr::getInstance();
entityContainer = EntityContainer::getInstance();
}
Entity::Entity( EntityType eType, string name, int health, bool destructible, Point upperLeft, double width, double height,
Substance substance, DetectableType dType, bool fixed, bool moves )
:Detectable( upperLeft, width, height, substance, dType, fixed, moves )
{
this->eType = eType;
this->name = name;
this->health = health;
this->destructible = destructible;
resourceMgr = ResourceMgr::getInstance();
entityContainer = EntityContainer::getInstance();
}
Entity::Entity( EntityType eType, string name, int health, bool destructible, Point center, double radius,
Substance substance, DetectableType dType, bool fixed, bool moves )
:Detectable( center, radius, substance, dType, fixed, moves )
{
this->eType = eType;
this->name = name;
this->health = health;
this->destructible = destructible;
resourceMgr = ResourceMgr::getInstance();
entityContainer = EntityContainer::getInstance();
}
Entity::~Entity()
{
resourceMgr->release();
entityContainer->release();
}
string Entity::getName()
{
return name;
}
void Entity::insertStimResp( Stimulus stim, const string& targetName, Response resp )
{
StimulusResponseList.push_back( StimResp( stim, targetName, resp ) );
}
void Entity::handleFlags()
{
if( !touchedBy.empty() )
this->onTouch();
if( !unTouchedBy.empty() )
onUnTouch();
touchedBy.erase(touchedBy.begin(), touchedBy.end());
touchedInfo.erase(touchedInfo.begin(), touchedInfo.end());
unTouchedBy.erase(unTouchedBy.begin(), unTouchedBy.end());
}
Entity* Entity::getEntity( const string& entityName )
{
return entityContainer->getEntity( entityName );
}
const vector< Entity* >& Entity::getAllEntities() const
{
return entityContainer->getAllEntities();
}
void Entity::touch( const string& name )
{
touchedBy.push_back( name );
}
bool Entity::touched(const string name) const
{
for(int i = 0; i < touchedBy.size(); ++i)
if(touchedBy[i] == name)
return true;
return false;
}
void Entity::callFunction( const string& entityName, Response funcName )
{
//Find the pointer to the target node
Entity* targetEntity = getEntity( entityName );
//If the entity wasnt found end function
if( !targetEntity )
return;
//Call the specified function on the entity already found
Response function = funcName;
switch( function )
{
case OPEN:
targetEntity->open();
break;
case CLOSE:
targetEntity->close();
break;
}
}
void Entity::callFunction( const vector<StimResp>& srList, Stimulus stim )
{
//Traverse the vector looking for all instances that
//have the specified stimulus.
//Ex: Looks for all StimResp instances with a stimulus
// of onTouch. If stim == onTouch
for( int i = 0; i < srList.size(); ++i )
{
StimResp srInstance = srList[ i ];
//If the an instance has a matching stimulus call the
//response on the proper node.
if( srInstance.stimulus == stim )
callFunction( srInstance.targetName, srInstance.response );
}
}
EntityType Entity::getEntityType()
{
return eType;
}
const vector< Entity* >& Entity::getEntitiesOfType( EntityType eType ) const
{
return entityContainer->getEntitiesOfType( eType );//entitiesVec;
}
void Entity::draw()
{
if( dType == RECTANGLE )
{
glColor3f(0.0,0.0,0.0);
glBegin( GL_QUADS );
glVertex2f( upperLeft.getX() , upperLeft.getY() );
glVertex2f( upperRight.getX() , upperRight.getY() );
glVertex2f( lowerRight.getX() , lowerRight.getY() );
glVertex2f( lowerLeft.getX() , lowerLeft.getY() );
glEnd();
}
else
{
if(eType == BALL)
glColor3f(0.8, 0.8, 0.8);
if(eType == MAGNET)
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLE_FAN);
for(int i = 0; i <= 360; ++i)
glVertex2f(cos(i * 3.14 / 180.0) * getRadius() + center.getX(), sin(i * 3.14 / 180.0) * getRadius() + center.getY());
glEnd();
}
}
void Entity::wasTouchedBy(Detectable* d, Point loc1, Point loc2, double xAmount, double yAmount)
{
Entity* e = (Entity*)(d);
touchedBy.push_back( e->name );
touchedInfo.push_back(TouchedInfo(e->name, loc1, loc2, xAmount, yAmount));
}
void Entity::insertStimResp( const StimResp & stimResp )
{
StimulusResponseList.push_back( stimResp );
}
//ALL UNDEFINED FUNCTIONS INHERITED/DEFINED BY CHILDREN
void Entity::onTouch()
{
callFunction( StimulusResponseList, ONTOUCH );
}
void Entity::onUnTouch()
{
callFunction( StimulusResponseList, ONUNTOUCH );
}
void Entity::onAlways()
{
}
void Entity::onFrameEnd()
{
}
void Entity::open()
{
}
void Entity::close()
{
}
void Entity::printSR()
{
for( int i = 0; i < StimulusResponseList.size(); ++i )
cout << StimulusResponseList[i].stimulus << " " << StimulusResponseList[i].targetName << " " << StimulusResponseList[i].response << endl;
}