class_name ObstacleSpawner
extends Spawner

@export var obstacles: Array[PackedScene] = []
@export_range(70, 2000, 1, "or_greater") var min_spawn_distance: int = 128
@export_range(70, 2000, 1, "or_greater") var max_spawn_distance: int = 512

var _distance_left: int = 0


func _ready() -> void:
	RideManager.get_instance().distance_changed.connect(_on_distance_changed)
	set_random_distance()


func _on_distance_changed(pixels: int) -> void:
	_distance_left -= pixels
	if _distance_left <= 0:
		spawn_obstacle()
		set_random_distance()


func set_random_distance() -> void:
	_distance_left = randi_range(min_spawn_distance, max_spawn_distance)


func spawn_obstacle() -> void:
	spawning_object = obstacles[randi() % obstacles.size()]
	spawn()
