afxEA_PhysicalZone.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "T3D/physicalZone.h"
  27. #include "afx/afxEffectDefs.h"
  28. #include "afx/afxEffectWrapper.h"
  29. #include "afx/afxChoreographer.h"
  30. #include "afx/ce/afxPhysicalZone.h"
  31. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  32. // afxEA_PhysicalZone
  33. class afxEA_PhysicalZone : public afxEffectWrapper
  34. {
  35. typedef afxEffectWrapper Parent;
  36. afxPhysicalZoneData* zone_data;
  37. PhysicalZone* physical_zone;
  38. SceneObject* cons_obj;
  39. void do_runtime_substitutions();
  40. void set_cons_object(SceneObject*);
  41. public:
  42. /*C*/ afxEA_PhysicalZone();
  43. /*D*/ ~afxEA_PhysicalZone();
  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 ea_set_scope_status(bool flag);
  49. virtual void onDeleteNotify(SimObject*);
  50. };
  51. //~~~~~~~~~~~~~~~~~~~~//
  52. afxEA_PhysicalZone::afxEA_PhysicalZone()
  53. {
  54. zone_data = 0;
  55. physical_zone = 0;
  56. cons_obj = 0;
  57. }
  58. afxEA_PhysicalZone::~afxEA_PhysicalZone()
  59. {
  60. if (physical_zone)
  61. physical_zone->deleteObject();
  62. if (zone_data && zone_data->isTempClone())
  63. delete zone_data;
  64. zone_data = 0;
  65. }
  66. void afxEA_PhysicalZone::ea_set_datablock(SimDataBlock* db)
  67. {
  68. zone_data = dynamic_cast<afxPhysicalZoneData*>(db);
  69. }
  70. bool afxEA_PhysicalZone::ea_start()
  71. {
  72. if (!zone_data)
  73. {
  74. Con::errorf("afxEA_PhysicalZone::ea_start() -- missing or incompatible datablock.");
  75. return false;
  76. }
  77. do_runtime_substitutions();
  78. return true;
  79. }
  80. bool afxEA_PhysicalZone::ea_update(F32 dt)
  81. {
  82. if (!physical_zone)
  83. {
  84. // create and register effect
  85. physical_zone = new PhysicalZone();
  86. physical_zone->mVelocityMod = zone_data->mVelocityMod;
  87. physical_zone->mGravityMod = zone_data->mGravityMod;
  88. physical_zone->mAppliedForce = zone_data->mAppliedForce;
  89. physical_zone->force_type = zone_data->force_type;
  90. physical_zone->orient_force = zone_data->orient_force;
  91. physical_zone->setField("polyhedron", zone_data->mPolyhedron);
  92. if (!physical_zone->registerObject())
  93. {
  94. delete physical_zone;
  95. physical_zone = 0;
  96. Con::errorf("afxEA_PhysicalZone::ea_update() -- effect failed to register.");
  97. return false;
  98. }
  99. deleteNotify(physical_zone);
  100. physical_zone->activate();
  101. }
  102. if (physical_zone)
  103. {
  104. if (zone_data->exclude_cons_obj)
  105. {
  106. afxConstraint* pos_constraint = getPosConstraint();
  107. set_cons_object((pos_constraint) ? pos_constraint->getSceneObject() : 0);
  108. }
  109. if (mDo_fades)
  110. physical_zone->setFadeAmount(mFade_value);
  111. physical_zone->setTransform(mUpdated_xfm);
  112. }
  113. return true;
  114. }
  115. void afxEA_PhysicalZone::ea_finish(bool was_stopped)
  116. {
  117. if (physical_zone)
  118. {
  119. set_cons_object(0);
  120. physical_zone->deleteObject();
  121. physical_zone = 0;
  122. }
  123. }
  124. void afxEA_PhysicalZone::ea_set_scope_status(bool in_scope)
  125. {
  126. if (physical_zone)
  127. {
  128. if (in_scope && !physical_zone->isActive())
  129. physical_zone->activate();
  130. else if (!in_scope && physical_zone->isActive())
  131. physical_zone->deactivate();
  132. }
  133. }
  134. void afxEA_PhysicalZone::onDeleteNotify(SimObject* obj)
  135. {
  136. if (physical_zone == dynamic_cast<PhysicalZone*>(obj))
  137. physical_zone = 0;
  138. Parent::onDeleteNotify(obj);
  139. }
  140. void afxEA_PhysicalZone::do_runtime_substitutions()
  141. {
  142. // only clone the datablock if there are substitutions
  143. if (zone_data->getSubstitutionCount() > 0)
  144. {
  145. // clone the datablock and perform substitutions
  146. afxPhysicalZoneData* orig_db = zone_data;
  147. zone_data = new afxPhysicalZoneData(*orig_db, true);
  148. orig_db->performSubstitutions(zone_data, mChoreographer, mGroup_index);
  149. }
  150. }
  151. void afxEA_PhysicalZone::set_cons_object(SceneObject* new_obj)
  152. {
  153. if (cons_obj == new_obj)
  154. return;
  155. if (cons_obj)
  156. {
  157. physical_zone->unregisterExcludedObject(cons_obj);
  158. //clearNotify(shape);
  159. }
  160. cons_obj = new_obj;
  161. if (cons_obj)
  162. {
  163. //deleteNotify(shape);
  164. physical_zone->registerExcludedObject(cons_obj);
  165. }
  166. }
  167. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  168. class afxEA_PhysicalZoneDesc : public afxEffectAdapterDesc, public afxEffectDefs
  169. {
  170. static afxEA_PhysicalZoneDesc desc;
  171. public:
  172. virtual bool testEffectType(const SimDataBlock*) const;
  173. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  174. virtual bool runsOnServer(const afxEffectWrapperData*) const { return true; }
  175. virtual bool runsOnClient(const afxEffectWrapperData*) const { return false; }
  176. virtual afxEffectWrapper* create() const { return new afxEA_PhysicalZone; }
  177. };
  178. afxEA_PhysicalZoneDesc afxEA_PhysicalZoneDesc::desc;
  179. bool afxEA_PhysicalZoneDesc::testEffectType(const SimDataBlock* db) const
  180. {
  181. return (typeid(afxPhysicalZoneData) == typeid(*db));
  182. }
  183. bool afxEA_PhysicalZoneDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  184. {
  185. return (timing.lifetime < 0);
  186. }
  187. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//