triggerComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5. #include "console/consoleTypes.h"
  6. #include "T3D/components/game/Triggercomponent.h"
  7. #include "core/util/safeDelete.h"
  8. #include "console/consoleTypes.h"
  9. #include "console/consoleObject.h"
  10. #include "core/stream/bitStream.h"
  11. #include "console/engineAPI.h"
  12. #include "sim/netConnection.h"
  13. #include "T3D/gameBase/gameConnection.h"
  14. #include "T3D/components/coreInterfaces.h"
  15. #include "math/mathUtils.h"
  16. #include "collision/concretePolyList.h"
  17. #include "collision/clippedPolyList.h"
  18. #include "gfx/sim/debugDraw.h"
  19. IMPLEMENT_CALLBACK( TriggerComponent, onEnterViewCmd, void,
  20. ( Entity* cameraEnt, bool firstTimeSeeing ), ( cameraEnt, firstTimeSeeing ),
  21. "@brief Called when an object enters the volume of the Trigger instance using this TriggerData.\n\n"
  22. "@param trigger the Trigger instance whose volume the object entered\n"
  23. "@param obj the object that entered the volume of the Trigger instance\n" );
  24. IMPLEMENT_CALLBACK( TriggerComponent, onExitViewCmd, void,
  25. ( Entity* cameraEnt ), ( cameraEnt ),
  26. "@brief Called when an object enters the volume of the Trigger instance using this TriggerData.\n\n"
  27. "@param trigger the Trigger instance whose volume the object entered\n"
  28. "@param obj the object that entered the volume of the Trigger instance\n" );
  29. IMPLEMENT_CALLBACK( TriggerComponent, onUpdateInViewCmd, void,
  30. ( Entity* cameraEnt ), ( cameraEnt ),
  31. "@brief Called when an object enters the volume of the Trigger instance using this TriggerData.\n\n"
  32. "@param trigger the Trigger instance whose volume the object entered\n"
  33. "@param obj the object that entered the volume of the Trigger instance\n" );
  34. IMPLEMENT_CALLBACK( TriggerComponent, onUpdateOutOfViewCmd, void,
  35. ( Entity* cameraEnt ), ( cameraEnt ),
  36. "@brief Called when an object enters the volume of the Trigger instance using this TriggerData.\n\n"
  37. "@param trigger the Trigger instance whose volume the object entered\n"
  38. "@param obj the object that entered the volume of the Trigger instance\n" );
  39. //////////////////////////////////////////////////////////////////////////
  40. // Constructor/Destructor
  41. //////////////////////////////////////////////////////////////////////////
  42. TriggerComponent::TriggerComponent() : Component()
  43. {
  44. mObjectList.clear();
  45. mVisible = false;
  46. mFriendlyName = "Trigger";
  47. mComponentType = "Trigger";
  48. mDescription = getDescriptionText("Calls trigger events when a client starts and stops seeing it. Also ticks while visible to clients.");
  49. }
  50. TriggerComponent::~TriggerComponent()
  51. {
  52. for(S32 i = 0;i < mFields.size();++i)
  53. {
  54. ComponentField &field = mFields[i];
  55. SAFE_DELETE_ARRAY(field.mFieldDescription);
  56. }
  57. SAFE_DELETE_ARRAY(mDescription);
  58. }
  59. IMPLEMENT_CO_NETOBJECT_V1(TriggerComponent);
  60. bool TriggerComponent::onAdd()
  61. {
  62. if(! Parent::onAdd())
  63. return false;
  64. return true;
  65. }
  66. void TriggerComponent::onRemove()
  67. {
  68. Parent::onRemove();
  69. }
  70. //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
  71. void TriggerComponent::onComponentAdd()
  72. {
  73. Parent::onComponentAdd();
  74. CollisionInterface *colInt = mOwner->getComponent<CollisionInterface>();
  75. if(colInt)
  76. {
  77. colInt->onCollisionSignal.notify(this, &TriggerComponent::potentialEnterObject);
  78. }
  79. }
  80. void TriggerComponent::onComponentRemove()
  81. {
  82. CollisionInterface *colInt = mOwner->getComponent<CollisionInterface>();
  83. if(colInt)
  84. {
  85. colInt->onCollisionSignal.remove(this, &TriggerComponent::potentialEnterObject);
  86. }
  87. Parent::onComponentRemove();
  88. }
  89. void TriggerComponent::componentAddedToOwner(Component *comp)
  90. {
  91. if (comp->getId() == getId())
  92. return;
  93. CollisionInterface *colInt = mOwner->getComponent<CollisionInterface>();
  94. if (colInt)
  95. {
  96. colInt->onCollisionSignal.notify(this, &TriggerComponent::potentialEnterObject);
  97. }
  98. }
  99. void TriggerComponent::componentRemovedFromOwner(Component *comp)
  100. {
  101. if (comp->getId() == getId()) //?????????
  102. return;
  103. CollisionInterface *colInt = mOwner->getComponent<CollisionInterface>();
  104. if (colInt)
  105. {
  106. colInt->onCollisionSignal.remove(this, &TriggerComponent::potentialEnterObject);
  107. }
  108. }
  109. void TriggerComponent::initPersistFields()
  110. {
  111. Parent::initPersistFields();
  112. addField("visibile", TypeBool, Offset( mVisible, TriggerComponent ), "" );
  113. addField("onEnterViewCmd", TypeCommand, Offset(mEnterCommand, TriggerComponent), "");
  114. addField("onExitViewCmd", TypeCommand, Offset(mOnExitCommand, TriggerComponent), "");
  115. addField("onUpdateInViewCmd", TypeCommand, Offset(mOnUpdateInViewCmd, TriggerComponent), "");
  116. }
  117. U32 TriggerComponent::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  118. {
  119. U32 retMask = Parent::packUpdate(con, mask, stream);
  120. return retMask;
  121. }
  122. void TriggerComponent::unpackUpdate(NetConnection *con, BitStream *stream)
  123. {
  124. Parent::unpackUpdate(con, stream);
  125. }
  126. void TriggerComponent::potentialEnterObject(SceneObject *collider)
  127. {
  128. if(testObject(collider))
  129. {
  130. bool found = false;
  131. for(U32 i=0; i < mObjectList.size(); i++)
  132. {
  133. if(mObjectList[i]->getId() == collider->getId())
  134. {
  135. found = true;
  136. break;
  137. }
  138. }
  139. if (!found)
  140. {
  141. mObjectList.push_back(collider);
  142. if (!mEnterCommand.isEmpty())
  143. {
  144. String command = String("%obj = ") + collider->getIdString() + ";" +
  145. String("%this = ") + getIdString() + ";" + mEnterCommand;
  146. Con::evaluate(command.c_str());
  147. }
  148. //onEnterTrigger_callback(this, enter);
  149. }
  150. }
  151. }
  152. bool TriggerComponent::testObject(SceneObject* enter)
  153. {
  154. //First, test to early out
  155. Box3F enterBox = enter->getWorldBox();
  156. //if(!mOwner->getWorldBox().intersect(enterBox) || !)
  157. // return false;
  158. //We're still here, so we should do actual work
  159. //We're going to be
  160. ConcretePolyList mClippedList;
  161. SphereF sphere;
  162. sphere.center = (mOwner->getWorldBox().minExtents + mOwner->getWorldBox().maxExtents) * 0.5;
  163. VectorF bv = mOwner->getWorldBox().maxExtents - sphere.center;
  164. sphere.radius = bv.len();
  165. Entity* enterEntity = dynamic_cast<Entity*>(enter);
  166. if(enterEntity)
  167. {
  168. //quick early out. If the bounds don't overlap, it cannot be colliding or inside
  169. if (!mOwner->getWorldBox().isOverlapped(enterBox))
  170. return false;
  171. //check if the entity has a collision shape
  172. CollisionInterface *cI = enterEntity->getComponent<CollisionInterface>();
  173. if (cI)
  174. {
  175. cI->buildPolyList(PLC_Collision, &mClippedList, mOwner->getWorldBox(), sphere);
  176. if (!mClippedList.isEmpty())
  177. {
  178. //well, it's clipped with, or inside, our bounds
  179. //now to test the clipped list against our own collision mesh
  180. CollisionInterface *myCI = mOwner->getComponent<CollisionInterface>();
  181. //wait, how would we NOT have this?
  182. if (myCI)
  183. {
  184. //anywho, build our list and then we'll check intersections
  185. ClippedPolyList myList;
  186. myList.setTransform(&(mOwner->getTransform()), mOwner->getScale());
  187. myList.setObject(mOwner);
  188. myCI->buildPolyList(PLC_Collision, &myList, enterBox, sphere);
  189. bool test = true;
  190. }
  191. }
  192. }
  193. }
  194. return mClippedList.isEmpty() == false;
  195. }
  196. void TriggerComponent::processTick()
  197. {
  198. Parent::processTick();
  199. if (!isActive())
  200. return;
  201. //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
  202. mVisible = false;
  203. if(isServerObject())
  204. {
  205. for(U32 i=0; i < mObjectList.size(); i++)
  206. {
  207. if(!testObject(mObjectList[i]))
  208. {
  209. if (!mOnExitCommand.isEmpty())
  210. {
  211. String command = String("%obj = ") + mObjectList[i]->getIdString() + ";" +
  212. String("%this = ") + getIdString() + ";" + mOnExitCommand;
  213. Con::evaluate(command.c_str());
  214. }
  215. mObjectList.erase(i);
  216. //mDataBlock->onLeaveTrigger_callback( this, remove );
  217. //onLeaveTrigger_callback(this, remove);
  218. }
  219. }
  220. /*if (!mTickCommand.isEmpty())
  221. Con::evaluate(mTickCommand.c_str());
  222. if (mObjects.size() != 0)
  223. onTickTrigger_callback(this);*/
  224. }
  225. }
  226. void TriggerComponent::visualizeFrustums(F32 renderTimeMS)
  227. {
  228. }
  229. GameConnection* TriggerComponent::getConnection(S32 connectionID)
  230. {
  231. for(NetConnection *conn = NetConnection::getConnectionList(); conn; conn = conn->getNext())
  232. {
  233. GameConnection* gameConn = dynamic_cast<GameConnection*>(conn);
  234. if (!gameConn || (gameConn && gameConn->isAIControlled()))
  235. continue;
  236. if(connectionID == gameConn->getId())
  237. return gameConn;
  238. }
  239. return NULL;
  240. }
  241. void TriggerComponent::addClient(S32 clientID)
  242. {
  243. }
  244. void TriggerComponent::removeClient(S32 clientID)
  245. {
  246. }
  247. DefineEngineMethod( TriggerComponent, addClient, void,
  248. ( S32 clientID ), ( -1 ),
  249. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  250. "@param objB Object to mount onto us\n"
  251. "@param slot Mount slot ID\n"
  252. "@param txfm (optional) mount offset transform\n"
  253. "@return true if successful, false if failed (objB is not valid)" )
  254. {
  255. if(clientID == -1)
  256. return;
  257. object->addClient( clientID );
  258. }
  259. DefineEngineMethod( TriggerComponent, removeClient, void,
  260. ( S32 clientID ), ( -1 ),
  261. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  262. "@param objB Object to mount onto us\n"
  263. "@param slot Mount slot ID\n"
  264. "@param txfm (optional) mount offset transform\n"
  265. "@return true if successful, false if failed (objB is not valid)" )
  266. {
  267. if(clientID == -1)
  268. return;
  269. object->removeClient( clientID );
  270. }
  271. DefineEngineMethod( TriggerComponent, visualizeFrustums, void,
  272. (F32 renderTime), (1000),
  273. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  274. "@param objB Object to mount onto us\n"
  275. "@param slot Mount slot ID\n"
  276. "@param txfm (optional) mount offset transform\n"
  277. "@return true if successful, false if failed (objB is not valid)" )
  278. {
  279. object->visualizeFrustums(renderTime);
  280. }