Trigger.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "graphics/dgl.h"
  23. #include "console/consoleTypes.h"
  24. #include "io/bitStream.h"
  25. #include "Trigger.h"
  26. // Script bindings.
  27. #include "Trigger_ScriptBinding.h"
  28. // Debug Profiling.
  29. #include "debug/profiler.h"
  30. //------------------------------------------------------------------------------
  31. IMPLEMENT_CONOBJECT(Trigger);
  32. //-----------------------------------------------------------------------------
  33. Trigger::Trigger()
  34. {
  35. // Setup some debug vector associations.
  36. VECTOR_SET_ASSOCIATION(mEnterColliders);
  37. VECTOR_SET_ASSOCIATION(mLeaveColliders);
  38. // Set default callbacks.
  39. mEnterCallback = true;
  40. mStayCallback = false;
  41. mLeaveCallback = true;
  42. // Use a static body by default.
  43. mBodyDefinition.type = b2_staticBody;
  44. // Gather contacts.
  45. mGatherContacts = true;
  46. }
  47. //-----------------------------------------------------------------------------
  48. bool Trigger::onAdd()
  49. {
  50. // Eventually, we'll need to deal with Server/Client functionality!
  51. // Call Parent.
  52. if(!Parent::onAdd())
  53. return false;
  54. // Return Okay.
  55. return true;
  56. }
  57. //-----------------------------------------------------------------------------
  58. void Trigger::initPersistFields()
  59. {
  60. addProtectedField("EnterCallback", TypeBool, Offset(mEnterCallback, Trigger), &setEnterCallback, &defaultProtectedGetFn, &writeEnterCallback,"");
  61. addProtectedField("StayCallback", TypeBool, Offset(mStayCallback, Trigger), &setStayCallback, &defaultProtectedGetFn, &writeStayCallback, "");
  62. addProtectedField("LeaveCallback", TypeBool, Offset(mLeaveCallback, Trigger), &setLeaveCallback, &defaultProtectedGetFn, &writeLeaveCallback, "");
  63. Parent::initPersistFields();
  64. }
  65. //-----------------------------------------------------------------------------
  66. void Trigger::preIntegrate(const F32 totalTime, const F32 elapsedTime, DebugStats *pDebugStats)
  67. {
  68. // Call Parent.
  69. Parent::preIntegrate(totalTime, elapsedTime, pDebugStats);
  70. // Clear Collider Callback Lists.
  71. mEnterColliders.clear();
  72. mLeaveColliders.clear();
  73. }
  74. //-----------------------------------------------------------------------------
  75. void Trigger::integrateObject( const F32 totalTime, const F32 elapsedTime, DebugStats *pDebugStats )
  76. {
  77. // Call Parent.
  78. Parent::integrateObject(totalTime, elapsedTime, pDebugStats);
  79. // Debug Profiling.
  80. PROFILE_SCOPE(Trigger_IntegrateObject);
  81. // Perform "OnEnter" callback.
  82. if ( mEnterCallback && mEnterColliders.size() > 0 )
  83. {
  84. // Debug Profiling.
  85. PROFILE_SCOPE(Trigger_OnEnterCallback);
  86. for ( collideCallbackType::iterator contactItr = mEnterColliders.begin(); contactItr != mEnterColliders.end(); ++contactItr )
  87. {
  88. Con::executef(this, 2, "onEnter", (*contactItr)->getIdString());
  89. }
  90. }
  91. // Fetch current contacts.
  92. const Scene::typeContactVector* pCurrentContacts = getCurrentContacts();
  93. // Sanity!
  94. AssertFatal( pCurrentContacts != NULL, "Trigger::integrateObject() - Contacts not initialized correctly." );
  95. // Perform "OnStay" callback.
  96. if ( mStayCallback && pCurrentContacts->size() > 0 )
  97. {
  98. // Debug Profiling.
  99. PROFILE_SCOPE(Trigger_OnStayCallback);
  100. for ( Scene::typeContactVector::const_iterator contactItr = pCurrentContacts->begin(); contactItr != pCurrentContacts->end(); ++contactItr )
  101. {
  102. // Fetch colliding object.
  103. SceneObject* pCollideWidth = contactItr->getCollideWith( this );
  104. Con::executef(this, 2, "onStay", pCollideWidth->getIdString());
  105. }
  106. }
  107. // Perform "OnLeave" callback.
  108. if ( mLeaveCallback && mLeaveColliders.size() > 0 )
  109. {
  110. // Debug Profiling.
  111. PROFILE_SCOPE(Trigger_OnLeaveCallback);
  112. for ( collideCallbackType::iterator contactItr = mLeaveColliders.begin(); contactItr != mLeaveColliders.end(); ++contactItr )
  113. {
  114. Con::executef(this, 2, "onLeave", (*contactItr)->getIdString());
  115. }
  116. }
  117. }
  118. //-----------------------------------------------------------------------------
  119. void Trigger::onBeginCollision( const TickContact& tickContact )
  120. {
  121. // Call parent.
  122. Parent::onBeginCollision( tickContact );
  123. // Add to enter colliders.
  124. mEnterColliders.push_back( tickContact.getCollideWith( this ) );
  125. }
  126. //-----------------------------------------------------------------------------
  127. void Trigger::onEndCollision( const TickContact& tickContact )
  128. {
  129. // Call parent.
  130. Parent::onEndCollision( tickContact );
  131. // Add to leave colliders.
  132. mLeaveColliders.push_back( tickContact.getCollideWith( this ) );
  133. }
  134. //-----------------------------------------------------------------------------
  135. void Trigger::copyTo(SimObject* object)
  136. {
  137. Parent::copyTo(object);
  138. AssertFatal(dynamic_cast<Trigger*>(object), "Trigger::copyTo() - Object is not the correct type.");
  139. Trigger* trigger = static_cast<Trigger*>(object);
  140. trigger->mEnterCallback = mEnterCallback;
  141. trigger->mStayCallback = mStayCallback;
  142. trigger->mLeaveCallback = mLeaveCallback;
  143. }