# Life Particles in Jinx!Script
# based on code writen by Gabriel Henrique Maia 
# found here http://codepen.io/GabrielMaia/pen/YParZw
# translated to Jinx!Script by Bob Kojima
# bob.k.kojima@gmail.com

:init

	config patricles_count = 50 
	config max_radius = 15
	config life_time = 100
	
	w = matrix_x
	h = matrix_y

	for i = 1 to patricles_count step 1				
		gosub setup
	next

end

:render

	# black out screen 
	clear

  for i = 1 to patricles_count step 1
  	gosub update
           
    #outer circle 
    hsv2rgb hue[i], 255, 255, r, g, b 
    circle x[i], y[i], radius[i], r, g, b, 1
    
    #inner circle   
    hsv2rgb hue[i], 150, 255, r, g, b 
		circle x[i], y[i], radius[i] * .66, r, g, b, 1
   
  next

end

:setup
	sx[i] = rnd(4) - 2
	if sx[i] = 0
		sx[i] = -1
	endif
	sy[i] = rnd(4) - 2
	if sy[i] = 0
		sy[i] = -1
	endif
	radius[i] = 4
	radiuscomplete[i] = rnd(max_radius)
	x[i] = rnd(w)
	y[i] = rnd(h)
	hue[i] = rnd(360)
	isactive[i] = 1
	lifetime[i] = rnd(life_time)
	isdead[i] = 0
  return
end

:update
	x[i] = x[i] + sx[i]
	y[i] = y[i] + sy[i]
	
	if radius[i] < radiuscomplete[i] & lifetime[i] > 0 
		radius[i] = radius[i] + 1
	else if radius[i] >= radiuscomplete[i] & isdead[i] = 0
		lifetime[i] = lifetime[i] - 1
		if lifetime[i] = 0
			isdead[i] = 1
		endif
	else if lifetime[i] = 0
		radius[i] = radius[i] - 1
		if radius[i] = 0
			isactive[i] = 0
		endif
	endif

	if x[i] <= radius[i] | x[i] >= w - radius[i]
		sx[i] = sx[i] * -1
	endif
	if y[i] <= radius[i] | y[i] >= h - radius[i]
		sy[i] = sy[i] * -1
	endif
	
	if isactive[i] = 0
		gosub setup
	endif
	
	return
end