:NAME disguise

:PARAM float32			duration
:PARAM s_ping_effect@	creation_ping
:PARAM s_ping_effect@	death_ping

:VAR float32 start_time
:VAR c_object* player
:VAR boolean abort
:VAR boolean played_expiring_sound

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

k_game_time_mode= e_value("e_time_mode", "_time_mode_game")
k_expiring_time_left= 2.0

k_bar_width= 1
k_bar_height= 0.25
k_y_offset= 0.4

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

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


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

function param_initialize(p)
	p.duration = 3
end

function var_initialize(v)
	v.start_time= 0
	v.player= nil
	v.abort= false
	v.played_expiring_sound= false
end


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

function still_running()
	return true -- CB_HACK: fix this.
end

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
	
	render_rectangle(object.plate, top_left, end_point, k_empty_color, true)

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

end

function copy_state(source, target)
	local position= source.world_position_get()
	local rotation= source.transform.rotation
	local velocity= source.physics_model.velocity
	local source_path= source.path_get()
	local target_path= target.path_get()
	local source_laf= source.laf_get()
	local target_laf= target.laf_get()

	target.position_set(position.x, position.y)
	target.rotation_set(rotation)
	target.physics_model.velocity_set(velocity.x, velocity.y)
	
	if (source_path and target_path) then
		source_path.copy_state(target_path)
	end
	
	if (source_path) then
		source_path.clear_path()
	end
	
	if (source_laf and target_laf) then
		source_laf.copy_state(target_laf)
	end
	
	-- transfer over all the player control state!
	local source_control= source._c.player_control
	local target_control= target._c.player_control
	
	target_control.active_touch_index= source_control.active_touch_index
	target_control.touch_active= source_control.touch_active
	target_control.endpoint_active= source_control.endpoint_active
	target_control.target_active= source_control.target_active
	target_control.no_more_steps= source_control.no_more_steps
	target_control.is_immediate= source_control.is_immediate
	target_control.exited= source_control.exited
	target_control.disabled= source_control.disabled
	target_control.invalid_gear_ping_pending= source_control.invalid_gear_ping_pending
	target_control.first_move= source_control.first_move

	target_control.rendered_line= source_control.rendered_line
	target_control.endpoint= source_control.endpoint
	target_control.target= source_control.target

	-- and remove these object from the source (just in case)
	source_control.rendered_line= nil
	source_control.endpoint= nil
	source_control.target= nil

end

function play_ping(object, effect)
	local distance_map= object.distance_map_get()
	if (distance_map) then
		local world_pos= object.world_position_get()
		distance_map.compute_at_point(world_pos.x, world_pos.y)
		distance_map.distance_map.ping(effect)
	end
end

function attach(object)
	local ws= world_state_get()
	local player= ws.primary_player_get()
	
	var.start_time= ws.time.time
	
	if (player) then
		local player_physics= player.physics_model
		local gear_manager= ws.global_script("gear_manager")
	
		copy_state(player, object)
		player.hide(true)
		gear_manager.player_proxy= object
		if (player_physics) then
			player_physics.collisions_disable()
		end
		var.player= player
	end
	
	if (param.creation_ping) then
		play_ping(object, param.creation_ping)
	end
	
	local sound= object.sound_get()	
	if (sound) then
		local deploy_sound= sound.get_sound_by_name("deploy")
		if (deploy_sound) then
			deploy_sound.play(k_game_time_mode)
		end
	end
	
end

function delete_disguise(object)

	local player_control= object._c.player_control
	if (player_control) then
		if (player_control.rendered_line) then
			object_delete(player_control.rendered_line)
		end
		if (player_control.endpoint) then
			object_delete(player_control.endpoint)
		end
		if (player_control.target) then
			object_delete(player_control.target)
		end
	end
	
	if (param.death_ping) then
		play_ping(object, param.death_ping)
	end

	object.delete_all_children()
	object_delete(object)

end


function update(object)
	
	local ws= world_state_get()			-- CB: What's the world and time, Kenneth?
	local time= ws.time.time

	local time_lived= time - var.start_time
	if ((not var.abort) and (time_lived < param.duration)) then
		draw_timer(object, time)
		
		if ((not var.played_expiring_sound) and ((param.duration - time_lived) < k_expiring_time_left)) then
			var.played_expiring_sound= true
			object.sound_play("expiring", k_game_time_mode)
		end
	else
		local player= var.player
		if (player) then
			local player_physics= player.physics_model
			local gear_manager= ws.global_script("gear_manager")
			
			player.hide(false)
			copy_state(object, player)
			gear_manager.player_proxy= nil
			if (player_physics) then
				player_physics.collisions_enable()
			end
		end
		
		delete_disguise(object)		
		
	end
end

component_register(attach, update, nil, nil)
