extends MultiMeshInstance3D
var mm: MultiMesh
var parent_node: BushController
static var bush_mesh: Mesh = null
var material: ShaderMaterial

func _ready() -> void:
	parent_node = get_parent() as BushController
	if parent_node == null:
		Log.pr("Error: Parent node is not a BushController!")
	
	# Load mesh once and reuse
	if bush_mesh == null:
		bush_mesh = load("res://Entities/Bush/assets/bush_large.res")

func setup_multimesh() -> void:
	if parent_node == null:
		return
	
	if bush_mesh == null:
		Log.pr("Error: Could not load bush mesh")
		return
	
	# Reuse existing MultiMesh if possible, or create new one
	if mm == null:
		mm = MultiMesh.new()
		mm.transform_format = MultiMesh.TRANSFORM_3D
		mm.mesh = bush_mesh

		material = ColorData.get_random_bush_material(Season.current)
		mm.mesh.surface_set_material(0, material)
	
	# Configure instance count
	mm.instance_count = parent_node.bush_instance_range
	
	# Get shared RNG from GroundTile
	var rng = parent_node.parent_node.get_rng()
	
	# Generate positions using shared RNG
	for i in range(mm.instance_count):
		var random_pos = Vector3(
			rng.randf_range(-1.0, 1.0),
			0.0,
			rng.randf_range(-1.0, 1.0)
		)
		
		var random_rotation = rng.randf_range(0.0, TAU)
		var basis = Basis(Vector3.UP, random_rotation)
		
		var random_scale = rng.randf_range(0.3, 0.9)
		basis = basis.scaled(Vector3(random_scale, random_scale, random_scale))
		
		var tx = Transform3D(basis, random_pos)
		mm.set_instance_transform(i, tx)

	multimesh = mm
