-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtriggerComponent.cpp
357 lines (280 loc) · 10.7 KB
/
triggerComponent.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
#include "console/consoleTypes.h"
#include "T3D/components/game/triggerComponent.h"
#include "core/util/safeDelete.h"
#include "console/consoleTypes.h"
#include "console/consoleObject.h"
#include "core/stream/bitStream.h"
#include "console/engineAPI.h"
#include "sim/netConnection.h"
#include "T3D/gameBase/gameConnection.h"
#include "T3D/components/coreInterfaces.h"
#include "math/mathUtils.h"
#include "collision/concretePolyList.h"
#include "collision/clippedPolyList.h"
#include "gfx/sim/debugDraw.h"
IMPLEMENT_CALLBACK( TriggerComponent, onEnterView, void,
( Entity* cameraEnt, bool firstTimeSeeing ), ( cameraEnt, firstTimeSeeing ),
"@brief Called when an object enters the volume of the Trigger instance using this TriggerData.\n\n"
"@param trigger the Trigger instance whose volume the object entered\n"
"@param obj the object that entered the volume of the Trigger instance\n" );
IMPLEMENT_CALLBACK( TriggerComponent, onExitView, void,
( Entity* cameraEnt ), ( cameraEnt ),
"@brief Called when an object enters the volume of the Trigger instance using this TriggerData.\n\n"
"@param trigger the Trigger instance whose volume the object entered\n"
"@param obj the object that entered the volume of the Trigger instance\n" );
IMPLEMENT_CALLBACK( TriggerComponent, onUpdateInView, void,
( Entity* cameraEnt ), ( cameraEnt ),
"@brief Called when an object enters the volume of the Trigger instance using this TriggerData.\n\n"
"@param trigger the Trigger instance whose volume the object entered\n"
"@param obj the object that entered the volume of the Trigger instance\n" );
IMPLEMENT_CALLBACK( TriggerComponent, onUpdateOutOfView, void,
( Entity* cameraEnt ), ( cameraEnt ),
"@brief Called when an object enters the volume of the Trigger instance using this TriggerData.\n\n"
"@param trigger the Trigger instance whose volume the object entered\n"
"@param obj the object that entered the volume of the Trigger instance\n" );
//////////////////////////////////////////////////////////////////////////
// Constructor/Destructor
//////////////////////////////////////////////////////////////////////////
TriggerComponent::TriggerComponent() : Component()
{
mObjectList.clear();
mVisible = false;
mFriendlyName = "Trigger";
mComponentType = "Trigger";
mDescription = getDescriptionText("Calls trigger events when a client starts and stops seeing it. Also ticks while visible to clients.");
}
TriggerComponent::~TriggerComponent()
{
for(S32 i = 0;i < mFields.size();++i)
{
ComponentField &field = mFields[i];
SAFE_DELETE_ARRAY(field.mFieldDescription);
}
SAFE_DELETE_ARRAY(mDescription);
}
IMPLEMENT_CO_NETOBJECT_V1(TriggerComponent);
bool TriggerComponent::onAdd()
{
if(! Parent::onAdd())
return false;
return true;
}
void TriggerComponent::onRemove()
{
Parent::onRemove();
}
//This is mostly a catch for situations where the behavior is re-added to the object and the like and we may need to force an update to the behavior
void TriggerComponent::onComponentAdd()
{
Parent::onComponentAdd();
CollisionInterface *colInt = mOwner->getComponent<CollisionInterface>();
if(colInt)
{
colInt->onCollisionSignal.notify(this, &TriggerComponent::potentialEnterObject);
}
}
void TriggerComponent::onComponentRemove()
{
CollisionInterface *colInt = mOwner->getComponent<CollisionInterface>();
if(colInt)
{
colInt->onCollisionSignal.remove(this, &TriggerComponent::potentialEnterObject);
}
Parent::onComponentRemove();
}
void TriggerComponent::componentAddedToOwner(Component *comp)
{
if (comp->getId() == getId())
return;
CollisionInterface *colInt = mOwner->getComponent<CollisionInterface>();
if (colInt)
{
colInt->onCollisionSignal.notify(this, &TriggerComponent::potentialEnterObject);
}
}
void TriggerComponent::componentRemovedFromOwner(Component *comp)
{
if (comp->getId() == getId()) //?????????
return;
CollisionInterface *colInt = mOwner->getComponent<CollisionInterface>();
if (colInt)
{
colInt->onCollisionSignal.remove(this, &TriggerComponent::potentialEnterObject);
}
}
void TriggerComponent::initPersistFields()
{
Parent::initPersistFields();
addField("visibile", TypeBool, Offset( mVisible, TriggerComponent ), "" );
addField("onEnterViewCmd", TypeCommand, Offset(mOnEnterViewCmd, TriggerComponent), "");
addField("onExitViewCmd", TypeCommand, Offset(mOnExitViewCmd, TriggerComponent), "");
addField("onUpdateInViewCmd", TypeCommand, Offset(mOnUpdateInViewCmd, TriggerComponent), "");
}
U32 TriggerComponent::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
{
U32 retMask = Parent::packUpdate(con, mask, stream);
return retMask;
}
void TriggerComponent::unpackUpdate(NetConnection *con, BitStream *stream)
{
Parent::unpackUpdate(con, stream);
}
void TriggerComponent::potentialEnterObject(SceneObject *collider)
{
if(testObject(collider))
{
bool found = false;
for(U32 i=0; i < mObjectList.size(); i++)
{
if(mObjectList[i]->getId() == collider->getId())
{
found = true;
break;
}
}
if (!found)
{
mObjectList.push_back(collider);
if (!mOnEnterViewCmd.isEmpty())
{
String command = String("%obj = ") + collider->getIdString() + ";" +
String("%this = ") + getIdString() + ";" + mOnEnterViewCmd;
Con::evaluate(command.c_str());
}
//onEnterTrigger_callback(this, enter);
}
}
}
bool TriggerComponent::testObject(SceneObject* enter)
{
//First, test to early out
Box3F enterBox = enter->getWorldBox();
//if(!mOwner->getWorldBox().intersect(enterBox) || !)
// return false;
//We're still here, so we should do actual work
//We're going to be
ConcretePolyList mClippedList;
SphereF sphere;
sphere.center = (mOwner->getWorldBox().minExtents + mOwner->getWorldBox().maxExtents) * 0.5;
VectorF bv = mOwner->getWorldBox().maxExtents - sphere.center;
sphere.radius = bv.len();
Entity* enterEntity = dynamic_cast<Entity*>(enter);
if(enterEntity)
{
//quick early out. If the bounds don't overlap, it cannot be colliding or inside
if (!mOwner->getWorldBox().isOverlapped(enterBox))
return false;
//check if the entity has a collision shape
CollisionInterface *cI = enterEntity->getComponent<CollisionInterface>();
if (cI)
{
cI->buildPolyList(PLC_Collision, &mClippedList, mOwner->getWorldBox(), sphere);
if (!mClippedList.isEmpty())
{
//well, it's clipped with, or inside, our bounds
//now to test the clipped list against our own collision mesh
CollisionInterface *myCI = mOwner->getComponent<CollisionInterface>();
//wait, how would we NOT have this?
if (myCI)
{
//anywho, build our list and then we'll check intersections
ClippedPolyList myList;
MatrixF ownerTransform = mOwner->getTransform();
myList.setTransform(&ownerTransform, mOwner->getScale());
myList.setObject(mOwner);
myCI->buildPolyList(PLC_Collision, &myList, enterBox, sphere);
}
}
}
}
return mClippedList.isEmpty() == false;
}
void TriggerComponent::processTick()
{
Parent::processTick();
if (!isActive())
return;
//get our list of active clients, and see if they have cameras, if they do, build a frustum and see if we exist inside that
mVisible = false;
if(isServerObject())
{
for(U32 i=0; i < mObjectList.size(); i++)
{
if(!testObject(mObjectList[i]))
{
if (!mOnExitViewCmd.isEmpty())
{
String command = String("%obj = ") + mObjectList[i]->getIdString() + ";" +
String("%this = ") + getIdString() + ";" + mOnExitViewCmd;
Con::evaluate(command.c_str());
}
mObjectList.erase(i);
//mDataBlock->onLeaveTrigger_callback( this, remove );
//onLeaveTrigger_callback(this, remove);
}
}
/*if (!mTickCommand.isEmpty())
Con::evaluate(mTickCommand.c_str());
if (mObjects.size() != 0)
onTickTrigger_callback(this);*/
}
}
void TriggerComponent::visualizeFrustums(F32 renderTimeMS)
{
}
GameConnection* TriggerComponent::getConnection(S32 connectionID)
{
for(NetConnection *conn = NetConnection::getConnectionList(); conn; conn = conn->getNext())
{
GameConnection* gameConn = dynamic_cast<GameConnection*>(conn);
if (!gameConn || (gameConn && gameConn->isAIControlled()))
continue;
if(connectionID == gameConn->getId())
return gameConn;
}
return NULL;
}
void TriggerComponent::addClient(S32 clientID)
{
}
void TriggerComponent::removeClient(S32 clientID)
{
}
DefineEngineMethod( TriggerComponent, addClient, void,
( S32 clientID ), ( -1 ),
"@brief Mount objB to this object at the desired slot with optional transform.\n\n"
"@param objB Object to mount onto us\n"
"@param slot Mount slot ID\n"
"@param txfm (optional) mount offset transform\n"
"@return true if successful, false if failed (objB is not valid)" )
{
if(clientID == -1)
return;
object->addClient( clientID );
}
DefineEngineMethod( TriggerComponent, removeClient, void,
( S32 clientID ), ( -1 ),
"@brief Mount objB to this object at the desired slot with optional transform.\n\n"
"@param objB Object to mount onto us\n"
"@param slot Mount slot ID\n"
"@param txfm (optional) mount offset transform\n"
"@return true if successful, false if failed (objB is not valid)" )
{
if(clientID == -1)
return;
object->removeClient( clientID );
}
DefineEngineMethod( TriggerComponent, visualizeFrustums, void,
(F32 renderTime), (1000),
"@brief Mount objB to this object at the desired slot with optional transform.\n\n"
"@param objB Object to mount onto us\n"
"@param slot Mount slot ID\n"
"@param txfm (optional) mount offset transform\n"
"@return true if successful, false if failed (objB is not valid)" )
{
object->visualizeFrustums(renderTime);
}