extends Node3D

const RAMP_SPAWN_GROUP := "RampSpawn"
const DROP_SPAWN_GROUP := "DropSpawn"
const PUTT_SPAWN_GROUP := "PuttSpawn"

const PUTT_VEC := Vector3(0, 0, 2)

@export var ball_scene: PackedScene


func spawn_balls(spawn_group: String, initial_impulse: Vector3 = Vector3.ZERO) -> void:
	for spawn: Node3D in get_tree().get_nodes_in_group(spawn_group):
		var ball: GameBall = ball_scene.instantiate()
		var callback := func() -> void:
			if ball.sleeping:
				ball.queue_free()
		ball.sleeping_state_changed.connect(callback)
		spawn.add_sibling(ball)
		ball.global_transform = spawn.global_transform
		ball.freeze = false
		if initial_impulse:
			ball.apply_impulse(
				initial_impulse,
				(
					initial_impulse.normalized()
					* -1
					* ball.radius
					* ball.radius
					* ShotSetup.SHOT_OFFSET_Z_FACTOR
				)
			)


func _process(_delta: float) -> void:
	if Input.is_action_just_pressed("debug_1"):
		spawn_balls(RAMP_SPAWN_GROUP)
	if Input.is_action_just_pressed("debug_2"):
		spawn_balls(DROP_SPAWN_GROUP)
	if Input.is_action_just_pressed("debug_3"):
		spawn_balls(PUTT_SPAWN_GROUP, PUTT_VEC)
