class_name BallPoint extends Node3D
## Ball spawn point & origin. I.E. the "tee".

## Emitted when a new ball is placed.
signal ball_changed(ball: GameBall)

## Scenes for each type of ball.
const SCENE_MAP: Dictionary = {
	GameBall.Type.BASIC: preload("res://src/equipment/balls/physics_ball/physics_ball.tscn"),
	GameBall.Type.PLASMA: preload("res://src/equipment/balls/plasma_ball/plasma_ball.tscn"),
}

@export var ball: GameBall:
	set(value):
		ball = value
		ball_changed.emit(ball)


## Get a new instance of a ball of the given type.
## Returns null if the type can't be instantiated (e.g. NONE type)
func get_instance(type: GameBall.Type) -> GameBall:
	if type in SCENE_MAP:
		var scene: PackedScene = SCENE_MAP.get(type)
		return scene.instantiate() as GameBall
	return null


## Clear any existing ball, instantiate a new one of the given type, and place it at the ball point.
func spawn_ball(type: GameBall.Type) -> void:
	# Clear existing ball
	if is_instance_valid(ball):
		ball.queue_free()

	ball = get_instance(type)
	if is_instance_valid(ball):
		add_child(ball)
		snap()


## Snap the ball back to the ball point.
func snap() -> void:
	ball.global_transform = global_transform
