afxMooring.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "afx/arcaneFX.h"
  25. #include "math/mathIO.h"
  26. #include "afx/afxChoreographer.h"
  27. #include "afx/ce/afxMooring.h"
  28. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  29. // afxMooringData
  30. IMPLEMENT_CO_DATABLOCK_V1(afxMooringData);
  31. ConsoleDocClass( afxMooringData,
  32. "@brief A datablock that specifies a Mooring effect.\n\n"
  33. "A Mooring is an invisible effect object which can be positioned and oriented within a scene like other objects. Its main "
  34. "purpose is to serve as a common mount point for other effects within the same choreographer. Typically one uses AFX "
  35. "animation features to create movement for a Mooring and then other effects are bound to it using effect-to-effect "
  36. "constraints (#effect)."
  37. "\n\n"
  38. "@ingroup afxEffects\n"
  39. "@ingroup AFX\n"
  40. "@ingroup Datablocks\n"
  41. );
  42. afxMooringData::afxMooringData()
  43. {
  44. track_pos_only = false;
  45. networking = SCOPE_ALWAYS;
  46. display_axis_marker = false;
  47. }
  48. afxMooringData::afxMooringData(const afxMooringData& other, bool temp_clone) : GameBaseData(other, temp_clone)
  49. {
  50. track_pos_only = other.track_pos_only;
  51. networking = other.networking;
  52. display_axis_marker = other.display_axis_marker;
  53. }
  54. #define myOffset(field) Offset(field, afxMooringData)
  55. void afxMooringData::initPersistFields()
  56. {
  57. addField("displayAxisMarker", TypeBool, myOffset(display_axis_marker),
  58. "Specifies whether to display an axis to help visualize the position and orientation "
  59. "of the mooring.");
  60. addField("trackPosOnly", TypeBool, myOffset(track_pos_only),
  61. "This field is only meaningful for networking settings of SCOPE_ALWAYS and GHOSTABLE. "
  62. "In these cases, client moorings are ghosting a mooring on the server, and "
  63. "trackPosOnly determines if the client moorings need to be updated with the server "
  64. "mooring's complete transform or just its position. If only the position needs to be "
  65. "tracked, setting trackPosOnly to true will reduce the network traffic.");
  66. addField("networking", TypeS8, myOffset(networking),
  67. "Specifies the networking model used for the mooring and should be one of: "
  68. "$AFX::SCOPE_ALWAYS, $AFX::GHOSTABLE, $AFX::SERVER_ONLY, or $AFX::CLIENT_ONLY");
  69. Parent::initPersistFields();
  70. }
  71. bool afxMooringData::onAdd()
  72. {
  73. if (Parent::onAdd() == false)
  74. return false;
  75. return true;
  76. }
  77. void afxMooringData::packData(BitStream* stream)
  78. {
  79. Parent::packData(stream);
  80. stream->write(display_axis_marker);
  81. stream->write(track_pos_only);
  82. stream->write(networking);
  83. }
  84. void afxMooringData::unpackData(BitStream* stream)
  85. {
  86. Parent::unpackData(stream);
  87. stream->read(&display_axis_marker);
  88. stream->read(&track_pos_only);
  89. stream->read(&networking);
  90. }
  91. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  92. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  93. // afxMooring
  94. IMPLEMENT_CO_NETOBJECT_V1(afxMooring);
  95. ConsoleDocClass( afxMooring,
  96. "@brief A Mooring effect as defined by an afxMooringData datablock.\n\n"
  97. "A Mooring is an invisible effect object which can be positioned and oriented within "
  98. "a scene like other objects. Its main purpose is to serve as a common mount point for "
  99. "other effects within the same choreographer. Typically one uses AFX animation "
  100. "features to create movement for a Mooring and then other effects are bound to it "
  101. "using effect-to-effect constraints (#effect).\n"
  102. "@ingroup afxEffects\n"
  103. "@ingroup AFX\n"
  104. );
  105. afxMooring::afxMooring()
  106. {
  107. mNetFlags.set(Ghostable | ScopeAlways);
  108. chor_id = 0;
  109. hookup_with_chor = false;
  110. ghost_cons_name = ST_NULLSTRING;
  111. mDataBlock = NULL;
  112. }
  113. afxMooring::afxMooring(U32 networking, U32 chor_id, StringTableEntry cons_name)
  114. {
  115. if (networking & SCOPE_ALWAYS)
  116. {
  117. mNetFlags.clear();
  118. mNetFlags.set(Ghostable | ScopeAlways);
  119. }
  120. else if (networking & GHOSTABLE)
  121. {
  122. mNetFlags.clear();
  123. mNetFlags.set(Ghostable);
  124. }
  125. else if (networking & SERVER_ONLY)
  126. {
  127. mNetFlags.clear();
  128. }
  129. else // if (networking & CLIENT_ONLY)
  130. {
  131. mNetFlags.clear();
  132. mNetFlags.set(IsGhost);
  133. }
  134. this->chor_id = chor_id;
  135. hookup_with_chor = false;
  136. this->ghost_cons_name = cons_name;
  137. mDataBlock = NULL;
  138. }
  139. afxMooring::~afxMooring()
  140. {
  141. }
  142. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  143. bool afxMooring::onNewDataBlock(GameBaseData* dptr, bool reload)
  144. {
  145. mDataBlock = dynamic_cast<afxMooringData*>(dptr);
  146. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  147. return false;
  148. return true;
  149. }
  150. void afxMooring::advanceTime(F32 dt)
  151. {
  152. Parent::advanceTime(dt);
  153. if (hookup_with_chor)
  154. {
  155. afxChoreographer* chor = arcaneFX::findClientChoreographer(chor_id);
  156. if (chor)
  157. {
  158. chor->setGhostConstraintObject(this, ghost_cons_name);
  159. hookup_with_chor = false;
  160. }
  161. }
  162. Point3F pos = getRenderPosition();
  163. }
  164. U32 afxMooring::packUpdate(NetConnection* conn, U32 mask, BitStream* stream)
  165. {
  166. U32 retMask = Parent::packUpdate(conn, mask, stream);
  167. // InitialUpdate
  168. if (stream->writeFlag(mask & InitialUpdateMask))
  169. {
  170. stream->write(chor_id);
  171. stream->writeString(ghost_cons_name);
  172. }
  173. if (stream->writeFlag(mask & PositionMask))
  174. {
  175. if (mDataBlock->track_pos_only)
  176. mathWrite(*stream, mObjToWorld.getPosition());
  177. else
  178. stream->writeAffineTransform(mObjToWorld);
  179. }
  180. return retMask;
  181. }
  182. //~~~~~~~~~~~~~~~~~~~~//
  183. void afxMooring::unpackUpdate(NetConnection * conn, BitStream * stream)
  184. {
  185. Parent::unpackUpdate(conn, stream);
  186. // InitialUpdate
  187. if (stream->readFlag())
  188. {
  189. stream->read(&chor_id);
  190. ghost_cons_name = stream->readSTString();
  191. if (chor_id != 0 && ghost_cons_name != ST_NULLSTRING)
  192. hookup_with_chor = true;
  193. }
  194. if (stream->readFlag())
  195. {
  196. if (mDataBlock->track_pos_only)
  197. {
  198. Point3F pos;
  199. mathRead(*stream, &pos);
  200. setPosition(pos);
  201. }
  202. else
  203. {
  204. MatrixF mat;
  205. stream->readAffineTransform(&mat);
  206. setTransform(mat);
  207. setRenderTransform(mat);
  208. }
  209. }
  210. }
  211. void afxMooring::setTransform(const MatrixF& mat)
  212. {
  213. Parent::setTransform(mat);
  214. setMaskBits(PositionMask);
  215. }
  216. bool afxMooring::onAdd()
  217. {
  218. if(!Parent::onAdd())
  219. return false;
  220. mObjBox = Box3F(Point3F(-0.5, -0.5, -0.5), Point3F(0.5, 0.5, 0.5));
  221. addToScene();
  222. return true;
  223. }
  224. void afxMooring::onRemove()
  225. {
  226. removeFromScene();
  227. Parent::onRemove();
  228. }
  229. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//