projectile.h 9.6 KB

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