:NAME door


:PARAM s_transform2d open_transform

:CONFIG float32 initial_position
--:CONFIG float32 stay_open_duration

:VAR float32 position
:VAR float32 desired_position
:VAR float32 speed
:VAR boolean refresh_position
:VAR boolean hold_open_now
:VAR boolean opening
:VAR boolean closing
:VAR float32 holding_start_time
:VAR c_sound* sound_open
:VAR c_sound* sound_close

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

local vectorlib = require "scripts/vector_math"

k_stay_open_duration = 2.5	-- CB: How long should doors stay open for?

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

-- remember 1 = open, 0 = closed

function param_initialize(p)
	p.open_transform.identity()
	p.open_transform.offset.x= 2	-- CB: Do we need to modify this for wide vs. narrow doors?
end

function config_initialize(c)
	c.initial_position= 0
--	c.stay_open_duration= 1.5
end

function var_initialize(v)
	v.position= 0
	v.desired_position= 0
	v.speed= 0
	v.hold_open_now = false
	v.holding_start_time = -30000	-- CB: Hacky...
	
	v.sound_open= nil
	v.sound_close= nil
	v.opening= false
	v.closing= false
	
end

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

k_a= 10
k_b= -5
k_epsilon= 0.001

k_open_threshold= 4
k_game_time_mode= e_value("e_time_mode", "_time_mode_game")



function attach(object)
	var.position = config.initial_position
	var.desired_position = var.position
	var.speed = 0
	var.refresh_position= true
	
	if (var.desired_position <= 0.5) then
		var.closing= true
	else
		var.opening= true
	end

	local sound= object.sound_get()	
	if (sound) then
		var.sound_open= sound.get_sound_by_name("open")
		var.sound_close= sound.get_sound_by_name("close")
	end
	
end

function update(object)	
	local ws= world_state_get()			-- CB: What's the world and time, Kenneth?
	local time= ws.time.time	
	local dt= ws.time.delta_t()
				
	-- CB: Adding this next section
--	if((var.hold_open_now) and ((config.stay_open_duration + var.holding_start_time) >= time))then
		--print("CB: holding the door open")
--	else
	if(not (var.hold_open_now and 
			((k_stay_open_duration + var.holding_start_time) >= time))) then  -- CB: If we're NOT holding the door open...
		
		-- run the little system
		local delta= var.desired_position - var.position
		var.hold_open_now = false
		
		if (var.desired_position > 0.5 and not var.opening) then

			if (var.sound_close and var.sound_close.is_playing()) then
				var.sound_close.stop()
			end
			if (var.sound_open and not var.sound_open.is_playing()) then
				var.sound_open.play(k_game_time_mode)
			end
			var.opening= true
			var.closing= false
		
		elseif (var.desired_position <= 0.5 and not var.closing) then

			if (var.sound_open and var.sound_open.is_playing()) then
				var.sound_open.stop()
			end
			if (var.sound_close and not var.sound_close.is_playing()) then
				var.sound_close.play(k_game_time_mode)
			end
			var.opening= false
			var.closing= true
		
		end
		
		if ((math.abs(delta) > k_epsilon) or var.refresh_position) then

			var.refresh_position= false		
			
			local acc= k_a*delta + k_b*var.speed
			var.speed= var.speed + dt*acc

			var.position = var.position + dt*var.speed
			if (var.position > 1) then	-- CB: If open, clamp at "open" and stop the speed.
				--print("CB: The door is open, so start holding it open.")
				var.position= 1
				var.speed= 0
				var.hold_open_now = true
				var.holding_start_time = time
			elseif (var.position < 0) then	-- CB: if closed, stop at "closed" and stop the speed.
				var.position= 0
				var.speed= 0
			end

			local node= object.definition.model.skeleton.nodes[0]
			if (node) then
				local node_state= object.node_state_from_node(node)
				if (node_state) then
					
					local transform= node_state.transform
					local open_transform= param.open_transform

					node_state.position_set(open_transform.offset.x*var.position, open_transform.offset.y*var.position)
					node_state.rotation_set(open_transform.rotation*var.position)
					object.mark_all_nodes_dirty()
				end
			end
			
			if (object.physics_model) then
				object.physics_model.rebuild()
			end
			
			local arena_blocker= object.arena_blocker_get()
			arena_blocker.refresh()
		end
	end
end

component_register(attach, update, nil, nil)
