:NAME camera_behavior

:VAR boolean		frozen

:CONFIG angle32		min_angle
:CONFIG angle32		max_angle

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


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

function var_initialize(v)
	v.frozen= false
end

function config_initialize(c)
	c.min_angle= -math.pi		-- +/- 180 degrees
	c.max_angle= math.pi
end

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


function attach(object)
	local aim= object.aim_get()
	if (aim) then
		aim.set_angle(object.transform.rotation)
	end
end

function update(object)

	if (var.frozen) then
		-- do nothing
	else
		local perception= object.perception_get()
		local control= object.control_get()
		if (control and perception) then
			
			local target= perception.get_target()
			if (target and target.is_known() and perception.target_cache.is_visible()) then
				local position= object.world_position_get()
				local look_target= target.location.point
				local deltax= look_target.x - position.x
				local deltay= look_target.y - position.y
				local dist= math.sqrt(deltax*deltax + deltay*deltay)
				control.aim_vector.x= deltax/dist
				control.aim_vector.y= deltay/dist
			end
		end
	end
	
end

component_register(attach, update, nil, nil)
