:NAME blinds


:GLOBAL boolean		started
:GLOBAL boolean		in_progress
:GLOBAL boolean		finished
:GLOBAL int32		blind_index
:GLOBAL float32		start_time
:GLOBAL c_object_placement* blind_placement
:GLOBAL c_object_placement* bloom_placement


k_load_pending_time= 1.5
k_load_delay_time= 0.6				-- the amount of time we just hang out after finishing the blinds
k_total_finish_time= k_load_pending_time + k_load_delay_time
k_load_pending_blind_count= 21
k_load_pending_blind_animation_duration= 0.6
k_load_pending_blind_spacing= 0.2
k_load_pending_blind_delay= (k_load_pending_time-k_load_pending_blind_animation_duration)/(k_load_pending_blind_count-1)
k_blind_rotation= -30*math.pi/180
k_blind_offset= {}
k_blind_offset.x= k_load_pending_blind_spacing*math.cos(k_blind_rotation)
k_blind_offset.y= k_load_pending_blind_spacing*math.sin(k_blind_rotation)



function global_initialize(g)
	g.started= false
	g.finished= false
	g.in_progress= false
	g.blind_index= -1
	g.blind_placement= nil
	g.bloom_placement= nil
	g.start_time= 0

end


function start_blinds()
	global.start_time= world_state_get().master_time.time
	global.in_progress= true
	
	if (global.bloom_placement) then
	
		world_state_get().place(global.bloom_placement)
	
	end
end


function update_blinds()

	local master_time= world_state_get().master_time.time
	local delta_t= master_time - global.start_time

	if (delta_t > k_total_finish_time) then
		-- finished
		global.finished= true
	elseif (global.blind_index < (k_load_pending_blind_count-1)) then
		local desired_index= math.floor(delta_t / k_load_pending_blind_delay)
		if (desired_index > global.blind_index) then
		
			global.blind_index= desired_index
			
			if (global.blind_placement) then
				
				local blind= world_state_get().place(global.blind_placement)
				if (blind) then
					local position= blind.world_position_get()
		
					-- offset to the correct place
					local x= desired_index*k_blind_offset.x + position.x
					local y= desired_index*k_blind_offset.y + position.y
					
					blind.position_set(x, y)
				end
				
			end
		end
	end
end

function start()

	global.blind_placement= world_get().placement_get("blind")
	global.bloom_placement= world_get().placement_get("bloom")

end


function update()
	if (global.started) then
		if (not global.finished) then
		
			if (not global.in_progress) then
				start_blinds()
			end
			
			if (global.in_progress) then
				update_blinds()
			end
		
		end
	end
end






system_register(start, update, nil, nil)
