home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 63 / CDACTUAL63.iso / Aplicaciones / DarkBasic / DemoDarkBasic.exe / help / examples / basic3d / exam15.dba < prev    next >
Encoding:
Text File  |  1999-12-08  |  3.5 KB  |  127 lines

  1. Rem * Title  : Particle Bombs
  2. Rem * Author : Aristides Mytaras
  3. Rem * Date   : 13th November 1999
  4. rem ===============================================
  5. rem DARK BASIC demonstration of particle explosions
  6. rem ===============================================
  7. rem An original example by Aristides Mytaras
  8. rem -----------------------------------------------
  9. hide mouse
  10.  
  11. rem Arrays for position, velocity and life of each particle
  12. dim partx#(300)
  13. dim party#(300)
  14. dim partz#(300)
  15. dim partxvel#(300)
  16. dim partyvel#(300)
  17. dim partzvel#(300)
  18. dim partlife(300)
  19. inten#=50
  20. camz#=500
  21.  
  22. rem User prompt
  23. dim max(1)
  24. max(1)=99
  25. center text 320,20,"...PROCESSING "+str$(max(1))+" PARTICLES..."
  26.  
  27. rem Make the particle objects, starting from object 50
  28. sync on
  29. for t=1 to max(1) : make object plain (50+t),5,5 : next t
  30. color backdrop 0
  31.  
  32. rem Main loop
  33. do
  34.  
  35. rem User prompt
  36. center text 320,20,"...USE SPACEBAR TO EXPLODE..."
  37.  
  38. rem if <space> is pressed...
  39. if spacekey()=1
  40.     rem ...get a random point...
  41.     x#=rnd(500)
  42.     y#=rnd(500)+10
  43.     z#=rnd(500)
  44.     rem ...and request an explosion,max particles,intensity,80 life
  45.     request_explosion(x#,y#,z#,max(1),inten#,80)
  46. endif
  47.  
  48. rem use the upkey and downkey to vary intensity
  49. rem and the mouse buttons to zoom in and out
  50. if upkey()=1 then inten#=inten#+1:set cursor 0,0:print "intensity:";inten#
  51. if downkey()=1 then inten#=inten#-1:set cursor 0,0:print "intensity:";inten#
  52. if mouseclick()=2 then camz#=camz#+10
  53. if mouseclick()=1 then camz#=camz#-10
  54.  
  55. position camera -100,100,camz#
  56. point camera 150,150,150
  57.  
  58. rem this function must be called once every cycle to deal with the particles
  59. handle_explosion
  60.  
  61. sync
  62. loop
  63.  
  64. rem functions:
  65.  
  66. rem -----------------
  67. rem request explosion
  68. function request_explosion(ex#,ey#,ez#,particles,intensity#,life)
  69. t=0:p=0
  70. repeat
  71.     t=t+1
  72.         rem if the particular particle (gettit?) is not busy... (is dead):
  73.         if partlife(t)=0
  74.             p=p+1
  75.             rem ...make it alive...
  76.             partlife(t)=life + rnd(life/5)
  77.             rem ....put it at the center of the explosion....
  78.             partx#(t)=ex#
  79.             party#(t)=ey#
  80.             partz#(t)=ez#
  81.             rem ...and give it some push.
  82.             partyvel#(t)=rnd(intensity#)-(intensity#/2)
  83.             partxvel#(t)=rnd(intensity#)-(intensity#/2)
  84.             partzvel#(t)=rnd(intensity#)-(intensity#/2)
  85.             rem ...oh, and make it visible!
  86.             show object (50+t)
  87.         endif
  88.     rem if the number of particles requested is met, exit the loop
  89.     if p>=particles then exit
  90. until t>=max(1)
  91. endfunction
  92.  
  93. rem ----------------
  94. rem handle explosion
  95. function handle_explosion()
  96.  
  97. for t=1 to max(1)
  98.     rem if the particle is alive...
  99.     if partlife(t)>1
  100.         partlife(t) = partlife(t) - 1
  101.         position object 50+t,partx#(t),party#(t),partz#(t)
  102.         rem calculate friction
  103.         partxvel#(t)=partxvel#(t)*0.95
  104.         partyvel#(t)=partyvel#(t)*0.95
  105.         partzvel#(t)=partzvel#(t)*0.95
  106.         rem and gravity...
  107.         partyvel#(t)=partyvel#(t)-0.5
  108.         rem and add the acceleration values to the actual coordinates.
  109.         partx#(t) = partx#(t) + partxvel#(t)
  110.        party#(t) = party#(t) + partyvel#(t)
  111.         partz#(t) = partz#(t) + partzvel#(t)
  112.         rem if it hits the ground, reverse its horisontal acceleration so it bounces
  113.         if party#(t)<=0 then partyvel#(t)=partyvel#(t)*-0.7
  114.         rem do some random rotation so it looks good...
  115.         rotate object t+50,wrapvalue(object angle x(t+50)+rnd(50)),wrapvalue(object angle y(t+50)+rnd(50)),0
  116.     endif
  117.     rem if particle is close to dying...
  118.     if partlife(t)=1
  119.         rem ...make it dead
  120.         partlife(t)=0
  121.         rem ...and hide it too.
  122.         hide object 50+t
  123.     endif
  124. next t
  125.  
  126. endfunction
  127.