//*********************************************************************************
//                          SPARK Wrapper Rain Demo
//                  Demonstrating very complex particle systems
//*********************************************************************************



backdrop on
color backdrop 0

hide light 0

// loading the images

textureExplosion = 1
textureFlash = 2
textureSpark1 = 3
textureSpark1 = 4
textureSpark2 = 5
textureWave = 6

load image "media\explosion.bmp",textureExplosion
load image "media\flash.bmp",textureFlash
load image "media\spark1.bmp",textureSpark1
load image "media\point.bmp",textureSpark2
load image "media\wave.bmp",textureWave


// we create a prototype system 
expolsionPrototype = sp create system (1 , 0,0.0,0)

// set the autodelete on , so the instances are deleted when they become inactive

sp set system autodelete expolsionPrototype , 1

// we add 6 groups 

smokeGroup = sp add group (expolsionPrototype ,15, 0.65 ,0,0,0)
flameGroup = sp add group (expolsionPrototype ,15, 0.65 ,0,0,0)
flashGroup = sp add group (expolsionPrototype ,3 , 0.65 ,0,0,0)
spark1Group = sp add group (expolsionPrototype ,20 , 0.65 , 0,0,0)
spark2Group = sp add group (expolsionPrototype ,400 , 0.65 , 0,0,0)
waveGroup = sp add group (expolsionPrototype ,1 , 0.65 , 0,0,0)


//***********************************************************************************
//************************* SET THE SMOKE PARAMETERS ********************************
//***********************************************************************************


sp set group lifetime smokeGroup , 2.5 , 3
smokeRenderer = sp set quad renderer (smokeGroup,textureExplosion,1,1,2,2,2)

// for performance reasons we set our renderer shared 
// so all instances will use
// this renderer ( we don't need a new renderer each time
// a new explosion is created...)

sp set shared smokeRenderer ,1

smokeSizeInterpolator = sp add interpolator (smokeGroup , PARAM_SCALE ,0.6,0.8,1,1.4)
smokeTextureInterpolator= sp add interpolator (smokeGroup , PARAM_ATLAS_DIMENSIONS , 0,4 , 1)
angleInterpolator = sp add interpolator (smokeGroup , PARAM_ANGLE , 0 , 1.57 , 0 , 1.57)

// we also share the interpolators...

sp set shared smokeSizeInterpolator ,1
sp set shared smokeTextureInterpolator,1
sp set shared angleInterpolator,1

// set a complex graph color interpolator

ent1 = sp rgba(51,51,51,0)
ent2 = sp rgba(51,51,51,102)
ent3 = sp rgba(51,51,51,153)
ent4 = sp rgba(51,51,51,102)
ent5 = sp rgba(51,51,51,153)
ent6 = sp rgba(51,51,51,0)

colorGraph = sp add rgba graph(smokeGroup)
sp add rgba graph entry colorGraph , 0 , ent1 
sp add rgba graph entry colorGraph , 0.4 , ent2 , ent3 // <- rnd colors between ent2-ent3
sp add rgba graph entry colorGraph , 0.6 , ent4 , ent5 // <- rnd colors between ent4-ent5
sp add rgba graph entry colorGraph , 1.0 , ent6

sp set shared colorGraph,1

// only one emitter

smokeEmitter = sp add random emitter (smokeGroup,-1,0.02,0.04 ,15,sp sphere zone(0,0,0,0.6))

// and a gravity modifier.

smokeGrav = sp add gravity modifier (smokeGroup , 0,0.05,0)
sp set shared smokeGrav,1


//***********************************************************************************
//************************* SET THE FLAME PARAMETERS ********************************
//***********************************************************************************

sp set group lifetime flameGroup , 1.5 , 2
flameRenderer = sp set quad renderer (flameGroup,textureExplosion,1,1,2,2,2)
sp set shared flameRenderer ,1


// interpolators :

// set a graph interpolator for the scale parameter

sizeInterpolator = sp add param graph(flameGroup , PARAM_SCALE)
sp add param graph entry sizeInterpolator , 0 , 0.25
sp add param graph entry sizeInterpolator , 0.02 , 0.6 , 0.8
sp add param graph entry sizeInterpolator , 1 , 1, 1.4

FTextureInterpolator= sp add interpolator (flameGroup , PARAM_ATLAS_DIMENSIONS , 0,4 , 1)
FangleInterpolator = sp add interpolator (flameGroup , PARAM_ANGLE , 0 , 1.57 , 0 , 1.57)


sp set shared FTextureInterpolator,1
sp set shared FangleInterpolator,1
sp set shared sizeInterpolator,1

// color graph
rgbaGraph = sp add rgba graph(flameGroup)
sp add rgba graph entry rgbaGraph , 0.5 , 0xFF8033FF
sp add rgba graph entry rgbaGraph , 1 , 0x33333300
sp set shared rgbaGraph,1

// and finally the flame emitter

flameEmitter = sp add normal emitter (flameGroup,-1 , 0.06 ,0.1 ,15 , sp sphere zone(0,0,0,0.4) , sp point zone(0,0,0))


