followPathComponent.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include "T3D/components/game/followPathComponent.h"
  2. IMPLEMENT_CO_DATABLOCK_V1(FollowPathComponent);
  3. IMPLEMENT_CALLBACK(FollowPathComponent, onNode, void, (S32 node), (node),
  4. "A script callback that indicates the path camera has arrived at a specific node in its path. Server side only.\n"
  5. "@param Node Unique ID assigned to this node.\n");
  6. FollowPathComponent::FollowPathComponent()
  7. {
  8. delta.time = 0;
  9. delta.timeVec = 0;
  10. //mDataBlock = 0;
  11. mState = Forward;
  12. mNodeBase = 0;
  13. mNodeCount = 0;
  14. mPosition = 0;
  15. mTarget = 0;
  16. mTargetSet = false;
  17. }
  18. FollowPathComponent::~FollowPathComponent()
  19. {
  20. }
  21. bool FollowPathComponent::onAdd()
  22. {
  23. if (!Parent::onAdd())
  24. return false;
  25. /*// Initialize from the current transform.
  26. if (!mNodeCount)
  27. {
  28. QuatF rot(getTransform());
  29. Point3F pos = getPosition();
  30. mSpline.removeAll();
  31. mSpline.push_back(new CameraSpline::Knot(pos, rot, 1,
  32. CameraSpline::Knot::NORMAL, CameraSpline::Knot::SPLINE));
  33. mNodeCount = 1;
  34. }
  35. //
  36. mObjBox.maxExtents = mObjScale;
  37. mObjBox.minExtents = mObjScale;
  38. mObjBox.minExtents.neg();
  39. resetWorldBox();*/
  40. return true;
  41. }
  42. void FollowPathComponent::onRemove()
  43. {
  44. Parent::onRemove();
  45. }
  46. void FollowPathComponent::initPersistFields()
  47. {
  48. Parent::initPersistFields();
  49. }
  50. void FollowPathComponent::componentAddedToOwner(Component *comp)
  51. {
  52. Parent::componentAddedToOwner(comp);
  53. }
  54. void FollowPathComponent::componentRemovedFromOwner(Component *comp)
  55. {
  56. Parent::componentRemovedFromOwner(comp);
  57. }
  58. void FollowPathComponent::processTick()
  59. {
  60. Parent::processTick();
  61. // Move to new time
  62. advancePosition(TickMs);
  63. // Set new position
  64. MatrixF mat;
  65. interpolateMat(mPosition, &mat);
  66. mOwner->setTransform(mat);
  67. mOwner->updateContainer();
  68. }
  69. void FollowPathComponent::interpolateTick(F32 dt)
  70. {
  71. Parent::interpolateTick(dt);
  72. MatrixF mat;
  73. interpolateMat(delta.time + (delta.timeVec * dt), &mat);
  74. mOwner->setRenderTransform(mat);
  75. }
  76. void FollowPathComponent::interpolateMat(F32 pos, MatrixF* mat)
  77. {
  78. /*CameraSpline::Knot knot;
  79. mSpline.value(pos - mNodeBase, &knot);
  80. knot.mRotation.setMatrix(mat);
  81. mat->setPosition(knot.mPosition);*/
  82. }
  83. void FollowPathComponent::advanceTime(F32 dt)
  84. {
  85. Parent::advanceTime(dt);
  86. }
  87. void FollowPathComponent::advancePosition(S32 ms)
  88. {
  89. /*delta.timeVec = mPosition;
  90. // Advance according to current speed
  91. if (mState == Forward) {
  92. mPosition = mSpline.advanceTime(mPosition - mNodeBase, ms);
  93. if (mPosition > F32(mNodeCount - 1))
  94. mPosition = F32(mNodeCount - 1);
  95. mPosition += (F32)mNodeBase;
  96. if (mTargetSet && mPosition >= mTarget) {
  97. mTargetSet = false;
  98. mPosition = mTarget;
  99. mState = Stop;
  100. }
  101. }
  102. else
  103. if (mState == Backward) {
  104. mPosition = mSpline.advanceTime(mPosition - mNodeBase, -ms);
  105. if (mPosition < 0)
  106. mPosition = 0;
  107. mPosition += mNodeBase;
  108. if (mTargetSet && mPosition <= mTarget) {
  109. mTargetSet = false;
  110. mPosition = mTarget;
  111. mState = Stop;
  112. }
  113. }
  114. // Script callbacks
  115. if (int(mPosition) != int(delta.timeVec))
  116. onNode(int(mPosition));
  117. // Set frame interpolation
  118. delta.time = mPosition;
  119. delta.timeVec -= mPosition;*/
  120. }
  121. //----------------------------------------------------------------------------
  122. /*void FollowPathComponent::getCameraTransform(F32* pos, MatrixF* mat)
  123. {
  124. // Overide the ShapeBase method to skip all the first/third person support.
  125. getRenderEyeTransform(mat);
  126. // Apply Camera FX.
  127. mat->mul(gCamFXMgr.getTrans());
  128. }*/
  129. //----------------------------------------------------------------------------
  130. void FollowPathComponent::setPosition(F32 pos)
  131. {
  132. mPosition = mClampF(pos, (F32)mNodeBase, (F32)(mNodeBase + mNodeCount - 1));
  133. MatrixF mat;
  134. interpolateMat(mPosition, &mat);
  135. mOwner->setTransform(mat);
  136. setMaskBits(PositionMask);
  137. }
  138. void FollowPathComponent::setTarget(F32 pos)
  139. {
  140. mTarget = pos;
  141. mTargetSet = true;
  142. if (mTarget > mPosition)
  143. mState = Forward;
  144. else
  145. if (mTarget < mPosition)
  146. mState = Backward;
  147. else {
  148. mTargetSet = false;
  149. mState = Stop;
  150. }
  151. setMaskBits(TargetMask | StateMask);
  152. }
  153. void FollowPathComponent::setState(State s)
  154. {
  155. mState = s;
  156. setMaskBits(StateMask);
  157. }
  158. //-----------------------------------------------------------------------------
  159. void FollowPathComponent::reset(F32 speed)
  160. {
  161. /*CameraSpline::Knot *knot = new CameraSpline::Knot;
  162. mSpline.value(mPosition - mNodeBase, knot);
  163. if (speed)
  164. knot->mSpeed = speed;
  165. mSpline.removeAll();
  166. mSpline.push_back(knot);
  167. mNodeBase = 0;
  168. mNodeCount = 1;
  169. mPosition = 0;
  170. mTargetSet = false;
  171. mState = Forward;
  172. setMaskBits(StateMask | PositionMask | WindowMask | TargetMask);*/
  173. }
  174. /*void FollowPathComponent::pushBack(CameraSpline::Knot *knot)
  175. {
  176. // Make room at the end
  177. if (mNodeCount == NodeWindow) {
  178. delete mSpline.remove(mSpline.getKnot(0));
  179. mNodeBase++;
  180. }
  181. else
  182. mNodeCount++;
  183. // Fill in the new node
  184. mSpline.push_back(knot);
  185. setMaskBits(WindowMask);
  186. // Make sure the position doesn't fall off
  187. if (mPosition < mNodeBase) {
  188. mPosition = (F32)mNodeBase;
  189. setMaskBits(PositionMask);
  190. }
  191. }
  192. void FollowPathComponent::pushFront(CameraSpline::Knot *knot)
  193. {
  194. // Make room at the front
  195. if (mNodeCount == NodeWindow)
  196. delete mSpline.remove(mSpline.getKnot(mNodeCount));
  197. else
  198. mNodeCount++;
  199. mNodeBase--;
  200. // Fill in the new node
  201. mSpline.push_front(knot);
  202. setMaskBits(WindowMask);
  203. // Make sure the position doesn't fall off
  204. if (mPosition > F32(mNodeBase + (NodeWindow - 1)))
  205. {
  206. mPosition = F32(mNodeBase + (NodeWindow - 1));
  207. setMaskBits(PositionMask);
  208. }
  209. }*/
  210. void FollowPathComponent::popFront()
  211. {
  212. /*if (mNodeCount < 2)
  213. return;
  214. // Remove the first node. Node base and position are unaffected.
  215. mNodeCount--;
  216. delete mSpline.remove(mSpline.getKnot(0));
  217. if (mPosition > 0)
  218. mPosition--;*/
  219. }
  220. //----------------------------------------------------------------------------
  221. void FollowPathComponent::onNode(S32 node)
  222. {
  223. //if (!isGhost())
  224. // onNode_callback(node);
  225. }
  226. /*U32 FollowPathComponent::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  227. {
  228. Parent::packUpdate(con, mask, stream);
  229. if (stream->writeFlag(mask & StateMask))
  230. stream->writeInt(mState, StateBits);
  231. if (stream->writeFlag(mask & PositionMask))
  232. stream->write(mPosition);
  233. if (stream->writeFlag(mask & TargetMask))
  234. if (stream->writeFlag(mTargetSet))
  235. stream->write(mTarget);
  236. if (stream->writeFlag(mask & WindowMask)) {
  237. stream->write(mNodeBase);
  238. stream->write(mNodeCount);
  239. for (S32 i = 0; i < mNodeCount; i++) {
  240. CameraSpline::Knot *knot = mSpline.getKnot(i);
  241. mathWrite(*stream, knot->mPosition);
  242. mathWrite(*stream, knot->mRotation);
  243. stream->write(knot->mSpeed);
  244. stream->writeInt(knot->mType, CameraSpline::Knot::NUM_TYPE_BITS);
  245. stream->writeInt(knot->mPath, CameraSpline::Knot::NUM_PATH_BITS);
  246. }
  247. }
  248. // The rest of the data is part of the control object packet update.
  249. // If we're controlled by this client, we don't need to send it.
  250. if (stream->writeFlag(getControllingClient() == con && !(mask & InitialUpdateMask)))
  251. return 0;
  252. return 0;
  253. }
  254. void FollowPathComponent::unpackUpdate(NetConnection *con, BitStream *stream)
  255. {
  256. Parent::unpackUpdate(con, stream);
  257. // StateMask
  258. if (stream->readFlag())
  259. mState = stream->readInt(StateBits);
  260. // PositionMask
  261. if (stream->readFlag())
  262. {
  263. stream->read(&mPosition);
  264. delta.time = mPosition;
  265. delta.timeVec = 0;
  266. }
  267. // TargetMask
  268. if (stream->readFlag())
  269. {
  270. mTargetSet = stream->readFlag();
  271. if (mTargetSet)
  272. stream->read(&mTarget);
  273. }
  274. // WindowMask
  275. if (stream->readFlag())
  276. {
  277. mSpline.removeAll();
  278. stream->read(&mNodeBase);
  279. stream->read(&mNodeCount);
  280. for (S32 i = 0; i < mNodeCount; i++)
  281. {
  282. CameraSpline::Knot *knot = new CameraSpline::Knot();
  283. mathRead(*stream, &knot->mPosition);
  284. mathRead(*stream, &knot->mRotation);
  285. stream->read(&knot->mSpeed);
  286. knot->mType = (CameraSpline::Knot::Type)stream->readInt(CameraSpline::Knot::NUM_TYPE_BITS);
  287. knot->mPath = (CameraSpline::Knot::Path)stream->readInt(CameraSpline::Knot::NUM_PATH_BITS);
  288. mSpline.push_back(knot);
  289. }
  290. }
  291. // Controlled by the client?
  292. if (stream->readFlag())
  293. return;
  294. }*/