:NAME key

:GLOBAL c_object*	key_counter

:PARAM float32 pickup_distance

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

local vectorlib = require "scripts/vector_math"

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

function global_initialize(g)
	g.key_counter= nil
end

function param_initialize(p)
	p.pickup_distance= 0.8
end

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


function attach(object)

end

function update(object)
	local ws= world_state_get()
	local iterator= ws.component_cache.component_player.iterator_get()
	
	while (true) do
		local player= iterator.next()
		if (player) then
			local player_object= player.object
			if (not player_object.is_hidden()) then
				local player_control= player_object._p.player_control
				if (player_control and player_control.can_pick_up_loot) then

					local object_point= object.world_position_get()
					local player_point= player_object.world_position_get()
					if (vectorlib.distance2d(object_point, player_point) < param.pickup_distance) then
					
						local gear_manager= ws.global_script("gear_manager")
						gear_manager.key_count = gear_manager.key_count + 1
						local sound= object.sound_get()
						if (sound) then
							sound.play_sound("aquired", false)
						end
					
						object_delete(object)
					end
				end
			end
		else
			break
		end
	end
end

component_register(attach, update, nil, nil)
