afxEA_Debris.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/debris.h"
  27. #include "afx/afxEffectDefs.h"
  28. #include "afx/afxEffectWrapper.h"
  29. #include "afx/afxChoreographer.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. // afxEA_Debris
  32. class afxEA_Debris : public afxEffectWrapper
  33. {
  34. typedef afxEffectWrapper Parent;
  35. DebrisData* debris_data;
  36. Debris* debris;
  37. bool exploded;
  38. bool debris_done;
  39. void do_runtime_substitutions();
  40. public:
  41. /*C*/ afxEA_Debris();
  42. /*D*/ ~afxEA_Debris();
  43. virtual bool isDone();
  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 onDeleteNotify(SimObject*);
  49. };
  50. //~~~~~~~~~~~~~~~~~~~~//
  51. afxEA_Debris::afxEA_Debris()
  52. {
  53. debris_data = 0;
  54. debris = 0;
  55. exploded = false;
  56. debris_done = false;
  57. }
  58. afxEA_Debris::~afxEA_Debris()
  59. {
  60. if (debris)
  61. clearNotify(debris);
  62. }
  63. bool afxEA_Debris::isDone()
  64. {
  65. return (mDatablock->use_as_cons_obj) ? debris_done : exploded;
  66. }
  67. void afxEA_Debris::ea_set_datablock(SimDataBlock* db)
  68. {
  69. debris_data = dynamic_cast<DebrisData*>(db);
  70. }
  71. bool afxEA_Debris::ea_start()
  72. {
  73. if (!debris_data)
  74. {
  75. Con::errorf("afxEA_Debris::ea_start() -- missing or incompatible datablock.");
  76. return false;
  77. }
  78. do_runtime_substitutions();
  79. debris = new Debris();
  80. debris->onNewDataBlock(debris_data, false);
  81. return true;
  82. }
  83. bool afxEA_Debris::ea_update(F32 dt)
  84. {
  85. if (exploded && debris)
  86. {
  87. if (mIn_scope)
  88. {
  89. mUpdated_xfm = debris->getRenderTransform();
  90. mUpdated_xfm.getColumn(3, &mUpdated_pos);
  91. }
  92. }
  93. if (!exploded && debris)
  94. {
  95. if (mIn_scope)
  96. {
  97. Point3F dir_vec(0,1,0);
  98. mUpdated_xfm.mulV(dir_vec);
  99. debris->init(mUpdated_pos, dir_vec);
  100. if (!debris->registerObject())
  101. {
  102. delete debris;
  103. debris = 0;
  104. Con::errorf("afxEA_Debris::ea_update() -- effect failed to register.");
  105. return false;
  106. }
  107. deleteNotify(debris);
  108. }
  109. exploded = true;
  110. }
  111. return true;
  112. }
  113. void afxEA_Debris::ea_finish(bool was_stopped)
  114. {
  115. if (debris)
  116. {
  117. clearNotify(debris);
  118. debris = 0;
  119. }
  120. exploded = false;
  121. }
  122. void afxEA_Debris::onDeleteNotify(SimObject* obj)
  123. {
  124. // debris deleted?
  125. Debris* del_debris = dynamic_cast<Debris*>(obj);
  126. if (del_debris == debris)
  127. {
  128. debris = NULL;
  129. debris_done = true;
  130. }
  131. }
  132. void afxEA_Debris::do_runtime_substitutions()
  133. {
  134. // only clone the datablock if there are substitutions
  135. if (debris_data->getSubstitutionCount() > 0)
  136. {
  137. // clone the datablock and perform substitutions
  138. DebrisData* orig_db = debris_data;
  139. debris_data = new DebrisData(*orig_db, true);
  140. orig_db->performSubstitutions(debris_data, mChoreographer, mGroup_index);
  141. }
  142. }
  143. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  144. class afxEA_DebrisDesc : public afxEffectAdapterDesc, public afxEffectDefs
  145. {
  146. static afxEA_DebrisDesc desc;
  147. public:
  148. virtual bool testEffectType(const SimDataBlock*) const;
  149. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  150. virtual bool runsOnServer(const afxEffectWrapperData*) const { return false; }
  151. virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; }
  152. virtual afxEffectWrapper* create() const { return new afxEA_Debris; }
  153. };
  154. afxEA_DebrisDesc afxEA_DebrisDesc::desc;
  155. bool afxEA_DebrisDesc::testEffectType(const SimDataBlock* db) const
  156. {
  157. return (typeid(DebrisData) == typeid(*db));
  158. }
  159. bool afxEA_DebrisDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  160. {
  161. return (ew->use_as_cons_obj && timing.lifetime < 0);
  162. }
  163. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//