lightning.h

Engine/source/T3D/fx/lightning.h

More...

Classes:

Public Defines

define

Detailed Description

Public Defines

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