afxEA_AnimClip.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/afxAnimClip.h"
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. // afxEA_AnimClip
  32. class afxEA_AnimClip : public afxEffectWrapper
  33. {
  34. typedef afxEffectWrapper Parent;
  35. afxAnimClipData* clip_data;
  36. bool started;
  37. F32 anim_lifetime;
  38. U32 anim_tag;
  39. U32 lock_tag;
  40. void do_runtime_substitutions();
  41. public:
  42. /*C*/ afxEA_AnimClip();
  43. /*C*/ ~afxEA_AnimClip();
  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. };
  49. //~~~~~~~~~~~~~~~~~~~~//
  50. afxEA_AnimClip::afxEA_AnimClip()
  51. {
  52. clip_data = 0;
  53. started = false;
  54. anim_lifetime = 0;
  55. anim_tag = 0;
  56. lock_tag = 0;
  57. }
  58. afxEA_AnimClip::~afxEA_AnimClip()
  59. {
  60. if (clip_data && clip_data->isTempClone())
  61. delete clip_data;
  62. clip_data = 0;
  63. }
  64. void afxEA_AnimClip::ea_set_datablock(SimDataBlock* db)
  65. {
  66. clip_data = dynamic_cast<afxAnimClipData*>(db);
  67. }
  68. bool afxEA_AnimClip::ea_start()
  69. {
  70. if (!clip_data)
  71. {
  72. Con::errorf("afxEA_AnimClip::ea_start() -- missing or incompatible datablock.");
  73. return false;
  74. }
  75. do_runtime_substitutions();
  76. afxConstraint* pos_constraint = getPosConstraint();
  77. if (mFull_lifetime == INFINITE_LIFETIME && pos_constraint != 0)
  78. anim_lifetime = pos_constraint->getAnimClipDuration(clip_data->clip_name);
  79. else
  80. anim_lifetime = mFull_lifetime;
  81. anim_tag = 0;
  82. lock_tag = 0;
  83. return true;
  84. }
  85. bool afxEA_AnimClip::ea_update(F32 dt)
  86. {
  87. afxConstraint* pos_constraint = getPosConstraint();
  88. if (!started && pos_constraint != 0)
  89. {
  90. bool go_for_it = true;
  91. if (pos_constraint->getDamageState() == ShapeBase::Enabled)
  92. go_for_it = !clip_data->ignore_enabled;
  93. else if (pos_constraint->getDamageState() == ShapeBase::Disabled)
  94. go_for_it = !clip_data->ignore_disabled;
  95. if (go_for_it && (clip_data->ignore_first_person || clip_data->ignore_third_person))
  96. {
  97. ShapeBase* shape = dynamic_cast<ShapeBase*>(pos_constraint->getSceneObject());
  98. if (shape)
  99. {
  100. bool is_first_person = shape->isFirstPerson();
  101. if (clip_data->ignore_first_person && is_first_person)
  102. go_for_it = false;
  103. if (clip_data->ignore_third_person && !is_first_person)
  104. go_for_it = false;
  105. }
  106. }
  107. if (go_for_it)
  108. {
  109. F32 rate = clip_data->rate/mProp_time_factor;
  110. F32 pos = mFmod(mLife_elapsed, anim_lifetime)/anim_lifetime;
  111. pos = mFmod(pos + clip_data->pos_offset, 1.0);
  112. if (clip_data->rate < 0)
  113. pos = 1.0f - pos;
  114. anim_tag = pos_constraint->setAnimClip(clip_data->clip_name, pos, rate, clip_data->trans,
  115. clip_data->is_death_anim);
  116. if (clip_data->lock_anim)
  117. lock_tag = pos_constraint->lockAnimation();
  118. }
  119. started = true;
  120. }
  121. return true;
  122. }
  123. void afxEA_AnimClip::ea_finish(bool was_stopped)
  124. {
  125. afxConstraint* pos_constraint = getPosConstraint();
  126. if (pos_constraint && anim_tag != 0)
  127. {
  128. pos_constraint->resetAnimation(anim_tag);
  129. }
  130. if (pos_constraint && lock_tag != 0)
  131. pos_constraint->unlockAnimation(lock_tag);
  132. started = false;
  133. }
  134. void afxEA_AnimClip::do_runtime_substitutions()
  135. {
  136. // only clone the datablock if there are substitutions
  137. if (clip_data->getSubstitutionCount() > 0)
  138. {
  139. // clone the datablock and perform substitutions
  140. afxAnimClipData* orig_db = clip_data;
  141. clip_data = new afxAnimClipData(*orig_db, true);
  142. orig_db->performSubstitutions(clip_data, mChoreographer, mGroup_index);
  143. }
  144. }
  145. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  146. // afxEA_AnimClipDesc
  147. class afxEA_AnimClipDesc : public afxEffectAdapterDesc, public afxEffectDefs
  148. {
  149. static afxEA_AnimClipDesc desc;
  150. public:
  151. virtual bool testEffectType(const SimDataBlock*) const;
  152. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  153. virtual bool runsOnServer(const afxEffectWrapperData*) const { return true; }
  154. virtual bool runsOnClient(const afxEffectWrapperData*) const { return true; }
  155. virtual bool isPositional(const afxEffectWrapperData*) const { return false; }
  156. virtual afxEffectWrapper* create() const { return new afxEA_AnimClip; }
  157. };
  158. afxEA_AnimClipDesc afxEA_AnimClipDesc::desc;
  159. bool afxEA_AnimClipDesc::testEffectType(const SimDataBlock* db) const
  160. {
  161. return (typeid(afxAnimClipData) == typeid(*db));
  162. }
  163. bool afxEA_AnimClipDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  164. {
  165. return (timing.lifetime < 0);
  166. }
  167. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//