afxEA_StaticShape.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/afxStaticShape.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. // afxEA_StaticShape
  32. class afxEA_StaticShape : public afxEffectWrapper
  33. {
  34. typedef afxEffectWrapper Parent;
  35. StaticShapeData* shape_data;
  36. afxStaticShape* static_shape;
  37. bool fade_out_started;
  38. bool do_spawn;
  39. void do_runtime_substitutions();
  40. public:
  41. /*C*/ afxEA_StaticShape();
  42. /*D*/ ~afxEA_StaticShape();
  43. virtual void ea_set_datablock(SimDataBlock*);
  44. virtual bool ea_start();
  45. virtual bool ea_update(F32 dt);
  46. virtual void ea_finish(bool was_stopped);
  47. virtual void ea_set_scope_status(bool flag);
  48. virtual void onDeleteNotify(SimObject*);
  49. virtual void getUpdatedBoxCenter(Point3F& pos);
  50. virtual TSShape* getTSShape();
  51. virtual TSShapeInstance* getTSShapeInstance();
  52. };
  53. //~~~~~~~~~~~~~~~~~~~~//
  54. afxEA_StaticShape::afxEA_StaticShape()
  55. {
  56. shape_data = 0;
  57. static_shape = 0;
  58. fade_out_started = false;
  59. do_spawn = true;
  60. }
  61. afxEA_StaticShape::~afxEA_StaticShape()
  62. {
  63. if (static_shape)
  64. static_shape->deleteObject();
  65. if (shape_data && shape_data->isTempClone())
  66. delete shape_data;
  67. shape_data = 0;
  68. }
  69. void afxEA_StaticShape::ea_set_datablock(SimDataBlock* db)
  70. {
  71. shape_data = dynamic_cast<StaticShapeData*>(db);
  72. afxStaticShapeData* afx_shape_data = dynamic_cast<afxStaticShapeData*>(shape_data);
  73. do_spawn = (afx_shape_data) ? afx_shape_data->do_spawn : false;
  74. }
  75. bool afxEA_StaticShape::ea_start()
  76. {
  77. if (!shape_data)
  78. {
  79. Con::errorf("afxEA_StaticShape::ea_start() -- missing or incompatible datablock.");
  80. return false;
  81. }
  82. do_runtime_substitutions();
  83. // fades are handled using startFade() calls.
  84. mDo_fades = false;
  85. return true;
  86. }
  87. bool afxEA_StaticShape::ea_update(F32 dt)
  88. {
  89. if (!static_shape)
  90. {
  91. // create and register effect
  92. static_shape = new afxStaticShape();
  93. if (mDatablock->use_ghost_as_cons_obj && mDatablock->effect_name != ST_NULLSTRING)
  94. static_shape->init(mChoreographer->getChoreographerId(), mDatablock->effect_name);
  95. static_shape->onNewDataBlock(shape_data, false);
  96. if (!static_shape->registerObject())
  97. {
  98. delete static_shape;
  99. static_shape = 0;
  100. Con::errorf("afxEA_StaticShape::ea_update() -- effect failed to register.");
  101. return false;
  102. }
  103. deleteNotify(static_shape);
  104. registerForCleanup(static_shape);
  105. if (mEW_timing.fade_in_time > 0.0f)
  106. static_shape->startFade(mEW_timing.fade_in_time, 0, false);
  107. }
  108. if (static_shape)
  109. {
  110. if (!fade_out_started && mElapsed > mFade_out_start)
  111. {
  112. if (!do_spawn)
  113. {
  114. if (mEW_timing.fade_out_time > 0.0f)
  115. static_shape->startFade(mEW_timing.fade_out_time, 0, true);
  116. }
  117. fade_out_started = true;
  118. }
  119. if (mIn_scope)
  120. {
  121. static_shape->setTransform(mUpdated_xfm);
  122. static_shape->setScale(mUpdated_scale);
  123. }
  124. }
  125. return true;
  126. }
  127. void afxEA_StaticShape::ea_finish(bool was_stopped)
  128. {
  129. if (!static_shape)
  130. return;
  131. if (do_spawn)
  132. {
  133. Con::executef(shape_data, "onSpawn", static_shape->getIdString(), mDatablock->effect_name);
  134. clearNotify(static_shape);
  135. }
  136. else
  137. static_shape->deleteObject();
  138. static_shape = 0;
  139. }
  140. void afxEA_StaticShape::ea_set_scope_status(bool in_scope)
  141. {
  142. if (static_shape)
  143. static_shape->setVisibility(do_spawn || in_scope);
  144. }
  145. void afxEA_StaticShape::onDeleteNotify(SimObject* obj)
  146. {
  147. if (static_shape == dynamic_cast<afxStaticShape*>(obj))
  148. static_shape = 0;
  149. Parent::onDeleteNotify(obj);
  150. }
  151. void afxEA_StaticShape::getUpdatedBoxCenter(Point3F& pos)
  152. {
  153. if (static_shape)
  154. pos = static_shape->getBoxCenter();
  155. }
  156. TSShape* afxEA_StaticShape::getTSShape()
  157. {
  158. return (static_shape) ? ((TSShape*)static_shape->getShape()) : 0;
  159. }
  160. TSShapeInstance* afxEA_StaticShape::getTSShapeInstance()
  161. {
  162. return (static_shape) ? static_shape->getShapeInstance() : 0;
  163. }
  164. void afxEA_StaticShape::do_runtime_substitutions()
  165. {
  166. // only clone the datablock if there are substitutions
  167. if (shape_data->getSubstitutionCount() > 0)
  168. {
  169. if (typeid(afxStaticShapeData) == typeid(*shape_data))
  170. {
  171. afxStaticShapeData* orig_db = (afxStaticShapeData*)shape_data;
  172. shape_data = new afxStaticShapeData(*orig_db, true);
  173. orig_db->performSubstitutions(shape_data, mChoreographer, mGroup_index);
  174. }
  175. else
  176. {
  177. StaticShapeData* orig_db = shape_data;
  178. shape_data = new StaticShapeData(*orig_db, true);
  179. orig_db->performSubstitutions(shape_data, mChoreographer, mGroup_index);
  180. }
  181. }
  182. }
  183. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  184. class afxEA_StaticShapeDesc : public afxEffectAdapterDesc, public afxEffectDefs
  185. {
  186. static afxEA_StaticShapeDesc desc;
  187. public:
  188. virtual bool testEffectType(const SimDataBlock*) const;
  189. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  190. virtual bool runsOnServer(const afxEffectWrapperData*) const { return true; }
  191. virtual bool runsOnClient(const afxEffectWrapperData*) const { return false; }
  192. virtual afxEffectWrapper* create() const { return new afxEA_StaticShape; }
  193. };
  194. afxEA_StaticShapeDesc afxEA_StaticShapeDesc::desc;
  195. bool afxEA_StaticShapeDesc::testEffectType(const SimDataBlock* db) const
  196. {
  197. if (typeid(StaticShapeData) == typeid(*db))
  198. return true;
  199. if (typeid(afxStaticShapeData) == typeid(*db))
  200. return true;
  201. return false;
  202. }
  203. bool afxEA_StaticShapeDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  204. {
  205. return (timing.lifetime < 0);
  206. }
  207. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//