# GrassController.gd
extends Node3D
class_name BushController

@onready var bush_multimesh: MultiMeshInstance3D = $BushMultimesh
var parent_node: GroundTile = null
var bush_density: float = 0.5
var bush_instance_range: int = 10

func _ready() -> void:
	parent_node = get_parent() as GroundTile

func spawn_bushes_for_cell(value):
	if value == null:
		return
	
	bush_density = value.vegetation_density
	update_bush_density()
	
	if bush_multimesh and bush_multimesh.has_method("setup_multimesh"):
		bush_multimesh.setup_multimesh()

func update_bush_density() -> void:
	if parent_node == null:
		return

	var rng = parent_node.get_rng()

	if bush_density > 0.8:
			bush_instance_range = rng.randi_range(5, 10)
	elif bush_density > 0.6:
			bush_instance_range = rng.randi_range(2, 7)
	elif bush_density > 0.3:
			bush_instance_range = rng.randi_range(0, 1)
	else:
			bush_instance_range = rng.randi_range(0, 0)
