2
0

lightning.h 6.1 KB

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