afxEA_PlayerMovement.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/afxPlayerMovement.h"
  31. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  32. // afxEA_PlayerMovement
  33. class afxEA_PlayerMovement : public afxEffectWrapper
  34. {
  35. typedef afxEffectWrapper Parent;
  36. afxPlayerMovementData* movement_data;
  37. U32 tag;
  38. void do_runtime_substitutions();
  39. public:
  40. /*C*/ afxEA_PlayerMovement();
  41. /*C*/ ~afxEA_PlayerMovement();
  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_PlayerMovement::afxEA_PlayerMovement()
  49. {
  50. movement_data = 0;
  51. tag = 0;
  52. }
  53. afxEA_PlayerMovement::~afxEA_PlayerMovement()
  54. {
  55. if (movement_data && movement_data->isTempClone())
  56. delete movement_data;
  57. movement_data = 0;
  58. }
  59. void afxEA_PlayerMovement::ea_set_datablock(SimDataBlock* db)
  60. {
  61. movement_data = dynamic_cast<afxPlayerMovementData*>(db);
  62. }
  63. bool afxEA_PlayerMovement::ea_start()
  64. {
  65. if (!movement_data)
  66. {
  67. Con::errorf("afxEA_PlayerMovement::ea_start() -- missing or incompatible datablock.");
  68. return false;
  69. }
  70. do_runtime_substitutions();
  71. tag = 0;
  72. afxConstraint* pos_cons = getPosConstraint();
  73. if (!pos_cons)
  74. {
  75. Con::warnf("afxEA_PlayerMovement::ea_start() -- missing position constraint.");
  76. return false;
  77. }
  78. Player* player = dynamic_cast<Player*>(pos_cons->getSceneObject());
  79. if (!player)
  80. {
  81. Con::warnf("afxEA_PlayerMovement::ea_start() -- position constraint is not a Player.");
  82. return false;
  83. }
  84. // setup player overrides
  85. if (movement_data->hasMovementOverride())
  86. tag = player->setMovementOverride(movement_data->speed_bias, &movement_data->movement, movement_data->movement_op);
  87. else
  88. tag = player->setMovementOverride(movement_data->speed_bias);
  89. return true;
  90. }
  91. bool afxEA_PlayerMovement::ea_update(F32 dt)
  92. {
  93. return true;
  94. }
  95. void afxEA_PlayerMovement::ea_finish(bool was_stopped)
  96. {
  97. afxConstraint* pos_cons = getPosConstraint();
  98. if (!pos_cons)
  99. return;
  100. Player* player = dynamic_cast<Player*>(pos_cons->getSceneObject());
  101. if (!player)
  102. return;
  103. // restore player overrides
  104. player->restoreMovement(tag);
  105. }
  106. void afxEA_PlayerMovement::do_runtime_substitutions()
  107. {
  108. // only clone the datablock if there are substitutions
  109. if (movement_data->getSubstitutionCount() > 0)
  110. {
  111. // clone the datablock and perform substitutions
  112. afxPlayerMovementData* orig_db = movement_data;
  113. movement_data = new afxPlayerMovementData(*orig_db, true);
  114. orig_db->performSubstitutions(movement_data, mChoreographer, mGroup_index);
  115. }
  116. }
  117. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  118. class afxEA_PlayerMovementDesc : public afxEffectAdapterDesc, public afxEffectDefs
  119. {
  120. static afxEA_PlayerMovementDesc desc;
  121. public:
  122. virtual bool testEffectType(const SimDataBlock*) const;
  123. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  124. virtual bool runsOnServer(const afxEffectWrapperData*) const { return true; }
  125. virtual bool runsOnClient(const afxEffectWrapperData*) const { return false; }
  126. virtual afxEffectWrapper* create() const { return new afxEA_PlayerMovement; }
  127. };
  128. afxEA_PlayerMovementDesc afxEA_PlayerMovementDesc::desc;
  129. bool afxEA_PlayerMovementDesc::testEffectType(const SimDataBlock* db) const
  130. {
  131. return (typeid(afxPlayerMovementData) == typeid(*db));
  132. }
  133. bool afxEA_PlayerMovementDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  134. {
  135. return (timing.lifetime < 0);
  136. }
  137. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//