thunder.bb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ;Lightning Effect
  2. ;David Bird
  3. ;[email protected]
  4. ;http://www.birdie72.freeserve.co.uk
  5. ;16/6/01 (beta 2.25)
  6. ;Example of creating a lightning mesh
  7. Graphics3D 640,480
  8. SetBuffer BackBuffer()
  9. ;Set Up collisions here
  10. ;
  11. Const TYPE_MOVE=1
  12. Const TYPE_TERR=2
  13. Collisions TYPE_MOVE,TYPE_TERR,2,3
  14. ;Create a camera
  15. cam=CreateCamera()
  16. CameraRange cam,1,25000
  17. CameraClsMode cam,False,True
  18. PositionEntity cam,0,50,-152
  19. EntityRadius cam,10
  20. EntityType cam,TYPE_MOVE
  21. ;Load terrain
  22. terr=LoadTerrain("hmap.bmp")
  23. TerrainDetail terr,4000
  24. ;Load textures
  25. tex1=LoadTexture("forest.bmp") ;Grass Texture
  26. ;resize textures
  27. ScaleTexture tex1,32,32
  28. EntityTexture terr,tex1
  29. ;scale terrain to size
  30. ScaleEntity terr,50,375,50
  31. EntityType terr,TYPE_TERR
  32. ;Add an infinate plane
  33. plane=CreatePlane()
  34. EntityTexture plane,tex1
  35. ScaleEntity plane,50,1,50
  36. EntityOrder plane,1
  37. EntityType plane,TYPE_TERR
  38. ;Load brush for lightning
  39. Global lightning_brush=LoadBrush("tex0.bmp",3)
  40. ;lightning forking ammount
  41. Const Lightning_Count=3
  42. Global flash=0
  43. ;
  44. ;Load a texture for use with as a cloud
  45. cloud_tex=LoadTexture("tex1.bmp",3)
  46. ;Create a simple plane for the sky
  47. ;and texture it
  48. sky=CreatePlane()
  49. EntityTexture sky,cloud_tex
  50. PositionEntity sky,0,500,0
  51. ScaleTexture cloud_tex,5000,5000
  52. ;Now do the other side
  53. sky2=CopyEntity(sky)
  54. RotateEntity sky2,180,0,0
  55. EntityParent sky2,sky
  56. ;
  57. ; Do audio here
  58. ;
  59. listen=CreateListener(cam,.00001,10,5) ;doh
  60. Global thunder=LoadSound("thunder.wav")
  61. wind_snd=LoadSound("wind.wav")
  62. LoopSound wind_snd
  63. EmitSound(wind_snd,terr)
  64. AmbientLight 30,30,30
  65. While Not KeyDown(1)
  66. ClsColor flash,flash,flash
  67. If flash>30 Then
  68. AmbientLight flash,flash,flash
  69. End If
  70. Cls
  71. If KeyDown(203) TurnEntity cam,0,+1,0 ;cursors to turn
  72. If KeyDown(205) TurnEntity cam,0,-1,0
  73. TranslateEntity cam,0,-9.8,0 ;drop camera to surface
  74. If KeyDown(44) MoveEntity cam,0,0,-8 ;A - fly foward
  75. If KeyDown(30) MoveEntity cam,0,0,8 ;Z - fly backward
  76. If KeyHit(2) Then Create_lightning(Rnd(1000),500,Rnd(1000),500,20)
  77. ;Do update
  78. Update_Lightning(cam)
  79. ;Move the clouds a bit
  80. MoveEntity sky,Sin(MilliSecs()/1000),0,3
  81. UpdateWorld
  82. RenderWorld
  83. Flip
  84. Wend
  85. ;Freeup entity's and textures
  86. Free_Lightning()
  87. FreeTexture tex1
  88. FreeTexture cloud_tex
  89. FreeEntity terr
  90. FreeEntity sky2
  91. FreeEntity sky
  92. FreeEntity plane
  93. FreeEntity cam
  94. EndGraphics
  95. End
  96. Type lightning
  97. Field ent
  98. Field surf
  99. Field alpha#
  100. End Type
  101. ;Update the lightning bolts
  102. Function Update_Lightning(cam)
  103. For a.lightning=Each lightning
  104. a\alpha=a\alpha-.1
  105. If a\alpha<1 Then
  106. EntityAlpha a\ent,a\alpha
  107. EntityColor a\ent,255*a\alpha,255*a\alpha,255
  108. End If
  109. If a\alpha<0 Then FreeEntity a\ent:Delete a
  110. Next
  111. If flash>0 Then flash=flash-20
  112. If flash<0 Then flash=0
  113. End Function
  114. ;Free up any memory used on with lightning.
  115. Function Free_Lightning()
  116. For a.lightning=Each lightning
  117. If a\ent Then FreeEntity a\ent
  118. Delete a
  119. Next
  120. End Function
  121. ;Create's Lightning_count bolts of
  122. ;lightning
  123. Function Create_lightning(gx#,gy#,gz#,high#,seg)
  124. flash=255
  125. pseg=seg
  126. For cnt=0 To Lightning_Count
  127. seg=Rand(4,pseg)
  128. a.lightning=New lightning
  129. a\alpha=2
  130. a\ent=CreateMesh()
  131. EntityFX a\ent,1
  132. a\surf=CreateSurface(a\ent,lightning_brush)
  133. x#=0
  134. y#=0
  135. z#=0
  136. stp#=high/Float(seg)
  137. For b=0 To seg
  138. AddVertex a\surf,x,y,z,0,0
  139. x=x-30
  140. AddVertex a\surf,x,y,z,1,0
  141. x=x+Rnd(-80,80)
  142. y=y-stp
  143. AddVertex a\surf,x,y,z,1,1
  144. x=x+30
  145. AddVertex a\surf,x,y,z,0,1
  146. Next
  147. For b=0 To (4*seg) Step 4
  148. AddTriangle(a\surf,b,b+1,b+2)
  149. AddTriangle(a\surf,b,b+2,b+3)
  150. AddTriangle(a\surf,b,b+2,b+1)
  151. AddTriangle(a\surf,b,b+3,b+2)
  152. Next
  153. PositionEntity a\ent,gx,gy,gz
  154. Next
  155. EmitSound thunder,a\ent
  156. End Function