afxEA_Force.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "afx/arcaneFX.h"
  25. #include "afx/afxChoreographer.h"
  26. #include "afx/afxEffectDefs.h"
  27. #include "afx/afxEffectWrapper.h"
  28. #include "afx/forces/afxForce.h"
  29. #include "afx/forces/afxForceSet.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. // afxEA_Force
  32. class afxEA_Force : public afxEffectWrapper
  33. {
  34. typedef afxEffectWrapper Parent;
  35. afxForceData* force_data;
  36. afxForce* force;
  37. afxForceSetMgr* force_set_mgr;
  38. void do_runtime_substitutions();
  39. public:
  40. /*C*/ afxEA_Force();
  41. /*D*/ ~afxEA_Force();
  42. virtual void ea_set_datablock(SimDataBlock*);
  43. virtual bool ea_start();
  44. virtual bool ea_update(F32 dt);
  45. virtual void ea_finish(bool was_stopped);
  46. };
  47. //~~~~~~~~~~~~~~~~~~~~//
  48. afxEA_Force::afxEA_Force()
  49. {
  50. force_data = 0;
  51. force = 0;
  52. force_set_mgr = 0;
  53. }
  54. afxEA_Force::~afxEA_Force()
  55. {
  56. if (force)
  57. {
  58. if (force_set_mgr)
  59. force_set_mgr->unregisterForce(force_data->force_set_name, force);
  60. delete force;
  61. }
  62. if (force_data && force_data->isTempClone())
  63. {
  64. delete force_data;
  65. force_data = 0;
  66. }
  67. force_set_mgr = 0;
  68. }
  69. void afxEA_Force::ea_set_datablock(SimDataBlock* db)
  70. {
  71. force_data = dynamic_cast<afxForceData*>(db);
  72. }
  73. bool afxEA_Force::ea_start()
  74. {
  75. if (!force_data)
  76. {
  77. Con::errorf("afxEA_Force::ea_start() -- missing or incompatible datablock.");
  78. return false;
  79. }
  80. do_runtime_substitutions();
  81. force_set_mgr = mChoreographer->getForceSetMgr();
  82. return true;
  83. }
  84. bool afxEA_Force::ea_update(F32 dt)
  85. {
  86. if (!force)
  87. {
  88. force = (force_data->force_desc) ? force_data->force_desc->create() : 0;
  89. if (!force)
  90. {
  91. delete force;
  92. force = 0;
  93. Con::errorf(ConsoleLogEntry::General, "Force effect failed to instantiate. (%s)", mDatablock->getName());
  94. return false;
  95. }
  96. force->onNewDataBlock(force_data, false);
  97. if (force)
  98. {
  99. force_set_mgr->registerForce(force_data->force_set_name, force);
  100. force->start();
  101. }
  102. }
  103. if (force) // && in_scope)
  104. {
  105. if (mDo_fades)
  106. force->setFadeAmount(mFade_value);
  107. force->update(dt);
  108. }
  109. return true;
  110. }
  111. void afxEA_Force::ea_finish(bool was_stopped)
  112. {
  113. if (!force)
  114. return;
  115. if (force_set_mgr)
  116. force_set_mgr->unregisterForce(force_data->force_set_name, force);
  117. delete force;
  118. force = 0;
  119. }
  120. void afxEA_Force::do_runtime_substitutions()
  121. {
  122. force_data = force_data->cloneAndPerformSubstitutions(mChoreographer, mGroup_index);
  123. }
  124. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  125. class afxEA_ForceDesc : public afxEffectAdapterDesc, public afxEffectDefs
  126. {
  127. static afxEA_ForceDesc desc;
  128. public:
  129. virtual bool testEffectType(const SimDataBlock*) const;
  130. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  131. virtual bool runsOnServer(const afxEffectWrapperData*) const { return false; }
  132. virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; }
  133. virtual afxEffectWrapper* create() const { return new afxEA_Force; }
  134. };
  135. afxEA_ForceDesc afxEA_ForceDesc::desc;
  136. bool afxEA_ForceDesc::testEffectType(const SimDataBlock* db) const
  137. {
  138. if (dynamic_cast<const afxForceData*>(db) != 0)
  139. return afxForceDesc::identifyForce((afxForceData*) db);
  140. return false;
  141. }
  142. bool afxEA_ForceDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  143. {
  144. return (timing.lifetime < 0);
  145. }
  146. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//