:NAME zip

:PARAM float32		speed_increase    -- CB: could be a flat additive bonus instead
:PARAM float32		duration	

:VAR boolean started
--:VAR boolean disabled
:VAR float32 start_time
--:VAR float32 time

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

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.speed_increase = 3
	p.duration = 2		
end

function var_initialize(v)
	v.started= false
	v.start_time= 0
end


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


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 attach(object)
end

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

		local players_path= player.path_follow_get()
		if (not var.started) then			-- CB: Just starting to zip!
			var.started= true
			var.start_time= time
			
			players_path.speed_modifier= players_path.speed_modifier + param.speed_increase

			local sound= object.sound_get()
			if (sound) then
				sound.play_sound("start", false)
			end
		end
		
		if (var.started) then
			if (time < var.start_time + param.duration) then
				draw_timer(object, time)
			else
				players_path.speed_modifier= players_path.speed_modifier - param.speed_increase

				local sound= object.sound_get()
				if (sound) then
					sound.play_sound("end", false)
				end

				object_delete(object)
			end
			
		end	
	else
		-- delete myself if I have somehow become detached
		object_delete(object)
	end
end

component_register(attach, update, nil, nil)
