lightning.h

Engine/source/T3D/fx/lightning.h

More...

Classes:

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 _LIGHTNING_H_
 25#define _LIGHTNING_H_
 26
 27#ifndef _GAMEBASE_H_
 28#include "T3D/gameBase/gameBase.h"
 29#endif
 30#ifndef _TORQUE_LIST_
 31#include "core/util/tList.h"
 32#endif
 33#ifndef _COLOR_H_
 34#include "core/color.h"
 35#endif
 36#ifndef _RENDERPASSMANAGER_H_
 37#include "renderInstance/renderPassManager.h"
 38#endif
 39#ifndef _ENGINEAPI_H_
 40#include "console/engineAPI.h"
 41#endif
 42
 43#include "gfx/gfxTextureHandle.h"
 44
 45
 46
 47class ShapeBase;
 48class LightningStrikeEvent;
 49class SFXTrack;
 50
 51
 52// -------------------------------------------------------------------------
 53class LightningData : public GameBaseData
 54{
 55   typedef GameBaseData Parent;
 56
 57  public:
 58   enum Constants {
 59      MaxThunders = 8,
 60      MaxTextures = 8
 61   };
 62
 63   //-------------------------------------- Console set variables
 64  public:
 65   SFXTrack*          thunderSounds[MaxThunders];
 66   SFXTrack*         strikeSound;
 67   StringTableEntry  strikeTextureNames[MaxTextures];
 68
 69   //-------------------------------------- load set variables
 70  public:
 71
 72   GFXTexHandle  strikeTextures[MaxTextures];
 73   U32           numThunders;
 74
 75  protected:
 76   bool onAdd();
 77
 78  public:
 79   LightningData();
 80   ~LightningData();
 81
 82   void packData(BitStream*);
 83   void unpackData(BitStream*);
 84   bool preload(bool server, String &errorStr);
 85
 86   DECLARE_CONOBJECT(LightningData);
 87   static void initPersistFields();
 88};
 89
 90
 91// -------------------------------------------------------------------------
 92struct LightningBolt
 93{
 94
 95   struct Node
 96   {
 97      Point3F        point;
 98      VectorF        dirToMainLine;
 99   };
100
101   struct NodeManager
102   {
103      Node     nodeList[10];
104
105      Point3F  startPoint;
106      Point3F  endPoint;
107      U32      numNodes;
108      F32      maxAngle;
109
110      void generateNodes();
111   };
112
113   NodeManager mMajorNodes;
114   Vector< NodeManager> mMinorNodes;
115   
116   typedef Torque::List<LightningBolt> LightingBoltList;
117   LightingBoltList splitList;
118
119   F32      lifetime;
120   F32      elapsedTime;
121   F32      fadeTime;
122   bool     isFading;
123   F32      percentFade;
124   bool     startRender;
125   F32      renderTime;
126
127   F32      width;
128   F32      chanceOfSplit;
129   Point3F  startPoint;
130   Point3F  endPoint;
131
132   U32      numMajorNodes;
133   F32      maxMajorAngle;
134   U32      numMinorNodes;
135   F32      maxMinorAngle;
136
137   LightningBolt();
138   ~LightningBolt();
139
140   void  createSplit( const Point3F &startPoint, const Point3F &endPoint, U32 depth, F32 width );
141   F32   findHeight( Point3F &point, SceneManager* sceneManager );
142   void  render( const Point3F &camPos );
143   void  renderSegment( NodeManager &segment, const Point3F &camPos, bool renderLastPoint );
144   void  generate();
145   void  generateMinorNodes();
146   void  startSplits();
147   void  update( F32 dt );
148
149};
150
151
152// -------------------------------------------------------------------------
153class Lightning : public GameBase
154{
155   typedef GameBase Parent;
156
157  protected:
158   bool onAdd();
159   void onRemove();
160   bool onNewDataBlock( GameBaseData *dptr, bool reload );
161
162   DECLARE_CALLBACK( void, applyDamage, ( const Point3F& hitPosition, const Point3F& hitNormal, SceneObject* hitObject ));
163
164   struct Strike {
165      F32     xVal;             // Position in cloud layer of strike
166      F32     yVal;             //  top
167
168      bool    targetedStrike;   // Is this a targeted strike?
169      U32     targetGID;
170
171      F32     deathAge;         // Age at which this strike expires
172      F32     currentAge;       // Current age of this strike (updated by advanceTime)
173
174      LightningBolt bolt[3];
175
176      Strike* next;
177   };
178   struct Thunder {
179      F32      tRemaining;
180      Thunder* next;
181   };
182
183  public:
184
185   //-------------------------------------- Console set variables
186  public:
187
188   U32      strikesPerMinute;
189   F32      strikeWidth;
190   F32      chanceToHitTarget;
191   F32      strikeRadius;
192   F32      boltStartRadius;
193   ColorF   color;
194   ColorF   fadeColor;
195   bool     useFog;
196
197   GFXStateBlockRef  mLightningSB;
198
199  protected:
200
201   // Rendering
202   void prepRenderImage(SceneRenderState *state);
203   void renderObject(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance* );
204
205   // Time management
206   void processTick(const Move *move);
207   void interpolateTick(F32 delta);
208   void advanceTime(F32 dt);
209
210   // Strike management
211   void scheduleThunder(Strike*);
212
213   // Data members
214  private:
215   LightningData* mDataBlock;
216
217  protected:
218   U32     mLastThink;        // Valid only on server
219
220   Strike*  mStrikeListHead;   // Valid on on the client
221   Thunder* mThunderListHead;
222
223   static const U32 csmTargetMask;
224
225  public:
226   Lightning();
227   ~Lightning();
228
229   void warningFlashes();
230   void strikeRandomPoint();
231   void strikeObject(ShapeBase*);
232   void processEvent(LightningStrikeEvent*);
233
234   DECLARE_CONOBJECT(Lightning);
235   static void initPersistFields();
236
237   U32  packUpdate  (NetConnection *conn, U32 mask, BitStream *stream);
238   void unpackUpdate(NetConnection *conn,           BitStream *stream);
239};
240
241#endif // _H_LIGHTNING
242
243