turretShape.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. StringTableEntry mControlMap;
  66. public:
  67. TurretShapeData();
  68. DECLARE_CONOBJECT(TurretShapeData);
  69. static void initPersistFields();
  70. void packData(BitStream* stream) override;
  71. void unpackData(BitStream* stream) override;
  72. bool preload(bool server, String &errorStr) override;
  73. DECLARE_CALLBACK( void, onMountObject, ( SceneObject* turret, SceneObject* obj, S32 node ) );
  74. DECLARE_CALLBACK( void, onUnmountObject, ( SceneObject* turret, SceneObject* obj ) );
  75. DECLARE_CALLBACK( void, onStickyCollision, ( TurretShape* obj ) );
  76. };
  77. typedef TurretShapeData::FireLinkType TurretShapeFireLinkType;
  78. DefineEnumType( TurretShapeFireLinkType );
  79. //----------------------------------------------------------------------------
  80. class TurretShape: public Item
  81. {
  82. typedef Item Parent;
  83. typedef SceneObject GrandParent;
  84. protected:
  85. enum MaskBits {
  86. TurretUpdateMask = Parent::NextFreeMask << 0, ///< using one mask because we're running out of maskbits
  87. NextFreeMask = Parent::NextFreeMask << 1
  88. };
  89. // Client interpolation data for turret heading and pitch
  90. struct TurretStateDelta {
  91. Point3F rot;
  92. VectorF rotVec;
  93. F32 dt;
  94. };
  95. TurretStateDelta mTurretDelta;
  96. Point3F mRot; ///< Current heading and pitch
  97. bool mPitchAllowed; ///< Are pitch changes allowed
  98. bool mHeadingAllowed; ///< Are heading changes allowed
  99. F32 mPitchUp; ///< Pitch up limit, in radians
  100. F32 mPitchDown; ///< Pitch down limit, in radians
  101. F32 mHeadingMax; ///< Maximum heading limit from center, in radians
  102. F32 mPitchRate; ///< How fast the turret may pitch, in radians per second
  103. F32 mHeadingRate; ///< How fast the turret may yaw, in radians per second
  104. bool mRespawn;
  105. TSThread* mRecoilThread;
  106. TSThread* mImageStateThread;
  107. TSThread* mPitchThread;
  108. TSThread* mHeadingThread;
  109. // Static attributes
  110. TurretShapeData* mDataBlock;
  111. bool mSubclassTurretShapeHandlesScene; ///< A subclass of TurretShape will handle all of the adding to the scene
  112. void _setRotation(const Point3F& rot);
  113. void _updateNodes(const Point3F& rot);
  114. void _applyLimits(Point3F& rot);
  115. bool _outsideLimits(Point3F& rot); ///< Return true if any angle is outside of the limits
  116. void onUnmount(SceneObject* obj,S32 node) override;
  117. // Script level control
  118. bool allowManualRotation;
  119. bool allowManualFire;
  120. void updateAnimation(F32 dt);
  121. void onImage(U32 imageSlot, bool unmount) override;
  122. void onImageRecoil(U32 imageSlot,ShapeBaseImageData::StateData::RecoilState) override;
  123. void onImageStateAnimation(U32 imageSlot, const char* seqName, bool direction, bool scaleToState, F32 stateTimeOutValue) override;
  124. public:
  125. TurretShape();
  126. virtual ~TurretShape();
  127. static void initPersistFields();
  128. bool onAdd() override;
  129. void onRemove() override;
  130. bool onNewDataBlock(GameBaseData *dptr, bool reload) override;
  131. const char* getStateName();
  132. void updateDamageLevel() override;
  133. void processTick(const Move *move) override;
  134. void interpolateTick(F32 dt) override;
  135. void advanceTime(F32 dt) override;
  136. void setTransform( const MatrixF &mat ) override;
  137. virtual bool getAllowManualRotation() { return allowManualRotation; }
  138. virtual void setAllowManualRotation(bool allow) { setMaskBits(TurretUpdateMask); allowManualRotation = allow; }
  139. virtual bool getAllowManualFire() { return allowManualFire; }
  140. virtual void setAllowManualFire(bool allow) { setMaskBits(TurretUpdateMask); allowManualFire = allow; }
  141. virtual void updateMove(const Move* move);
  142. bool getNodeTransform(S32 node, MatrixF& mat);
  143. bool getWorldNodeTransform(S32 node, MatrixF& mat);
  144. Point3F getTurretRotation() {return mRot;}
  145. void setTurretRotation(const Point3F& rot) {_setRotation(rot);}
  146. bool doRespawn() { return mRespawn; };
  147. void mountObject( SceneObject *obj, S32 node, const MatrixF &xfm = MatrixF::Identity ) override;
  148. void unmountObject( SceneObject *obj ) override;
  149. void getCameraParameters(F32 *min,F32* max,Point3F* offset,MatrixF* rot) override;
  150. void getCameraTransform(F32* pos,MatrixF* mat) override;
  151. void writePacketData( GameConnection* conn, BitStream* stream ) override;
  152. void readPacketData( GameConnection* conn, BitStream* stream ) override;
  153. U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream) override;
  154. void unpackUpdate(NetConnection *conn, BitStream *stream) override;
  155. virtual void getWeaponMountTransform( S32 index, const MatrixF &xfm, MatrixF *outMat );
  156. virtual void getRenderWeaponMountTransform( F32 delta, S32 index, const MatrixF &xfm, MatrixF *outMat );
  157. void getImageTransform(U32 imageSlot,MatrixF* mat) override;
  158. void getRenderImageTransform(U32 imageSlot,MatrixF* mat,bool noEyeOffset=false) override;
  159. void getImageTransform(U32 imageSlot,S32 node, MatrixF* mat) override;
  160. void getRenderImageTransform(U32 imageSlot,S32 node, MatrixF* mat) override;
  161. void prepRenderImage( SceneRenderState* state ) override;
  162. void prepBatchRender( SceneRenderState *state, S32 mountedImageIndex ) override;
  163. DECLARE_CONOBJECT(TurretShape);
  164. };
  165. #endif // _TURRETSHAPE_H_