extends Camera3D

var held_dart: Dart = null

func _unhandled_input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			try_grab_dart(event.position)
		else:
			if held_dart:
				held_dart.release_drag()
				held_dart = null
	elif event is InputEventMouseMotion and held_dart:
		held_dart.update_drag(self, event.position)

func try_grab_dart(mouse_pos: Vector2):
	var ray_origin = project_ray_origin(mouse_pos)
	var ray_dir = project_ray_normal(mouse_pos)
	var ray_end = ray_origin + ray_dir * 1000.0

	var space_state = get_world_3d().direct_space_state
	var query = PhysicsRayQueryParameters3D.create(ray_origin, ray_end)
	var result = space_state.intersect_ray(query)

	if result and result.collider is Dart:
		held_dart = result.collider
		held_dart.start_drag(self, mouse_pos, result.position)
