extends Camera

#var target = Vector3(0.01,0,0)
var camrot = Vector2()
var camrotl = Vector2()
var cam_radius = 6
var cam_radiusl = 0
var cam_pos = Vector3()

func _ready():
	pass

func _input(ev):

	if ev is InputEventMouseMotion and Input.is_mouse_button_pressed(1):
		camrot.x -= ev.relative.x/2
		camrot.y = clamp(camrot.y + ev.relative.y, -80,80)

	if ev is InputEventMouseButton:
		if ev.button_index == BUTTON_WHEEL_UP:
			cam_radius = max(cam_radius - 0.5, 1);
		elif ev.button_index == BUTTON_WHEEL_DOWN:
			cam_radius = min(cam_radius + 0.5, 8);

func _physics_process(delta):
	
	camrotl = camrotl.linear_interpolate(camrot, 10 * delta)
	
	cam_radiusl = lerp(cam_radiusl, cam_radius, 5 * delta)
	
	var target = get_node("../character").translation
	target.y = get_node("../character").translation.y + 2

	cam_pos.x = target.x + cam_radiusl * sin(deg2rad(camrotl.x)) * cos(deg2rad(camrotl.y))
	cam_pos.y = target.y + cam_radiusl * sin(deg2rad(camrotl.y))
	cam_pos.z = target.z + cam_radiusl * cos(deg2rad(camrotl.x)) * cos(deg2rad(camrotl.y))

	self.set_translation(cam_pos)
	self.look_at_from_position(get_translation(), target, Vector3(0,1,0))
	
