projectile.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 _PROJECTILE_H_
  23. #define _PROJECTILE_H_
  24. #ifndef _GAMEBASE_H_
  25. #include "T3D/gameBase/gameBase.h"
  26. #endif
  27. #ifndef __RESOURCE_H__
  28. #include "core/resource.h"
  29. #endif
  30. #ifndef _TSSHAPE_H_
  31. #include "ts/tsShape.h"
  32. #endif
  33. #ifndef _LIGHTDESCRIPTION_H_
  34. #include "T3D/lightDescription.h"
  35. #endif
  36. #ifndef _LIGHTINFO_H_
  37. #include "lighting/lightInfo.h"
  38. #endif
  39. class ExplosionData;
  40. class SplashData;
  41. class ShapeBase;
  42. class TSShapeInstance;
  43. class TSThread;
  44. class PhysicsWorld;
  45. class DecalData;
  46. class LightDescription;
  47. class SFXTrack;
  48. class SFXSource;
  49. class ParticleEmitterData;
  50. class ParticleEmitter;
  51. class Projectile;
  52. //--------------------------------------------------------------------------
  53. /// Datablock for projectiles. This class is the base class for all other projectiles.
  54. class ProjectileData : public GameBaseData
  55. {
  56. typedef GameBaseData Parent;
  57. protected:
  58. bool onAdd();
  59. public:
  60. // variables set in datablock definition:
  61. // Shape related
  62. const char* projectileShapeName;
  63. /// Set to true if it is a billboard and want it to always face the viewer, false otherwise
  64. bool faceViewer;
  65. Point3F scale;
  66. /// [0,1] scale of how much velocity should be inherited from the parent object
  67. F32 velInheritFactor;
  68. /// Speed of the projectile when fired
  69. F32 muzzleVelocity;
  70. /// Force imparted on a hit object.
  71. F32 impactForce;
  72. /// Should it arc?
  73. bool isBallistic;
  74. /// How HIGH should it bounce (parallel to normal), [0,1]
  75. F32 bounceElasticity;
  76. /// How much momentum should be lost when it bounces (perpendicular to normal), [0,1]
  77. F32 bounceFriction;
  78. /// Should this projectile fall/rise different than a default object?
  79. F32 gravityMod;
  80. /// How long the projectile should exist before deleting itself
  81. U32 lifetime; // all times are internally represented as ticks
  82. /// How long it should not detonate on impact
  83. S32 armingDelay; // the values are converted on initialization with
  84. S32 fadeDelay; // the IRangeValidatorScaled field validator
  85. ExplosionData* explosion;
  86. S32 explosionId;
  87. ExplosionData* waterExplosion; // Water Explosion Datablock
  88. S32 waterExplosionId; // Water Explosion ID
  89. ExplosionData* playerExplosion; // Player Explosion Datablock
  90. S32 playerExplosionId; // Player Explosion ID
  91. SplashData* splash; // Water Splash Datablock
  92. S32 splashId; // Water splash ID
  93. DecalData *decal; // (impact) Decal Datablock
  94. S32 decalId; // (impact) Decal ID
  95. SFXTrack* sound; // Projectile Sound
  96. LightDescription *lightDesc;
  97. S32 lightDescId;
  98. // variables set on preload:
  99. Resource<TSShape> projectileShape;
  100. S32 activateSeq;
  101. S32 maintainSeq;
  102. ParticleEmitterData* particleEmitter;
  103. S32 particleEmitterId;
  104. ParticleEmitterData* particleWaterEmitter;
  105. S32 particleWaterEmitterId;
  106. ProjectileData();
  107. void packData(BitStream*);
  108. void unpackData(BitStream*);
  109. bool preload(bool server, String &errorStr);
  110. static bool setLifetime( void *object, const char *index, const char *data );
  111. static bool setArmingDelay( void *object, const char *index, const char *data );
  112. static bool setFadeDelay( void *object, const char *index, const char *data );
  113. static const char *getScaledValue( void *obj, const char *data);
  114. static S32 scaleValue( S32 value, bool down = true );
  115. static void initPersistFields();
  116. DECLARE_CONOBJECT(ProjectileData);
  117. DECLARE_CALLBACK( void, onExplode, ( Projectile* proj, Point3F pos, F32 fade ) );
  118. DECLARE_CALLBACK( void, onCollision, ( Projectile* proj, SceneObject* col, F32 fade, Point3F pos, Point3F normal ) );
  119. };
  120. //--------------------------------------------------------------------------
  121. /// Base class for all projectiles.
  122. class Projectile : public GameBase, public ISceneLight
  123. {
  124. typedef GameBase Parent;
  125. static bool _setInitialPosition( void* object, const char* index, const char* data );
  126. static bool _setInitialVelocity( void* object, const char* index, const char* data );
  127. public:
  128. // Initial conditions
  129. enum ProjectileConstants {
  130. SourceIdTimeoutTicks = 7, // = 231 ms
  131. DeleteWaitTime = 500, ///< 500 ms delete timeout (for network transmission delays)
  132. ExcessVelDirBits = 7,
  133. MaxLivingTicks = 4095,
  134. };
  135. enum UpdateMasks {
  136. BounceMask = Parent::NextFreeMask,
  137. ExplosionMask = Parent::NextFreeMask << 1,
  138. NextFreeMask = Parent::NextFreeMask << 2
  139. };
  140. Projectile();
  141. ~Projectile();
  142. DECLARE_CONOBJECT(Projectile);
  143. // SimObject
  144. bool onAdd();
  145. void onRemove();
  146. static void initPersistFields();
  147. // NetObject
  148. F32 getUpdatePriority(CameraScopeQuery *focusObject, U32 updateMask, S32 updateSkips);
  149. U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream);
  150. void unpackUpdate(NetConnection *conn, BitStream *stream);
  151. // SceneObject
  152. Point3F getVelocity() const { return mCurrVelocity; }
  153. void processTick( const Move *move );
  154. void advanceTime( F32 dt );
  155. void interpolateTick( F32 delta );
  156. // GameBase
  157. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  158. // Rendering
  159. void prepRenderImage( SceneRenderState *state );
  160. void prepBatchRender( SceneRenderState *state );
  161. /// Updates velocity and position, and performs collision testing.
  162. void simulate( F32 dt );
  163. /// What to do once this projectile collides with something
  164. virtual void onCollision(const Point3F& p, const Point3F& n, SceneObject*);
  165. /// What to do when this projectile explodes
  166. virtual void explode(const Point3F& p, const Point3F& n, const U32 collideType );
  167. bool pointInWater(const Point3F &point);
  168. void emitParticles(const Point3F&, const Point3F&, const Point3F&, const U32);
  169. void updateSound();
  170. virtual bool calculateImpact( F32 simTime,
  171. Point3F &pointOfImpact,
  172. F32 &impactTime );
  173. void setInitialPosition( const Point3F& pos );
  174. void setInitialVelocity( const Point3F& vel );
  175. protected:
  176. static const U32 csmStaticCollisionMask;
  177. static const U32 csmDynamicCollisionMask;
  178. static const U32 csmDamageableMask;
  179. static U32 smProjectileWarpTicks;
  180. PhysicsWorld *mPhysicsWorld;
  181. ProjectileData* mDataBlock;
  182. SimObjectPtr< ParticleEmitter > mParticleEmitter;
  183. SimObjectPtr< ParticleEmitter > mParticleWaterEmitter;
  184. SFXSource* mSound;
  185. // These two are server-side only
  186. Point3F mInitialPosition;
  187. Point3F mInitialVelocity;
  188. Point3F mCurrPosition;
  189. Point3F mCurrVelocity;
  190. S32 mSourceObjectId;
  191. S32 mSourceObjectSlot;
  192. // Time related variables common to all projectiles, managed by processTick
  193. U32 mCurrTick; ///< Current time in ticks
  194. SimObjectPtr<ShapeBase> mSourceObject; ///< Actual pointer to the source object, times out after SourceIdTimeoutTicks
  195. // Rendering related variables
  196. TSShapeInstance* mProjectileShape;
  197. TSThread* mActivateThread;
  198. TSThread* mMaintainThread;
  199. // ISceneLight
  200. virtual void submitLights( LightManager *lm, bool staticLighting );
  201. virtual LightInfo* getLight() { return mLight; }
  202. LightInfo *mLight;
  203. LightState mLightState;
  204. bool mHasExploded; ///< Prevent rendering, lighting, and duplicate explosions.
  205. F32 mFadeValue; ///< set in processTick, interpolation between fadeDelay and lifetime
  206. ///< in data block
  207. // Warping and back delta variables. Only valid on the client
  208. //
  209. Point3F mWarpStart;
  210. Point3F mWarpEnd;
  211. U32 mWarpTicksRemaining;
  212. Point3F mCurrDeltaBase;
  213. Point3F mCurrBackDelta;
  214. Point3F mExplosionPosition;
  215. Point3F mExplosionNormal;
  216. U32 mCollideHitType;
  217. };
  218. #endif // _PROJECTILE_H_