@tool
extends Node3D

@export_tool_button("Clear collisions", "Callable") var clear_col_action = clear_collisions
@export_tool_button("Generate collisions", "Callable") var generate_col_action = generate_collisions

func get_all_children_of_type(node, type) -> Array:
	var nodes : Array = []
	for child in node.get_children():
		if is_instance_of(child, type):
			nodes.append(child)
		if child.get_child_count() > 0:
			nodes.append_array(get_all_children_of_type(child, type))
	return nodes

func clear_collisions_on_meshes(meshes: Array):
	for mesh_instance: MeshInstance3D in meshes:
		for child in mesh_instance.get_children():
			child.queue_free()

func generate_collisions_on_meshes(meshes: Array):
	for mesh_instance: MeshInstance3D in meshes:
		mesh_instance.create_trimesh_collision()

func generate_collisions():
	var mesh_instances = get_all_children_of_type(self, MeshInstance3D)
	clear_collisions_on_meshes(mesh_instances)
	generate_collisions_on_meshes(mesh_instances)

func clear_collisions():
	var mesh_instances = get_all_children_of_type(self, MeshInstance3D)
	clear_collisions_on_meshes(mesh_instances)
