afxEA_FootSwitch.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/player.h"
  27. #include "afx/afxChoreographer.h"
  28. #include "afx/afxEffectDefs.h"
  29. #include "afx/afxEffectWrapper.h"
  30. #include "afx/ce/afxFootSwitch.h"
  31. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  32. // afxEA_FootSwitch
  33. class afxEA_FootSwitch : public afxEffectWrapper
  34. {
  35. typedef afxEffectWrapper Parent;
  36. afxFootSwitchData* mFootfall_data;
  37. Player* mPlayer;
  38. void do_runtime_substitutions();
  39. public:
  40. /*C*/ afxEA_FootSwitch();
  41. void set_overrides(Player*);
  42. void clear_overrides(Player*);
  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. };
  48. //~~~~~~~~~~~~~~~~~~~~//
  49. afxEA_FootSwitch::afxEA_FootSwitch()
  50. {
  51. mFootfall_data = 0;
  52. mPlayer = 0;
  53. }
  54. inline void afxEA_FootSwitch::set_overrides(Player* player)
  55. {
  56. if (mFootfall_data->override_all)
  57. player->overrideFootfallFX();
  58. else
  59. player->overrideFootfallFX(mFootfall_data->override_decals,
  60. mFootfall_data->override_sounds,
  61. mFootfall_data->override_dust);
  62. }
  63. inline void afxEA_FootSwitch::clear_overrides(Player* player)
  64. {
  65. if (mFootfall_data->override_all)
  66. player->restoreFootfallFX();
  67. else
  68. player->restoreFootfallFX(mFootfall_data->override_decals,
  69. mFootfall_data->override_sounds,
  70. mFootfall_data->override_dust);
  71. }
  72. void afxEA_FootSwitch::ea_set_datablock(SimDataBlock* db)
  73. {
  74. mFootfall_data = dynamic_cast<afxFootSwitchData*>(db);
  75. }
  76. bool afxEA_FootSwitch::ea_start()
  77. {
  78. if (!mFootfall_data)
  79. {
  80. Con::errorf("afxEA_FootSwitch::ea_start() -- missing or incompatible datablock.");
  81. return false;
  82. }
  83. do_runtime_substitutions();
  84. afxConstraint* pos_cons = getPosConstraint();
  85. mPlayer = (pos_cons) ? dynamic_cast<Player*>(pos_cons->getSceneObject()) : 0;
  86. if (mPlayer)
  87. set_overrides(mPlayer);
  88. return true;
  89. }
  90. bool afxEA_FootSwitch::ea_update(F32 dt)
  91. {
  92. if (!mPlayer)
  93. return true;
  94. afxConstraint* pos_cons = getPosConstraint();
  95. Player* temp_player = (pos_cons) ? dynamic_cast<Player*>(pos_cons->getSceneObject()) : 0;
  96. if (temp_player && temp_player != mPlayer)
  97. {
  98. mPlayer = temp_player;
  99. if (mPlayer)
  100. set_overrides(mPlayer);
  101. }
  102. return true;
  103. }
  104. void afxEA_FootSwitch::ea_finish(bool was_stopped)
  105. {
  106. if (!mPlayer)
  107. return;
  108. afxConstraint* pos_cons = getPosConstraint();
  109. Player* temp_player = (pos_cons) ? dynamic_cast<Player*>(pos_cons->getSceneObject()) : 0;
  110. if (temp_player == mPlayer)
  111. clear_overrides(mPlayer);
  112. }
  113. void afxEA_FootSwitch::do_runtime_substitutions()
  114. {
  115. // only clone the datablock if there are substitutions
  116. if (mFootfall_data->getSubstitutionCount() > 0)
  117. {
  118. // clone the datablock and perform substitutions
  119. afxFootSwitchData* orig_db = mFootfall_data;
  120. mFootfall_data = new afxFootSwitchData(*orig_db, true);
  121. orig_db->performSubstitutions(mFootfall_data, mChoreographer, mGroup_index);
  122. }
  123. }
  124. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  125. class afxEA_FootfallSwitchDesc : public afxEffectAdapterDesc, public afxEffectDefs
  126. {
  127. static afxEA_FootfallSwitchDesc 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 bool isPositional(const afxEffectWrapperData*) const { return false; }
  134. virtual afxEffectWrapper* create() const { return new afxEA_FootSwitch; }
  135. };
  136. afxEA_FootfallSwitchDesc afxEA_FootfallSwitchDesc::desc;
  137. bool afxEA_FootfallSwitchDesc::testEffectType(const SimDataBlock* db) const
  138. {
  139. return (typeid(afxFootSwitchData) == typeid(*db));
  140. }
  141. bool afxEA_FootfallSwitchDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  142. {
  143. return (timing.lifetime < 0);
  144. }
  145. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//