turretShape.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TURRETSHAPE_H_
  23. #define _TURRETSHAPE_H_
  24. #ifndef _ITEM_H_
  25. #include "T3D/item.h"
  26. #endif
  27. class PhysicsBody;
  28. class TurretShape;
  29. //----------------------------------------------------------------------------
  30. class TurretShapeData: public ItemData {
  31. typedef ItemData Parent;
  32. public:
  33. enum FireLinkType {
  34. FireTogether, ///< All weapons fire under trigger 0
  35. GroupedFire, ///< Weapon mounts 0,2 fire under trigger 0, mounts 1,3 fire under trigger 1
  36. IndividualFire, ///< Each weapon mount fires under its own trigger 0-3
  37. NumFireLinkTypeBits = 2
  38. };
  39. FireLinkType weaponLinkType; ///< How are the weapons linked together and triggered
  40. enum {
  41. NumMirrorDirectionNodes = 4,
  42. };
  43. enum Recoil {
  44. LightRecoil,
  45. MediumRecoil,
  46. HeavyRecoil,
  47. NumRecoilSequences
  48. };
  49. S32 recoilSequence[NumRecoilSequences];
  50. S32 pitchSequence; ///< Optional sequence played when the turret pitches
  51. S32 headingSequence; ///< Optional sequence played when the turret's heading changes
  52. F32 cameraOffset; ///< Vertical offset
  53. F32 maxHeading; ///< Max degrees to rotate from center. 180 or more indicates full rotation.
  54. F32 minPitch; ///< Min degrees to rotate down from straight ahead
  55. F32 maxPitch; ///< Max degrees to rotate up from straight ahead
  56. F32 headingRate; ///< Degrees per second rotation. 0 means not allowed. Less than 0 means instantaneous.
  57. F32 pitchRate; ///< Degrees per second rotation. 0 means not allowed. Less than 0 means instantaneous.
  58. S32 headingNode; ///< Code controlled node for heading changes
  59. S32 pitchNode; ///< Code controlled node for pitch changes
  60. S32 pitchNodes[NumMirrorDirectionNodes]; ///< Additional nodes that mirror the movements of the pitch node.
  61. S32 headingNodes[NumMirrorDirectionNodes]; ///< Additional nodes that mirror the movements of the heading node.
  62. S32 weaponMountNode[ShapeBase::MaxMountedImages]; ///< Where ShapeBaseImageData weapons are mounted
  63. bool startLoaded; ///< Should the turret's mounted weapon(s) start in a loaded state?
  64. bool zRotOnly; ///< Should the turret allow only z rotations (like an item)?
  65. public:
  66. TurretShapeData();
  67. DECLARE_CONOBJECT(TurretShapeData);
  68. static void initPersistFields();
  69. virtual void packData(BitStream* stream);
  70. virtual void unpackData(BitStream* stream);
  71. virtual bool preload(bool server, String &errorStr);
  72. DECLARE_CALLBACK( void, onMountObject, ( SceneObject* turret, SceneObject* obj, S32 node ) );
  73. DECLARE_CALLBACK( void, onUnmountObject, ( SceneObject* turret, SceneObject* obj ) );
  74. DECLARE_CALLBACK( void, onStickyCollision, ( TurretShape* obj ) );
  75. };
  76. typedef TurretShapeData::FireLinkType TurretShapeFireLinkType;
  77. DefineEnumType( TurretShapeFireLinkType );
  78. //----------------------------------------------------------------------------
  79. class TurretShape: public Item
  80. {
  81. typedef Item Parent;
  82. typedef SceneObject GrandParent;
  83. protected:
  84. enum MaskBits {
  85. TurretUpdateMask = Parent::NextFreeMask << 0, ///< using one mask because we're running out of maskbits
  86. NextFreeMask = Parent::NextFreeMask << 1
  87. };
  88. // Client interpolation data for turret heading and pitch
  89. struct TurretStateDelta {
  90. Point3F rot;
  91. VectorF rotVec;
  92. F32 dt;
  93. };
  94. TurretStateDelta mTurretDelta;
  95. Point3F mRot; ///< Current heading and pitch
  96. bool mPitchAllowed; ///< Are pitch changes allowed
  97. bool mHeadingAllowed; ///< Are heading changes allowed
  98. F32 mPitchUp; ///< Pitch up limit, in radians
  99. F32 mPitchDown; ///< Pitch down limit, in radians
  100. F32 mHeadingMax; ///< Maximum heading limit from center, in radians
  101. F32 mPitchRate; ///< How fast the turret may pitch, in radians per second
  102. F32 mHeadingRate; ///< How fast the turret may yaw, in radians per second
  103. bool mRespawn;
  104. TSThread* mRecoilThread;
  105. TSThread* mImageStateThread;
  106. TSThread* mPitchThread;
  107. TSThread* mHeadingThread;
  108. // Static attributes
  109. TurretShapeData* mDataBlock;
  110. bool mSubclassTurretShapeHandlesScene; ///< A subclass of TurretShape will handle all of the adding to the scene
  111. void _setRotation(const Point3F& rot);
  112. void _updateNodes(const Point3F& rot);
  113. void _applyLimits(Point3F& rot);
  114. bool _outsideLimits(Point3F& rot); ///< Return true if any angle is outside of the limits
  115. void onUnmount(SceneObject* obj,S32 node);
  116. // Script level control
  117. bool allowManualRotation;
  118. bool allowManualFire;
  119. void updateAnimation(F32 dt);
  120. virtual void onImage(U32 imageSlot, bool unmount);
  121. virtual void onImageRecoil(U32 imageSlot,ShapeBaseImageData::StateData::RecoilState);
  122. virtual void onImageStateAnimation(U32 imageSlot, const char* seqName, bool direction, bool scaleToState, F32 stateTimeOutValue);
  123. public:
  124. TurretShape();
  125. virtual ~TurretShape();
  126. static void initPersistFields();
  127. bool onAdd();
  128. void onRemove();
  129. bool onNewDataBlock(GameBaseData *dptr, bool reload);
  130. const char* getStateName();
  131. virtual void updateDamageLevel();
  132. virtual void processTick(const Move *move);
  133. virtual void interpolateTick(F32 dt);
  134. virtual void advanceTime(F32 dt);
  135. virtual void setTransform( const MatrixF &mat );
  136. virtual bool getAllowManualRotation() { return allowManualRotation; }
  137. virtual void setAllowManualRotation(bool allow) { setMaskBits(TurretUpdateMask); allowManualRotation = allow; }
  138. virtual bool getAllowManualFire() { return allowManualFire; }
  139. virtual void setAllowManualFire(bool allow) { setMaskBits(TurretUpdateMask); allowManualFire = allow; }
  140. virtual void updateMove(const Move* move);
  141. bool getNodeTransform(S32 node, MatrixF& mat);
  142. bool getWorldNodeTransform(S32 node, MatrixF& mat);
  143. Point3F getTurretRotation() {return mRot;}
  144. void setTurretRotation(const Point3F& rot) {_setRotation(rot);}
  145. bool doRespawn() { return mRespawn; };
  146. virtual void mountObject( SceneObject *obj, S32 node, const MatrixF &xfm = MatrixF::Identity );
  147. virtual void unmountObject( SceneObject *obj );
  148. virtual void getCameraParameters(F32 *min,F32* max,Point3F* offset,MatrixF* rot);
  149. virtual void getCameraTransform(F32* pos,MatrixF* mat);
  150. virtual void writePacketData( GameConnection* conn, BitStream* stream );
  151. virtual void readPacketData( GameConnection* conn, BitStream* stream );
  152. virtual U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream);
  153. virtual void unpackUpdate(NetConnection *conn, BitStream *stream);
  154. virtual void getWeaponMountTransform( S32 index, const MatrixF &xfm, MatrixF *outMat );
  155. virtual void getRenderWeaponMountTransform( F32 delta, S32 index, const MatrixF &xfm, MatrixF *outMat );
  156. virtual void getImageTransform(U32 imageSlot,MatrixF* mat);
  157. virtual void getRenderImageTransform(U32 imageSlot,MatrixF* mat,bool noEyeOffset=false);
  158. virtual void getImageTransform(U32 imageSlot,S32 node, MatrixF* mat);
  159. virtual void getRenderImageTransform(U32 imageSlot,S32 node, MatrixF* mat);
  160. virtual void prepRenderImage( SceneRenderState* state );
  161. virtual void prepBatchRender( SceneRenderState *state, S32 mountedImageIndex );
  162. DECLARE_CONOBJECT(TurretShape);
  163. };
  164. #endif // _TURRETSHAPE_H_