:NAME door_locked_trigger

:PARAM float32 trigger_distance
:PARAM float32 pick_distance
:PARAM int32 time_to_pick
:PARAM s_function1d slider_animation
--:PARAM int32 start_delay

:VAR boolean is_unlocked
:VAR boolean being_picked
:VAR boolean magic_unlock
:VAR float32 picking_start_time
:VAR float32 unlock_time
:VAR float32 unlock_delay
:VAR s_vector2d p

:GLOBAL c_node*	slider_node
:GLOBAL float32 slider_offset_x


-- ------------------- constants ------------------- 

local vectorlib = require "scripts/vector_math"
k_game_time_mode= e_value("e_time_mode", "_time_mode_game")


k_default_open_threshold= 4
k_default_pick_distance = 3
k_default_pick_time = 3

k_unlock_delay= 0.2

k_bar_width= 1
k_bar_height= 0.25
k_y_offset= 0.4

k_bar_color= {}
k_bar_color.red= 0.85		
k_bar_color.blue= 0			
k_bar_color.green= 0.17			

k_empty_color={}
k_empty_color.red= 0.0		
k_empty_color.blue= 0.0			
k_empty_color.green= 0.0		

-- ----------------------------------------------------- 

function draw_timer(object, time)
	local base_point= object.world_position_get()

	base_point.x = base_point.x - k_bar_width/2
	base_point.y = base_point.y + k_y_offset
	
	local top_left= {}
	top_left.x= base_point.x
	top_left.y= base_point.y + k_bar_height
	
	local end_point= {}
	end_point.x = base_point.x + k_bar_width
	end_point.y = base_point.y
	
	-- args: plate, top-left, bottom-right, color, fill

	render_rectangle(object.plate, top_left, end_point, k_empty_color, true)

--	local fraction= 1 - (time - object.placement_time)/param.start_delay
	local fraction= 1 - (time - var.picking_start_time)/param.time_to_pick	
	end_point.x = base_point.x + fraction*k_bar_width
	
	render_rectangle(object.plate, top_left, end_point, k_bar_color, true)

end

-- ------------------- initialization ------------------- 


function param_initialize(p)
	p.trigger_distance = k_default_open_threshold
	p.pick_distance = k_default_pick_distance
	p.time_to_pick = k_default_pick_time
end

function var_initialize(v)
	v.is_unlocked = false
	v.being_picked = false
	v.magic_unlock = false
	v.picking_start_time = -1
	v.unlock_delay= 0
	v.p.x= 0
	v.p.y= 0
end

function global_initialize(g)
	g.slider_node= nil
	g.slider_offset_x= 0
end

-- ------------------- component ------------------- 

function attach(object)
	local position= object.world_position_get()
	local p= var.p

	p.x= position.x
	p.y= position.y

	local model= object.model_get()
	if (model) then
		local iterator= model.markers.iterator_get()
		local marker0= iterator.next()
		
		if (marker0) then
			local pos0= marker0.world_position_get(object)
			p.x= pos0.x
			p.y= pos0.y
		end
	end
	
	--find the slider node
	
	local model= object.model_get()
	if (model) then
		local skeleton= model.skeleton
		if (skeleton) then
			global.slider_node= skeleton.node_get_by_name("slider")
			if (global.slider_node) then
				local slider_state= object.node_state_from_node(global.slider_node)
				if (slider_state) then
					global.slider_offset_x= slider_state.transform.offset.x
				end
			end
		end
	end
end

function start_unlock(object, time)
	var.unlock_time= time
	var.unlock_delay= k_unlock_delay
	
	local sound= object.sound_get()	
	if (sound) then
		local unlock_sound= sound.get_sound_by_name("unlock")
		if (unlock_sound) then
			unlock_sound.play(k_game_time_mode)
		end
	end
	
end

function update(object)

	local door_script= object._c.door
	
	if (door_script) then
		local desired_position= 0
		local ws = world_state_get()
		
		if (var.is_unlocked) then
			desired_position= 1
			sleep()
		elseif (var.unlock_delay > 0) then

			var.unlock_delay = var.unlock_delay - ws.time.delta_t()
			if (var.unlock_delay <= 0) then
				var.unlock_delay= 0
				var.is_unlocked= true
			end
			
			-- animate the slider
			if (global.slider_node) then
				local slider_state= object.node_state_from_node(global.slider_node)
				
				if (slider_state) then
					local t= ws.time.time - var.unlock_time
					local x= param.slider_animation.evaluate(t) + global.slider_offset_x
					local y= slider_state.transform.offset.y
					
					slider_state.position_set(x, y)
				
				end
			end

		elseif (var.magic_unlock) then
			start_unlock(object, ws.time.time)
		else
		
			local ws= world_state_get()
			local door_position= var.p
			local picking= false

			-- only look at the primary player!
			-- note that when disguised, the disguise object is the primary player

			local player= ws.primary_player_get()
			local gear_manager= ws.global_script("gear_manager")
			if (gear_manager and gear_manager.player_proxy) then
				player= gear_manager.player_proxy
			end
			
			if (player) then
				local player_position= player.world_position_get()
				local d= vectorlib.distance2d(player_position, door_position)
				if ((d < param.trigger_distance) and (d <= param.pick_distance)) then
					picking= true
				end
			end

			if (picking) then
				local gear_manager= ws.global_script("gear_manager")
				local time= ws.time.time
				
				if (gear_manager.key_count > 0) then
					-- key unlocks immediately
					gear_manager.key_count = gear_manager.key_count - 1
					start_unlock(object, time)
					
				elseif (var.being_picked and (time < (var.picking_start_time + param.time_to_pick))) then
					-- pick in progress
					draw_timer(object, time)
					
				elseif (var.being_picked and (time >= (var.picking_start_time + param.time_to_pick))) then
					-- finished picking
					var.being_picked = false
					start_unlock(object, time)
								
				elseif (not var.being_picked) then
					-- start picking
					var.picking_start_time = time
					var.being_picked = true
				end

			elseif (var.being_picked) then
				-- reset the pick process if we're not picking				
				var.being_picked = false
				var.picking_start_time = -1
			end
					
		end
		
		door_script.desired_position= desired_position
	end	
end

component_register(attach, update, nil, nil)
