item.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 _ITEM_H_
  23. #define _ITEM_H_
  24. #ifndef _SHAPEBASE_H_
  25. #include "T3D/shapeBase.h"
  26. #endif
  27. #ifndef _BOXCONVEX_H_
  28. #include "collision/boxConvex.h"
  29. #endif
  30. #ifndef _DYNAMIC_CONSOLETYPES_H_
  31. #include "console/dynamicTypes.h"
  32. #endif
  33. class PhysicsBody;
  34. //----------------------------------------------------------------------------
  35. struct ItemData: public ShapeBaseData {
  36. typedef ShapeBaseData Parent;
  37. F32 friction;
  38. F32 elasticity;
  39. bool sticky;
  40. F32 gravityMod;
  41. F32 maxVelocity;
  42. bool lightOnlyStatic;
  43. S32 lightType;
  44. ColorF lightColor;
  45. S32 lightTime;
  46. F32 lightRadius;
  47. bool simpleServerCollision;
  48. ItemData();
  49. DECLARE_CONOBJECT(ItemData);
  50. static void initPersistFields();
  51. virtual void packData(BitStream* stream);
  52. virtual void unpackData(BitStream* stream);
  53. };
  54. //----------------------------------------------------------------------------
  55. class Item: public ShapeBase
  56. {
  57. protected:
  58. typedef ShapeBase Parent;
  59. enum MaskBits {
  60. HiddenMask = Parent::NextFreeMask,
  61. ThrowSrcMask = Parent::NextFreeMask << 1,
  62. PositionMask = Parent::NextFreeMask << 2,
  63. RotationMask = Parent::NextFreeMask << 3,
  64. NextFreeMask = Parent::NextFreeMask << 4
  65. };
  66. // Client interpolation data
  67. struct StateDelta {
  68. Point3F pos;
  69. VectorF posVec;
  70. S32 warpTicks;
  71. Point3F warpOffset;
  72. F32 dt;
  73. };
  74. StateDelta delta;
  75. // Static attributes
  76. ItemData* mDataBlock;
  77. static F32 mGravity;
  78. bool mStatic;
  79. bool mRotate;
  80. //
  81. VectorF mVelocity;
  82. bool mAtRest;
  83. S32 mAtRestCounter;
  84. static const S32 csmAtRestTimer;
  85. bool mInLiquid;
  86. ShapeBase* mCollisionObject;
  87. U32 mCollisionTimeout;
  88. PhysicsBody *mPhysicsRep;
  89. bool mSubclassItemHandlesScene; ///< A subclass of Item will handle all of the adding to the scene
  90. protected:
  91. DECLARE_CALLBACK( void, onStickyCollision, ( const char* objID ));
  92. DECLARE_CALLBACK( void, onEnterLiquid, ( const char* objID, F32 waterCoverage, const char* liquidType ));
  93. DECLARE_CALLBACK( void, onLeaveLiquid, ( const char* objID, const char* liquidType ));
  94. public:
  95. void registerLights(LightManager * lightManager, bool lightingScene);
  96. enum LightType
  97. {
  98. NoLight = 0,
  99. ConstantLight,
  100. PulsingLight,
  101. NumLightTypes,
  102. };
  103. private:
  104. S32 mDropTime;
  105. LightInfo* mLight;
  106. public:
  107. Point3F mStickyCollisionPos;
  108. Point3F mStickyCollisionNormal;
  109. //
  110. private:
  111. OrthoBoxConvex mConvex;
  112. Box3F mWorkingQueryBox;
  113. void updateVelocity(const F32 dt);
  114. void updatePos(const U32 mask, const F32 dt);
  115. void updateWorkingCollisionSet(const U32 mask, const F32 dt);
  116. bool buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &box, const SphereF &sphere);
  117. void buildConvex(const Box3F& box, Convex* convex);
  118. void onDeleteNotify(SimObject*);
  119. static bool _setStatic(void *object, const char *index, const char *data);
  120. static bool _setRotate(void *object, const char *index, const char *data);
  121. protected:
  122. void _updatePhysics();
  123. void prepRenderImage(SceneRenderState *state);
  124. void advanceTime(F32 dt);
  125. public:
  126. DECLARE_CONOBJECT(Item);
  127. Item();
  128. ~Item();
  129. static void initPersistFields();
  130. static void consoleInit();
  131. bool onAdd();
  132. void onRemove();
  133. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  134. bool isStatic() { return mStatic; }
  135. bool isAtRest() { return mAtRest; }
  136. bool isRotating() { return mRotate; }
  137. Point3F getVelocity() const;
  138. void setVelocity(const VectorF& vel);
  139. void applyImpulse(const Point3F& pos,const VectorF& vec);
  140. void setCollisionTimeout(ShapeBase* obj);
  141. ShapeBase* getCollisionObject() { return mCollisionObject; };
  142. void processTick(const Move *move);
  143. void interpolateTick(F32 delta);
  144. virtual void setTransform(const MatrixF &mat);
  145. U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream);
  146. void unpackUpdate(NetConnection *conn, BitStream *stream);
  147. };
  148. typedef Item::LightType ItemLightType;
  149. DefineEnumType( ItemLightType );
  150. #endif