afxEA_CameraPuppet.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/gameBase/gameConnection.h"
  27. #include "afx/afxChoreographer.h"
  28. #include "afx/afxEffectDefs.h"
  29. #include "afx/afxEffectWrapper.h"
  30. #include "afx/afxCamera.h"
  31. #include "afx/ce/afxCameraPuppet.h"
  32. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  33. // afxEA_CameraPuppet
  34. class afxEA_CameraPuppet : public afxEffectWrapper
  35. {
  36. typedef afxEffectWrapper Parent;
  37. afxCameraPuppetData* puppet_data;
  38. afxConstraint* cam_cons;
  39. bool was_1st_person;
  40. void do_runtime_substitutions();
  41. public:
  42. /*C*/ afxEA_CameraPuppet();
  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. virtual void getUnconstrainedPosition(Point3F& pos);
  48. virtual void getUnconstrainedTransform(MatrixF& xfm);
  49. };
  50. //~~~~~~~~~~~~~~~~~~~~//
  51. afxEA_CameraPuppet::afxEA_CameraPuppet()
  52. {
  53. puppet_data = 0;
  54. cam_cons = 0;
  55. was_1st_person = false;
  56. }
  57. void afxEA_CameraPuppet::ea_set_datablock(SimDataBlock* db)
  58. {
  59. puppet_data = dynamic_cast<afxCameraPuppetData*>(db);
  60. }
  61. bool afxEA_CameraPuppet::ea_start()
  62. {
  63. if (!puppet_data)
  64. {
  65. Con::errorf("afxEA_CameraPuppet::ea_start() -- missing or incompatible datablock.");
  66. return false;
  67. }
  68. do_runtime_substitutions();
  69. afxConstraintID obj_id = mCons_mgr->getConstraintId(puppet_data->cam_def);
  70. cam_cons = mCons_mgr->getConstraint(obj_id);
  71. SceneObject* obj = (cam_cons) ? cam_cons->getSceneObject() : 0;
  72. if (obj && obj->isClientObject())
  73. {
  74. GameConnection* conn = GameConnection::getConnectionToServer();
  75. if (conn)
  76. {
  77. was_1st_person = conn->isFirstPerson();
  78. if (was_1st_person)
  79. conn->setFirstPerson(false);
  80. }
  81. }
  82. return true;
  83. }
  84. bool afxEA_CameraPuppet::ea_update(F32 dt)
  85. {
  86. SceneObject* obj = (cam_cons) ? cam_cons->getSceneObject() : 0;
  87. if (obj && mIn_scope)
  88. {
  89. obj->setTransform(mUpdated_xfm);
  90. }
  91. return true;
  92. }
  93. void afxEA_CameraPuppet::ea_finish(bool was_stopped)
  94. {
  95. afxCamera* afx_cam = dynamic_cast<afxCamera*>((cam_cons) ? cam_cons->getSceneObject() : 0);
  96. if (afx_cam && afx_cam->isClientObject())
  97. afx_cam->setThirdPersonSnapClient();
  98. if (was_1st_person)
  99. {
  100. GameConnection* conn = GameConnection::getConnectionToServer();
  101. if (conn)
  102. conn->setFirstPerson(true);
  103. }
  104. }
  105. void afxEA_CameraPuppet::getUnconstrainedPosition(Point3F& pos)
  106. {
  107. SceneObject* obj = (cam_cons) ? cam_cons->getSceneObject() : 0;
  108. if (obj)
  109. pos = obj->getRenderPosition();
  110. else
  111. pos.zero();
  112. }
  113. void afxEA_CameraPuppet::getUnconstrainedTransform(MatrixF& xfm)
  114. {
  115. SceneObject* obj = (cam_cons) ? cam_cons->getSceneObject() : 0;
  116. if (obj)
  117. xfm = obj->getRenderTransform();
  118. else
  119. xfm.identity();
  120. }
  121. void afxEA_CameraPuppet::do_runtime_substitutions()
  122. {
  123. // only clone the datablock if there are substitutions
  124. if (puppet_data->getSubstitutionCount() > 0)
  125. {
  126. // clone the datablock and perform substitutions
  127. afxCameraPuppetData* orig_db = puppet_data;
  128. puppet_data = new afxCameraPuppetData(*orig_db, true);
  129. orig_db->performSubstitutions(puppet_data, mChoreographer, mGroup_index);
  130. }
  131. }
  132. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  133. class afxEA_CameraPuppetDesc : public afxEffectAdapterDesc, public afxEffectDefs
  134. {
  135. static afxEA_CameraPuppetDesc desc;
  136. public:
  137. virtual bool testEffectType(const SimDataBlock*) const;
  138. virtual bool requiresStop(const afxEffectWrapperData*, const afxEffectTimingData&) const;
  139. virtual bool runsOnServer(const afxEffectWrapperData*) const;
  140. virtual bool runsOnClient(const afxEffectWrapperData*) const;
  141. virtual afxEffectWrapper* create() const { return new afxEA_CameraPuppet; }
  142. };
  143. afxEA_CameraPuppetDesc afxEA_CameraPuppetDesc::desc;
  144. bool afxEA_CameraPuppetDesc::testEffectType(const SimDataBlock* db) const
  145. {
  146. return (typeid(afxCameraPuppetData) == typeid(*db));
  147. }
  148. bool afxEA_CameraPuppetDesc::requiresStop(const afxEffectWrapperData* ew, const afxEffectTimingData& timing) const
  149. {
  150. return (timing.lifetime < 0);
  151. }
  152. bool afxEA_CameraPuppetDesc::runsOnServer(const afxEffectWrapperData* ew) const
  153. {
  154. U8 networking = ((const afxCameraPuppetData*)ew->effect_data)->networking;
  155. return ((networking & (SERVER_ONLY | SERVER_AND_CLIENT)) != 0);
  156. }
  157. bool afxEA_CameraPuppetDesc::runsOnClient(const afxEffectWrapperData* ew) const
  158. {
  159. U8 networking = ((const afxCameraPuppetData*)ew->effect_data)->networking;
  160. return ((networking & (CLIENT_ONLY | SERVER_AND_CLIENT)) != 0);
  161. }
  162. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//