afxEA_PlayerPuppet.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/afxPlayerPuppet.h"
  31. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  32. // afxEA_PlayerPuppet
  33. class afxEA_PlayerPuppet : public afxEffectWrapper
  34. {
  35. typedef afxEffectWrapper Parent;
  36. afxPlayerPuppetData* mover_data;
  37. afxConstraint* obj_cons;
  38. void do_runtime_substitutions();
  39. public:
  40. /*C*/ afxEA_PlayerPuppet();
  41. virtual void ea_set_datablock(SimDataBlock*);
  42. virtual bool ea_start();
  43. virtual bool ea_update(F32 dt);
  44. virtual void ea_finish(bool was_stopped);
  45. virtual void getUnconstrainedPosition(Point3F& pos);
  46. virtual void getUnconstrainedTransform(MatrixF& xfm);
  47. };
  48. //~~~~~~~~~~~~~~~~~~~~//
  49. afxEA_PlayerPuppet::afxEA_PlayerPuppet()
  50. {
  51. mover_data = 0;
  52. obj_cons = 0;
  53. }
  54. void afxEA_PlayerPuppet::ea_set_datablock(SimDataBlock* db)
  55. {
  56. mover_data = dynamic_cast<afxPlayerPuppetData*>(db);
  57. }
  58. bool afxEA_PlayerPuppet::ea_start()
  59. {
  60. if (!mover_data)
  61. {
  62. Con::errorf("afxEA_PlayerPuppet::ea_start() -- missing or incompatible datablock.");
  63. return false;
  64. }
  65. do_runtime_substitutions();
  66. afxConstraintID obj_id = mCons_mgr->getConstraintId(mover_data->obj_def);
  67. obj_cons = mCons_mgr->getConstraint(obj_id);
  68. Player* player = dynamic_cast<Player*>((obj_cons) ? obj_cons->getSceneObject() : 0);
  69. if (player)
  70. player->ignore_updates = true;
  71. return true;
  72. }
  73. bool afxEA_PlayerPuppet::ea_update(F32 dt)
  74. {
  75. SceneObject* obj = (obj_cons) ? obj_cons->getSceneObject() : 0;
  76. if (obj && mIn_scope)
  77. {
  78. obj->setTransform(mUpdated_xfm);
  79. }
  80. return true;
  81. }
  82. void afxEA_PlayerPuppet::ea_finish(bool was_stopped)
  83. {
  84. Player* player = dynamic_cast<Player*>((obj_cons) ? obj_cons->getSceneObject() : 0);
  85. if (player)
  86. {
  87. player->resetContactTimer();
  88. player->ignore_updates = false;
  89. }
  90. }
  91. void afxEA_PlayerPuppet::getUnconstrainedPosition(Point3F& pos)
  92. {
  93. SceneObject* obj = (obj_cons) ? obj_cons->getSceneObject() : 0;
  94. if (obj)
  95. pos = obj->getRenderPosition();
  96. else
  97. pos.zero();
  98. }
  99. void afxEA_PlayerPuppet::getUnconstrainedTransform(MatrixF& xfm)
  100. {
  101. SceneObject* obj = (obj_cons) ? obj_cons->getSceneObject() : 0;
  102. if (obj)
  103. xfm = obj->getRenderTransform();
  104. else
  105. xfm.identity();
  106. }
  107. void afxEA_PlayerPuppet::do_runtime_substitutions()
  108. {
  109. // only clone the datablock if there are substitutions
  110. if (mover_data->getSubstitutionCount() > 0)
  111. {
  112. // clone the datablock and perform substitutions
  113. afxPlayerPuppetData* orig_db = mover_data;
  114. mover_data = new afxPlayerPuppetData(*orig_db, true);
  115. orig_db->performSubstitutions(mover_data, mChoreographer, mGroup_index);
  116. }
  117. }
  118. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  119. class afxEA_PlayerPuppetDesc : public afxEffectAdapterDesc, public afxEffectDefs
  120. {
  121. static afxEA_PlayerPuppetDesc desc;
  122. public:
  123. virtual bool testEffectType(const SimDataBlock*) const;
  124. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  125. virtual bool runsOnServer(const afxEffectWrapperData*) const;
  126. virtual bool runsOnClient(const afxEffectWrapperData*) const;
  127. virtual afxEffectWrapper* create() const { return new afxEA_PlayerPuppet; }
  128. };
  129. afxEA_PlayerPuppetDesc afxEA_PlayerPuppetDesc::desc;
  130. bool afxEA_PlayerPuppetDesc::testEffectType(const SimDataBlock* db) const
  131. {
  132. return (typeid(afxPlayerPuppetData) == typeid(*db));
  133. }
  134. bool afxEA_PlayerPuppetDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  135. {
  136. return (timing.lifetime < 0);
  137. }
  138. bool afxEA_PlayerPuppetDesc::runsOnServer(const afxEffectWrapperData* ew) const
  139. {
  140. U8 networking = ((const afxPlayerPuppetData*)ew->effect_data)->networking;
  141. return ((networking & (SERVER_ONLY | SERVER_AND_CLIENT)) != 0);
  142. }
  143. bool afxEA_PlayerPuppetDesc::runsOnClient(const afxEffectWrapperData* ew) const
  144. {
  145. U8 networking = ((const afxPlayerPuppetData*)ew->effect_data)->networking;
  146. return ((networking & (CLIENT_ONLY | SERVER_AND_CLIENT)) != 0);
  147. }
  148. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//