projectile.h

Engine/source/T3D/projectile.h

More...

Classes:

class

Base class for all projectiles.

class

Datablock for projectiles. This class is the base class for all other projectiles.

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _PROJECTILE_H_
 25#define _PROJECTILE_H_
 26
 27#ifndef _GAMEBASE_H_
 28#include "T3D/gameBase/gameBase.h"
 29#endif
 30#ifndef __RESOURCE_H__
 31#include "core/resource.h"
 32#endif
 33#ifndef _TSSHAPE_H_
 34#include "ts/tsShape.h"
 35#endif
 36#ifndef _LIGHTDESCRIPTION_H_
 37#include "T3D/lightDescription.h"
 38#endif
 39#ifndef _LIGHTINFO_H_
 40#include "lighting/lightInfo.h"
 41#endif
 42
 43
 44class ExplosionData;
 45class SplashData;
 46class ShapeBase;
 47class TSShapeInstance;
 48class TSThread;
 49class PhysicsWorld;
 50class DecalData;
 51class LightDescription;
 52class SFXTrack;
 53class SFXSource;
 54class ParticleEmitterData;
 55class ParticleEmitter;
 56class Projectile;
 57
 58//--------------------------------------------------------------------------
 59/// Datablock for projectiles.  This class is the base class for all other projectiles.
 60class ProjectileData : public GameBaseData
 61{
 62   typedef GameBaseData Parent;
 63
 64protected:
 65   bool onAdd();
 66
 67public:
 68   // variables set in datablock definition:
 69   // Shape related
 70   const char* projectileShapeName;
 71
 72   /// Set to true if it is a billboard and want it to always face the viewer, false otherwise
 73   bool faceViewer;
 74   Point3F scale;
 75
 76
 77   /// [0,1] scale of how much velocity should be inherited from the parent object
 78   F32 velInheritFactor;
 79   /// Speed of the projectile when fired
 80   F32 muzzleVelocity;
 81
 82   /// Force imparted on a hit object.
 83   F32 impactForce;
 84
 85   /// Should it arc?
 86   bool isBallistic;
 87
 88   /// How HIGH should it bounce (parallel to normal), [0,1]
 89   F32 bounceElasticity;
 90   /// How much momentum should be lost when it bounces (perpendicular to normal), [0,1]
 91   F32 bounceFriction;
 92
 93   /// Should this projectile fall/rise different than a default object?
 94   F32 gravityMod;
 95
 96   /// How long the projectile should exist before deleting itself
 97   U32 lifetime;     // all times are internally represented as ticks
 98   /// How long it should not detonate on impact
 99   S32 armingDelay;  // the values are converted on initialization with
100   S32 fadeDelay;    // the IRangeValidatorScaled field validator
101
102   ExplosionData* explosion;
103   S32 explosionId;
104
105   ExplosionData* waterExplosion;      // Water Explosion Datablock
106   S32 waterExplosionId;               // Water Explosion ID
107
108   SplashData* splash;                 // Water Splash Datablock
109   S32 splashId;                       // Water splash ID
110
111   DecalData *decal;                   // (impact) Decal Datablock
112   S32 decalId;                        // (impact) Decal ID
113
114   SFXTrack* sound;                    // Projectile Sound
115   
116   LightDescription *lightDesc;
117   S32 lightDescId;   
118
119   // variables set on preload:
120   Resource<TSShape> projectileShape;
121   S32 activateSeq;
122   S32 maintainSeq;
123
124   ParticleEmitterData* particleEmitter;
125   S32 particleEmitterId;
126
127   ParticleEmitterData* particleWaterEmitter;
128   S32 particleWaterEmitterId;
129
130   ProjectileData();
131
132   void packData(BitStream*);
133   void unpackData(BitStream*);
134   bool preload(bool server, String &errorStr);
135
136   static bool setLifetime( void *object, const char *index, const char *data );
137   static bool setArmingDelay( void *object, const char *index, const char *data );
138   static bool setFadeDelay( void *object, const char *index, const char *data );
139   static const char *getScaledValue( void *obj, const char *data);
140   static S32 scaleValue( S32 value, bool down = true );
141
142   static void initPersistFields();
143   DECLARE_CONOBJECT(ProjectileData);
144
145   
146   DECLARE_CALLBACK( void, onExplode, ( Projectile* proj, Point3F pos, F32 fade ) );
147   DECLARE_CALLBACK( void, onCollision, ( Projectile* proj, SceneObject* col, F32 fade, Point3F pos, Point3F normal ) );
148};
149
150
151//--------------------------------------------------------------------------
152/// Base class for all projectiles.
153class Projectile : public GameBase, public ISceneLight
154{
155   typedef GameBase Parent;
156
157   static bool _setInitialPosition( void* object, const char* index, const char* data );
158   static bool _setInitialVelocity( void* object, const char* index, const char* data );
159
160public:
161
162   // Initial conditions
163   enum ProjectileConstants {
164      SourceIdTimeoutTicks = 7,   // = 231 ms
165      DeleteWaitTime       = 500, ///< 500 ms delete timeout (for network transmission delays)
166      ExcessVelDirBits     = 7,
167      MaxLivingTicks       = 4095,
168   };
169   enum UpdateMasks {
170      BounceMask    = Parent::NextFreeMask,
171      ExplosionMask = Parent::NextFreeMask << 1,
172      NextFreeMask  = Parent::NextFreeMask << 2
173   };
174
175   
176   Projectile();
177   ~Projectile();
178
179   DECLARE_CONOBJECT(Projectile);
180
181   // SimObject
182   bool onAdd();
183   void onRemove();
184   static void initPersistFields();
185
186   // NetObject
187   F32 getUpdatePriority(CameraScopeQuery *focusObject, U32 updateMask, S32 updateSkips);
188   U32  packUpdate  (NetConnection *conn, U32 mask, BitStream *stream);
189   void unpackUpdate(NetConnection *conn,           BitStream *stream);
190
191   // SceneObject
192   Point3F getVelocity() const { return mCurrVelocity; }
193   void processTick( const Move *move );   
194   void advanceTime( F32 dt );
195   void interpolateTick( F32 delta );   
196
197   // GameBase
198   bool onNewDataBlock( GameBaseData *dptr, bool reload );      
199
200   // Rendering
201   void prepRenderImage( SceneRenderState *state );
202   void prepBatchRender( SceneRenderState *state );   
203
204   /// Updates velocity and position, and performs collision testing.
205   void simulate( F32 dt );
206
207   /// What to do once this projectile collides with something
208   virtual void onCollision(const Point3F& p, const Point3F& n, SceneObject*);
209
210   /// What to do when this projectile explodes
211   virtual void explode(const Point3F& p, const Point3F& n, const U32 collideType );
212      
213   bool pointInWater(const Point3F &point);
214
215   void emitParticles(const Point3F&, const Point3F&, const Point3F&, const U32);
216
217   void updateSound();    
218
219   virtual bool calculateImpact( F32 simTime,
220                                 Point3F &pointOfImpact,
221                                 F32 &impactTime );
222
223   void setInitialPosition( const Point3F& pos );
224   void setInitialVelocity( const Point3F& vel );
225
226protected:
227
228   static const U32 csmStaticCollisionMask;
229   static const U32 csmDynamicCollisionMask;
230   static const U32 csmDamageableMask;   
231   static U32 smProjectileWarpTicks;
232
233   PhysicsWorld *mPhysicsWorld;
234
235   ProjectileData* mDataBlock;
236
237   SimObjectPtr< ParticleEmitter> mParticleEmitter;
238   SimObjectPtr< ParticleEmitter> mParticleWaterEmitter;
239
240   SFXSource* mSound;
241
242   // These two are server-side only
243   Point3F  mInitialPosition;
244   Point3F  mInitialVelocity;
245
246   Point3F  mCurrPosition;
247   Point3F  mCurrVelocity;
248   S32      mSourceObjectId;
249   S32      mSourceObjectSlot;
250
251   // Time related variables common to all projectiles, managed by processTick
252   U32 mCurrTick;                         ///< Current time in ticks
253   SimObjectPtr<ShapeBase> mSourceObject; ///< Actual pointer to the source object, times out after SourceIdTimeoutTicks
254
255   // Rendering related variables
256   TSShapeInstance* mProjectileShape;
257   TSThread*        mActivateThread;
258   TSThread*        mMaintainThread;
259
260   // ISceneLight
261   virtual void submitLights( LightManager *lm, bool staticLighting );
262   virtual LightInfo* getLight() { return mLight; }
263   
264   LightInfo *mLight;
265   LightState mLightState;   
266
267   bool             mHasExploded;   ///< Prevent rendering, lighting, and duplicate explosions.
268   F32              mFadeValue;     ///< set in processTick, interpolation between fadeDelay and lifetime
269                                    ///< in data block
270
271   // Warping and back delta variables.  Only valid on the client
272   //
273   Point3F mWarpStart;
274   Point3F mWarpEnd;
275   U32     mWarpTicksRemaining;
276
277   Point3F mCurrDeltaBase;
278   Point3F mCurrBackDelta;
279
280   Point3F mExplosionPosition;
281   Point3F mExplosionNormal;
282   U32     mCollideHitType;   
283};
284
285#endif // _PROJECTILE_H_
286
287