TGC Codebase Backup



Easy to Use 3D Sparkle Generator by Trek

9th Feb 2007 14:09
Summary

Allows you to create a sparkle fountain with only 3 commands.



Description

This code, which is very similar to my last codebase entry (Explosion Generator), allows you to create fountains of sparks extremely easily and efficiently. One command sets up your generator with a specified range of objects to be used for sparkles, another you stick in your main loop to control any active sparks, and then whenever you want to have a brilliant spray of sparks, you simply call the TriggerSparkles() function with the position, size, etc. of the sparkles you want to generate.

This is very handy for things like when your character collects a powerup or something like that.

To run this demo, simply download the included Spark.png graphic and put it in the same directory as the code.

No credit is required but a reference to "Trek Software" as the author is always appreciated.

To check out my other productions, go to www.TrekSoftware.org



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ` Sparkle Generator - version 1.0
` Programmed by D3
` www.TrekSoftware.org

` Basic setup stuff
set display mode 800,600,16
autocam off
sync on
sync rate 50
hide mouse

position camera 19,11.5,-20.6
rotate camera 14,-44,0

set ambient light 80

color backdrop rgb(150,170,190)

` Sample image of spark. You can use whatever image you'd like. For best results, use one with black around all
` the edges so that the spark won't appear to be cut off on the sides.
load image "Spark.png",2

` Sample floor object.
make object plain 1001,500,500
position object 1001,0,-.1,0
xrotate object 1001,90
fade object 1001,50

` This command initializes the generator. You must call this before using any other function for the generator.
` The first two parameters specify the object range the generator is allowed to use. This makes sure it doesn't
` interfere with any of your other objects. The third parameter is the y-coordinate of the floor. I set this value
` at .6 units higher than the actual floor position so that the sparks wouldn't stick halfway into it.
InitializeSparkles(1,1000,.5)

do
   ` Text instructions.
   center text screen width()/2,screen height()-40,"Press and Hold Space to Generate Sparkles"

   ` This line says that if the spacebar has been pressed and it's been long enough since it was last pressed
   ` to trigger the sparkle generator.
   `
   ` The TriggerSparkles() parameters are as follows : Number of sparkles, x position of generator, y position, z position,
   ` size of sparkles, maximum speed of sparkles, gravity, image number of sparkle.
   if spacekey()=1 and keyPress = 0 then TriggerSparkles(10,0,2,0,2,1,.05,2) : keyPress = 10

   ` Makes the wait counter for the space bar count down so you can activate space again. Irrelevant to the explosion generator.
   if keyPress>0 then dec keyPress

   ` This simple command needs to go in your main loop. It manages the physics and motion of your sparkles.
   ControlSparkles()

   sync
loop

` --------------------------------------------------------------------------------------------------------------------
`    To use the above commands, you need to copy and paste all the functions below this line into your source code.
` --------------------------------------------------------------------------------------------------------------------

` Initializes the global variables used by the generator.
` All are started with the characters "SG_" so as not to override any other variables or dimensions used in
` your program.
function InitializeSparkles(sOR, eOR, yLimit#)

   global SG_startOfRange = sOR
   global SG_endOfRange = eOR
   global SG_lastObject = sOR
   global SG_floor# = yLimit#

   DIM SG_animData#(eOR - sOR,11)

endfunction

` Triggers a burst of sparkles. It first checks to find the earliest range of objects it can use
` and makes sure it's in the allocated range, then it loops through the specified number of sparkles
` creating each one.
`
` You can edit the portion of the code that makes each object to meet your exact specifications.
function TriggerSparkles(num, posX#, posY#, posZ#, size#, maxAccel#, gravity# img)
   sId = SG_endOfRange
   maxAccel# = maxAccel#*100

   while object exist(sId)=0 and sId > SG_startOfRange
      dec sId
   endwhile

   if object exist(sId) = 1
      inc sId
   endif

   if sId+num > SG_endOfRange
      for n=1 to 50 : center text screen width()/2,screen height()/2,"***ERROR - Sparkle Bounds Exceeded***" : sync : next n
      exitfunction
   endif

   SG_lastObject = sId + num

   for n = sId to sId + num
      make object plain n,size#,size#
      position object n,posX#,posY#,posZ#
      texture object n,img
      ghost object on n
      set object light n,0
      set object to camera orientation n
      rotate object n,object angle x(n)+rnd(30),object angle y(n)+rnd(30),object angle z(n)+rnd(30)

      SG_animData#(n-SG_startOfRange,1)=(rnd(maxAccel#-maxAccel#/10)+maxAccel#/10)/100
      SG_animData#(n-SG_startOfRange,2)=gravity#
      SG_animData#(n-SG_startOfRange,3)=SG_animData#(sId-SG_startOfRange,1)
      SG_animData#(n-SG_startOfRange,4)=(rnd(maxAccel#)-maxAccel#/2)/200
      SG_animData#(n-SG_startOfRange,5)=(rnd(maxAccel#)-maxAccel#/2)/200
      SG_animData#(n-SG_startOfRange,6)=100
      SG_animData#(n-SG_startOfRange,7)=0
   next n
endfunction

` The main control function which runs through all the active (or possibly active) sparkles and
` controls their motion with a little basic physics or deletes them if they've faded to be invisible.

function ControlSparkles()

   for n = SG_startOfRange to SG_lastObject
      if object exist(n)=1
         position object n,object position x(n)+SG_animData#(n-SG_startOfRange,4),object position y(n)+SG_animData#(n-SG_startOfRange,1),object position z(n)+SG_animData#(n-SG_startOfRange,5)
         dec SG_animData#(n-SG_startOfRange,1),SG_animData#(n-SG_startOfRange,2)
         zrotate object n,rnd(90)
         if object position y(n)<SG_floor#
            position object n,object position x(n),SG_floor#,object position z(n)
            SG_animData#(n-SG_startOfRange,1) = SG_animData#(n-SG_startOfRange,3) / 2
            SG_animData#(n-SG_startOfRange,3) = SG_animData#(n-SG_startOfRange,3) / 2
            SG_animData#(n-SG_startOfRange,4) = SG_animData#(n-SG_startOfRange,4) / 2
            SG_animData#(n-SG_startOfRange,5) = SG_animData#(n-SG_startOfRange,5) / 2
            SG_animData#(n-SG_startOfRange,7)=1 : set object light n,1
         endif
         if SG_animData#(n-SG_startOfRange,7)=1
            dec SG_animData#(n-SG_startOfRange,6),2
            fade object n,SG_animData#(n-SG_startOfRange,6)
            if SG_animData#(n-SG_startOfRange,6)<=30
               delete object n
            endif
         endif
      endif
   next n

endfunction