projectile.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. SplashData* splash; // Water Splash Datablock
  90. S32 splashId; // Water splash ID
  91. DecalData *decal; // (impact) Decal Datablock
  92. S32 decalId; // (impact) Decal ID
  93. SFXTrack* sound; // Projectile Sound
  94. LightDescription *lightDesc;
  95. S32 lightDescId;
  96. // variables set on preload:
  97. Resource<TSShape> projectileShape;
  98. S32 activateSeq;
  99. S32 maintainSeq;
  100. ParticleEmitterData* particleEmitter;
  101. S32 particleEmitterId;
  102. ParticleEmitterData* particleWaterEmitter;
  103. S32 particleWaterEmitterId;
  104. ProjectileData();
  105. void packData(BitStream*);
  106. void unpackData(BitStream*);
  107. bool preload(bool server, String &errorStr);
  108. static bool setLifetime( void *object, const char *index, const char *data );
  109. static bool setArmingDelay( void *object, const char *index, const char *data );
  110. static bool setFadeDelay( void *object, const char *index, const char *data );
  111. static const char *getScaledValue( void *obj, const char *data);
  112. static S32 scaleValue( S32 value, bool down = true );
  113. static void initPersistFields();
  114. DECLARE_CONOBJECT(ProjectileData);
  115. DECLARE_CALLBACK( void, onExplode, ( Projectile* proj, Point3F pos, F32 fade ) );
  116. DECLARE_CALLBACK( void, onCollision, ( Projectile* proj, SceneObject* col, F32 fade, Point3F pos, Point3F normal ) );
  117. };
  118. //--------------------------------------------------------------------------
  119. /// Base class for all projectiles.
  120. class Projectile : public GameBase, public ISceneLight
  121. {
  122. typedef GameBase Parent;
  123. static bool _setInitialPosition( void* object, const char* index, const char* data );
  124. static bool _setInitialVelocity( void* object, const char* index, const char* data );
  125. public:
  126. // Initial conditions
  127. enum ProjectileConstants {
  128. SourceIdTimeoutTicks = 7, // = 231 ms
  129. DeleteWaitTime = 500, ///< 500 ms delete timeout (for network transmission delays)
  130. ExcessVelDirBits = 7,
  131. MaxLivingTicks = 4095,
  132. };
  133. enum UpdateMasks {
  134. BounceMask = Parent::NextFreeMask,
  135. ExplosionMask = Parent::NextFreeMask << 1,
  136. NextFreeMask = Parent::NextFreeMask << 2
  137. };
  138. Projectile();
  139. ~Projectile();
  140. DECLARE_CONOBJECT(Projectile);
  141. // SimObject
  142. bool onAdd();
  143. void onRemove();
  144. static void initPersistFields();
  145. // NetObject
  146. F32 getUpdatePriority(CameraScopeQuery *focusObject, U32 updateMask, S32 updateSkips);
  147. U32 packUpdate (NetConnection *conn, U32 mask, BitStream *stream);
  148. void unpackUpdate(NetConnection *conn, BitStream *stream);
  149. // SceneObject
  150. Point3F getVelocity() const { return mCurrVelocity; }
  151. void processTick( const Move *move );
  152. void advanceTime( F32 dt );
  153. void interpolateTick( F32 delta );
  154. // GameBase
  155. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  156. // Rendering
  157. void prepRenderImage( SceneRenderState *state );
  158. void prepBatchRender( SceneRenderState *state );
  159. /// Updates velocity and position, and performs collision testing.
  160. void simulate( F32 dt );
  161. /// What to do once this projectile collides with something
  162. virtual void onCollision(const Point3F& p, const Point3F& n, SceneObject*);
  163. /// What to do when this projectile explodes
  164. virtual void explode(const Point3F& p, const Point3F& n, const U32 collideType );
  165. bool pointInWater(const Point3F &point);
  166. void emitParticles(const Point3F&, const Point3F&, const Point3F&, const U32);
  167. void updateSound();
  168. virtual bool calculateImpact( F32 simTime,
  169. Point3F &pointOfImpact,
  170. F32 &impactTime );
  171. void setInitialPosition( const Point3F& pos );
  172. void setInitialVelocity( const Point3F& vel );
  173. protected:
  174. static const U32 csmStaticCollisionMask;
  175. static const U32 csmDynamicCollisionMask;
  176. static const U32 csmDamageableMask;
  177. static U32 smProjectileWarpTicks;
  178. PhysicsWorld *mPhysicsWorld;
  179. ProjectileData* mDataBlock;
  180. SimObjectPtr< ParticleEmitter > mParticleEmitter;
  181. SimObjectPtr< ParticleEmitter > mParticleWaterEmitter;
  182. SFXSource* mSound;
  183. // These two are server-side only
  184. Point3F mInitialPosition;
  185. Point3F mInitialVelocity;
  186. Point3F mCurrPosition;
  187. Point3F mCurrVelocity;
  188. S32 mSourceObjectId;
  189. S32 mSourceObjectSlot;
  190. // Time related variables common to all projectiles, managed by processTick
  191. U32 mCurrTick; ///< Current time in ticks
  192. SimObjectPtr<ShapeBase> mSourceObject; ///< Actual pointer to the source object, times out after SourceIdTimeoutTicks
  193. // Rendering related variables
  194. TSShapeInstance* mProjectileShape;
  195. TSThread* mActivateThread;
  196. TSThread* mMaintainThread;
  197. // ISceneLight
  198. virtual void submitLights( LightManager *lm, bool staticLighting );
  199. virtual LightInfo* getLight() { return mLight; }
  200. LightInfo *mLight;
  201. LightState mLightState;
  202. bool mHasExploded; ///< Prevent rendering, lighting, and duplicate explosions.
  203. F32 mFadeValue; ///< set in processTick, interpolation between fadeDelay and lifetime
  204. ///< in data block
  205. // Warping and back delta variables. Only valid on the client
  206. //
  207. Point3F mWarpStart;
  208. Point3F mWarpEnd;
  209. U32 mWarpTicksRemaining;
  210. Point3F mCurrDeltaBase;
  211. Point3F mCurrBackDelta;
  212. Point3F mExplosionPosition;
  213. Point3F mExplosionNormal;
  214. U32 mCollideHitType;
  215. };
  216. #endif // _PROJECTILE_H_