//***********************************************************************************
//************************* SET THE FLASH PARAMETERS ********************************
//***********************************************************************************

// set a very short lifetime , as it is just a flash 

sp set group lifetime flashGroup , 0.5 , 0.5
flashRenderer = sp set quad renderer (flashGroup,textureFlash,1,1,1,1,2)
sp set shared flashRenderer ,1

// only the scale parameter is interpolated

flahGraph1 = sp add param graph (flashGroup , PARAM_SCALE) 
sp add param graph entry flahGraph1 , 0 ,0.25
sp add param graph entry flahGraph1 , 0.1 ,1,2
sp set shared flahGraph1,1

// color graph for the flash

flahGraph2 = sp add rgba graph (flashGroup)
sp add rgba graph entry flahGraph2 , 0 , sp rgba (128,128,128,255) 
sp add rgba graph entry flahGraph2 , 0.4 , sp rgba (128,128,128,0)
sp set shared flahGraph2,1

// and an emitter

flashEmitter = sp add static emitter (flashGroup , -1 , 3 , sp sphere zone (0,0,0,0.4))
//***********************************************************************************
//************************* SET THE SPARK1 PARAMETERS *******************************
//***********************************************************************************

sp set group lifetime spark1Group , 0.1 , 1.0

// set the quads size to x = 0.05 and y = 1.0 (thin lines) 
spark1Renderer = sp set quad renderer (spark1Group,textureSpark1,0.05,1,1,1,2)

// And now we do something really cool but very advanced.Set the quads orientation
// in this renderer to be aligned to a direction.

sp set quad orientation spark1Renderer , DIRECTION_ALIGNED , 1,0,0 , 0,1,0
sp set shared spark1Renderer ,1

// scale parameter
spark1size = sp add interpolator (spark1Group ,PARAM_SCALE, 0.2 , 0.4 , 0)
sp set shared spark1size,1

// color interpolator 
spark1Rgba = sp add rgba interpolator (spark1Group , sp rgba(128,128,128,255), sp rgba(128,128,128,0) ,0)
sp set shared spark1Rgba,1

// spark1 emitter :
spark1Emitter = sp add normal emitter (spark1Group ,-1, 2,3 , 20 , sp point zone(0,0,0) , sp point zone(0,0,0)) 

// spark1 gravity force :
spark1Grav = sp add gravity modifier (spark1Group , 0,-1.5,0)
sp set shared spark1Grav,1


//***********************************************************************************
//************************* SET THE SPARK2 PARAMETERS *******************************
//***********************************************************************************

sp set group lifetime spark2Group , 1 , 3
spark2Renderer = sp set quad renderer (spark2Group,textureSpark2,0.02,0.02,1,1,2)
sp set shared spark2Renderer ,1

// generating colors for the random color interpolator.
color1 = sp rgba (255, 255 , 178 , 255)
color2 =sp rgba(255 , 255 , 178 ,255)
color3 =sp rgba (255 , 76 , 76 , 0)
color4 = sp rgba (255 , 255 , 76 , 0)

// color interpolator
spark2Rgba = sp add rgba interpolator (spark2Group , color1 , color2 ,color3 ,color4)
sp set shared spark2Rgba,1

// emitter
spark2Emitter = sp add normal emitter (spark2Group ,-1, 0.4 ,0.8 , 400 , sp point zone(0,0,0) , sp point zone(0,0,0)) 

// gravity force
spark2Grav = sp add gravity modifier (spark2Group , 0,-0.3,0)
sp set shared spark2Grav,1


//***********************************************************************************
//************************* SET THE WAVE PARAMETERS *******************************
//***********************************************************************************

sp set group lifetime waveGroup , 0.8 , 0.8
waveRenderer = sp set quad renderer (waveGroup,textureWave,1,1,1,1,2)

// we set our quads orientation to fixed (Not facing to the camera but have a fixed
// orientation defined by up and look vectors)
sp set quad orientation waveRenderer , FIXED_ORIENTATION , 1,0,0 , 0,1,0
sp set shared waveRenderer ,1

waveSize = sp add interpolator (waveGroup ,PARAM_SCALE, 0,4,0)

waveColor = sp add rgba interpolator (waveGroup , sp rgba(128,128,128, 211) , sp rgba (128,128,128,0) , 0)

// set the emitter's tank to 1.Only one wave is emited.
waveEmitter = sp add static emitter (waveGroup , -1 , 1 , sp point zone (0,0,0))


sp set shared waveSize,1
sp set shared waveColor,1


// because this is a very complex code it's recomended to save youre explosionSystem
// and load it back in youre game when you need it.

//sp save system "coolExplosion.spk",expolsionPrototype



do


position camera 0,0,0,0
rotate camera 0 ,camera angle x(0)+ mousemovey()*0.2 , camera angle y(0)+ mousemovex()*0.2 ,0
move camera 0,-5

if mouseclick() = 1 and hold = 0
    newExplosion = sp copy system( expolsionPrototype , rnd(5) , rnd(5) , rnd(5))
    hold = 1
    endif
    
if mouseclick() = 0 then hold = 0



sp update

loop
















