projectile.h 9.3 KB

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