// File: StartLights.agc
// Created: 20-04-23

#constant LIGHTS_BLINK		1
#constant LIGHTS_RED			2
#constant LIGHTS_YELLOW		3
#constant LIGHTS_GREEN		4
#constant LIGHTS_DONE		5

function StartLights(level ref as tLevel, env ref as tEnvironment)
	e as integer
	r as integer

	if level.lights.mode = LIGHTS_GREEN
		level.lights.mode = LIGHTS_DONE
		exitfunction level.lights.mode
	endif
	
	if GetRawKeyState(32) = 1	
		exitfunction level.lights.mode		
	endif
	
	if level.lights.init = FALSE
		level.lights.init = TRUE
		level.lights.elapsed = timer()
		level.lights.blink = 2
		level.lights.red = level.lights.blink + 1
		level.lights.yellow = level.lights.red + 1
	endif
	
	e = Timer() - level.lights.elapsed
	
	if e < level.lights.blink
		if level.lights.mode <> LIGHTS_BLINK		
			level.lights.mode = LIGHTS_BLINK		
			level.lights.colour = MakeColor(0xff, 0, 0)
		endif
	elseif e < level.lights.red 
		if level.lights.mode <> LIGHTS_RED
			PlaySound(env.coin.sound)
		endif
		level.lights.mode = LIGHTS_RED
		level.lights.colour = MakeColor(0xff, 0, 0)		
	elseif e < level.lights.yellow 
		if level.lights.mode <> LIGHTS_YELLOW
			PlaySound(env.coin.sound)
		endif
		level.lights.mode = LIGHTS_YELLOW
		level.lights.colour = MakeColor(0xff, 0xff, 0)		
	else
		PlaySound(env.start)
		level.lights.mode = LIGHTS_GREEN		
		level.lights.colour = MakeColor(0, 0xff, 0)				
	endif
	
	select level.lights.mode
		case LIGHTS_BLINK
			if timer() - level.lights.time > 0.5
				level.lights.time = Timer()
				r = GetColorRed(level.lights.colour)
				if r > (0xff * 0.5)
					level.lights.colour = DimColour(level.lights.colour, 0.5)	
				else
					level.lights.colour = MakeColor(0xff, 0, 0)		
				endif
			endif
		endcase
		case default
		endcase
	endselect
	
	SetObjectColour(level.lights.obj.id, level.lights.colour)			
	SetObjectEmissive(level.lights.obj.id, level.lights.colour)
		
endfunction level.lights.mode

function DimColour(colour as integer, amount as float)
	c as integer
	MakeColor(GetColorRed(colour) * amount, GetColorGreen(colour) * amount, GetColorBlue(colour) * amount)
endfunction c

function SetObjectColour(id as integer, colour as integer)
		SetObjectColor(id, GetColorRed(colour), GetColorGreen(colour), GetColorBlue(colour), 0xff)
endfunction

function SetObjectEmissive(id as integer, colour as integer)
		SetObjectColorEmissive(id, GetColorRed(colour), GetColorGreen(colour), GetColorBlue(colour))
endfunction