lightning.h 6.1 KB

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