:NAME crate_timer


:PARAM float32						start_delay
:PARAM s_ping_effect@				ping

:VAR boolean disabled

-- ------------------- globals ------------------- 

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

k_path_offset= 2
k_impulse_scale= 175


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

function param_initialize(p)
--	print("noise_maker param_initialize")
	p.start_delay= 5
	p.ping= nil
end

function var_initialize(v)
--	print("noise_maker var_initialize")
	v.disabled= false
end


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

k_bar_width= 1
k_bar_height= 0.25
k_y_offset= 0.4

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

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

k_cell_center= e_value("e_cell_location", "_cell_center")

function still_running() -- CB_HACK: fix this?
	return true
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
	
	-- 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
	end_point.x = base_point.x + fraction*k_bar_width
	
	render_rectangle(object.plate, top_left, end_point, k_bar_color, true)

end

function attach(object)
	--local sound= object.sound_get()
	--if (sound) then
	--	sound.play_sound("drop", false)
	--end
	
	local ws= world_state_get()
	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
	
	local player_path= player.path_get()
	local throw= true
	
	-- play sound
	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
	
	-- find a direction
	if (player_path and player_path.path_active()) then
		local d= player_path.distance_left()
		if (d > 0.75) then
			throw= false
		end
	end

	if (player and throw) then
	
		-- impulse myself in the forward direction
		local forward_vector= player.forward_vector_get()
		local physics= object.physics_model
		if (physics) then
			physics.impulse(k_impulse_scale*forward_vector.x, k_impulse_scale*forward_vector.y)
		end

		--[[
		local arena_state= nil
		local static_arena= world_get().arena
		
		if (static_arena) then
			arena_state= static_arena.arena_state_get()
		end
		
		if (arena_state) then
			local forward_vector= player.forward_vector_get()

			-- the below code picks a good direction based on a spatial query
			-- but in fact, I think we just want to throw it in our facing direction
			
			local directions= {}
			local success= false
			local horizontal = true
			local target_point= {}
			
			local position= player_path.get_origin()
			
			directions[0]= {}		-- the player's orientation
			directions[1]= {}
			directions[2]= {}
			directions[3]= {}
			directions[4]= {}
			
			directions[0].x= forward_vector.x
			directions[0].y= forward_vector.y
			
			horizontal= (math.abs(forward_vector.x) > math.abs(forward_vector.y))
			
			local hoffset= 1
			local voffset= 1
			
			if (horizontal) then
				voffset= 3
			else
				hoffset= 3
			end
			
			if (forward_vector.x >= 0) then
				directions[hoffset].x= 1
				directions[hoffset+1].x= -1
			else
				directions[hoffset].x= -1
				directions[hoffset+1].x= 1
			end
			directions[hoffset].y= 0
			directions[hoffset+1].y= 0
			
			if (forward_vector.y >= 0) then
				directions[voffset].y= 1
				directions[voffset+1].y= -1
			else
				directions[voffset].y= -1
				directions[voffset+1].y= 1
			end
			directions[voffset].x= 0
			directions[voffset+1].x= 0

			local direction_index= nil
			
			for i= 0, 3 do
				target_point.x= position.x + k_path_offset*directions[i].x
				target_point.y= position.y + k_path_offset*directions[i].y

				if (arena_state.trace_line(player_path.definition.path_radius, position.x, position.y, target_point.x, target_point.y)) then
					direction_index= i
					success= true
					break;				
				end
			end
			
			if (success and direction_index) then
				-- impulse myself in the given direction
				local physics= object.physics_model
				if (physics) then
					physics.impulse(k_impulse_scale*directions[direction_index].x, k_impulse_scale*directions[direction_index].y)
				end
			end
		end
		--]]
	end
end

function update(object)

	if (not var.disabled) then
		local ws= world_state_get()
		local time= ws.time.time

		if ((time - object.placement_time) > param.start_delay) then
			-- do your thing
			
			local spawner= object.spawn_get("crate")
			if (spawner) then
				local crate= spawner.spawn()
				if (crate) then
					
					local arena= world_get().arena
					local position= crate.world_position_get()
					local cell= arena.cell_get(position.x, position.y)
					local new_position= arena.cell_location_get(cell.x, cell.y, k_cell_center)
				
					crate.position_set(new_position.x, new_position.y)
					crate.rotation_set(0)
					
					local arena_blocker= crate.arena_blocker_get()
					if (arena_blocker) then
						arena_blocker.refresh()
					end
					
					-- activation sound
					local sound= object.sound_get()	
					if (sound) then
						local activate_sound= sound.get_sound_by_name("activate")
						if (activate_sound) then
							activate_sound.play(k_game_time_mode)
						end
					end

				end
			end
			
			object_delete(object)

		else
			draw_timer(object, time)
		end
	end		
end

component_register(attach, update, nil, nil)
