afxEA_CollisionEvent.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include <typeinfo>
  25. #include "afx/arcaneFX.h"
  26. #include "afx/afxEffectDefs.h"
  27. #include "afx/afxEffectWrapper.h"
  28. #include "afx/afxChoreographer.h"
  29. #include "afx/ce/afxCollisionEvent.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. // afxEA_CollisionEvent
  32. class afxEA_CollisionEvent : public afxEffectWrapper, ShapeBase::CollisionEventCallback
  33. {
  34. typedef afxEffectWrapper Parent;
  35. afxCollisionEventData* script_data;
  36. ShapeBase* shape;
  37. U32 trigger_mask;
  38. bool triggered;
  39. void do_runtime_substitutions();
  40. void set_shape(ShapeBase*);
  41. public:
  42. /*C*/ afxEA_CollisionEvent();
  43. /*D*/ ~afxEA_CollisionEvent();
  44. virtual void ea_set_datablock(SimDataBlock*);
  45. virtual bool ea_start();
  46. virtual bool ea_update(F32 dt);
  47. virtual void ea_finish(bool was_stopped);
  48. virtual void collisionNotify(SceneObject* obj0, SceneObject* obj1, const VectorF& vel);
  49. virtual void onDeleteNotify(SimObject*);
  50. };
  51. //~~~~~~~~~~~~~~~~~~~~//
  52. afxEA_CollisionEvent::afxEA_CollisionEvent()
  53. {
  54. script_data = 0;
  55. shape = 0;
  56. trigger_mask = 0;
  57. triggered = false;
  58. }
  59. afxEA_CollisionEvent::~afxEA_CollisionEvent()
  60. {
  61. if (shape)
  62. clearNotify(shape);
  63. if (script_data && script_data->isTempClone())
  64. delete script_data;
  65. script_data = 0;
  66. }
  67. void afxEA_CollisionEvent::ea_set_datablock(SimDataBlock* db)
  68. {
  69. script_data = dynamic_cast<afxCollisionEventData*>(db);
  70. }
  71. bool afxEA_CollisionEvent::ea_start()
  72. {
  73. if (!script_data)
  74. {
  75. Con::errorf("afxEA_CollisionEvent::ea_start() -- missing or incompatible datablock.");
  76. return false;
  77. }
  78. do_runtime_substitutions();
  79. if (script_data->gen_trigger && script_data->trigger_bit < 32)
  80. trigger_mask = 1 << script_data->trigger_bit;
  81. else
  82. trigger_mask = 0;
  83. triggered = false;
  84. return true;
  85. }
  86. bool afxEA_CollisionEvent::ea_update(F32 dt)
  87. {
  88. afxConstraint* pos_constraint = getPosConstraint();
  89. set_shape((pos_constraint) ? dynamic_cast<ShapeBase*>(pos_constraint->getSceneObject()) : 0);
  90. if (mChoreographer && trigger_mask != 0)
  91. {
  92. if (triggered)
  93. {
  94. mChoreographer->setTriggerMask(trigger_mask | mChoreographer->getTriggerMask());
  95. triggered = false;
  96. }
  97. else
  98. {
  99. mChoreographer->setTriggerMask(~trigger_mask & mChoreographer->getTriggerMask());
  100. }
  101. }
  102. return true;
  103. }
  104. void afxEA_CollisionEvent::ea_finish(bool was_stopped)
  105. {
  106. set_shape(0);
  107. }
  108. void afxEA_CollisionEvent::do_runtime_substitutions()
  109. {
  110. // only clone the datablock if there are substitutions
  111. if (script_data->getSubstitutionCount() > 0)
  112. {
  113. // clone the datablock and perform substitutions
  114. afxCollisionEventData* orig_db = script_data;
  115. script_data = new afxCollisionEventData(*orig_db, true);
  116. orig_db->performSubstitutions(script_data, mChoreographer, mGroup_index);
  117. }
  118. }
  119. void afxEA_CollisionEvent::set_shape(ShapeBase* new_shape)
  120. {
  121. if (shape == new_shape)
  122. return;
  123. if (shape)
  124. {
  125. shape->unregisterCollisionCallback(this);
  126. clearNotify(shape);
  127. }
  128. shape = new_shape;
  129. if (shape)
  130. {
  131. deleteNotify(shape);
  132. shape->registerCollisionCallback(this);
  133. }
  134. }
  135. void afxEA_CollisionEvent::collisionNotify(SceneObject* obj0, SceneObject* obj1, const VectorF& vel)
  136. {
  137. if (obj0 != shape || !mChoreographer || !mChoreographer->getDataBlock())
  138. return;
  139. if (script_data->method_name != ST_NULLSTRING)
  140. {
  141. char *arg_buf = Con::getArgBuffer(64);
  142. dSprintf(arg_buf, 256, "%g %g %g", vel.x, vel.y, vel.z);
  143. // CALL SCRIPT afxChoreographerData::method(%spell, %obj0, %obj1, %velocity)
  144. Con::executef(mChoreographer->getDataBlock(), script_data->method_name,
  145. mChoreographer->getIdString(),
  146. (obj0) ? obj0->getIdString() : "",
  147. (obj1) ? obj1->getIdString() : "",
  148. arg_buf,
  149. script_data->script_data);
  150. }
  151. if (!triggered && trigger_mask != 0)
  152. triggered = true;
  153. }
  154. void afxEA_CollisionEvent::onDeleteNotify(SimObject* obj)
  155. {
  156. if (obj == shape)
  157. {
  158. shape->unregisterCollisionCallback(this);
  159. shape = 0;
  160. }
  161. Parent::onDeleteNotify(obj);
  162. }
  163. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  164. class afxEA_CollisionEventDesc : public afxEffectAdapterDesc, public afxEffectDefs
  165. {
  166. static afxEA_CollisionEventDesc desc;
  167. public:
  168. virtual bool testEffectType(const SimDataBlock*) const;
  169. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  170. virtual bool runsOnServer(const afxEffectWrapperData*) const { return true; }
  171. virtual bool runsOnClient(const afxEffectWrapperData*) const { return false; }
  172. virtual bool isPositional(const afxEffectWrapperData*) const { return false; }
  173. virtual afxEffectWrapper* create() const { return new afxEA_CollisionEvent; }
  174. };
  175. afxEA_CollisionEventDesc afxEA_CollisionEventDesc::desc;
  176. bool afxEA_CollisionEventDesc::testEffectType(const SimDataBlock* db) const
  177. {
  178. return (typeid(afxCollisionEventData) == typeid(*db));
  179. }
  180. bool afxEA_CollisionEventDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  181. {
  182. return (timing.lifetime < 0);
  183. }
  184. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//