:NAME gear_loot

:PARAM float32 pickup_distance
:PARAM string gear_name


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

local vectorlib = require "scripts/vector_math"

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

function param_initialize(p)
	p.pickup_distance= 0.8
end

function var_initialize(v)

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_component= iterator.next()
		if (player_component) then
			local player= player_component.object
			if (not player.is_hidden()) then
				local player_control= player._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.world_position_get()
					if (vectorlib.distance2d(object_point, player_point) < param.pickup_distance) then
						local manager= ws.global_script("gear_manager")	
						if (manager) then
							local gear= manager[param.gear_name]
							if (gear) then
								gear.actual_count = gear.actual_count+1
							end
						end
						object_delete(object)
					end
				end
			end
		else
			break
		end
	end	
end

component_register(attach, update, nil, nil)
