extends Node3D

@export var amount: int = 2;
@export var offset: float = 2.0;
@export var shape_shader: Shader;

var nodes: Array[PathFollow3D];

@onready var path: Path3D = $Path
@onready var shape_material: ShaderMaterial = ShaderMaterial.new();
@onready var shapes: Array[PrimitiveMesh] = [BoxMesh.new(), PrismMesh.new(), TorusMesh.new(), CylinderMesh.new()];

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	$Camera3D.make_current();
	shape_material.set_shader(shape_shader);
	var path_length: float = (path as Path3D).get_curve().get_baked_length();
	
	for i in range(amount):
		var instance: PathFollow3D = PathFollow3D.new();
		var mesh_instance: MeshInstance3D = MeshInstance3D.new();
		mesh_instance.mesh = shapes.pick_random();
		mesh_instance.set_surface_override_material(0, shape_material);
		mesh_instance.set_instance_shader_parameter("id", i);
		instance.add_child(mesh_instance);
		
		path.add_child(instance);
		instance.set_progress_ratio(offset / 100 * i);
		
		nodes.push_back(instance);
		

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	for i in nodes.size():
		nodes[i].rotate(Vector3(1.0, 0.0, 0.0), 10 * delta);
	
