lightning.h 6.4 KB

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