# GrassController.gd
extends Node3D
class_name GrassController

@onready var grass_multimesh: MultiMeshInstance3D = $GrassMultimesh
var parent_node: GroundTile = null
var grass_density: float = 0.5
var grass_instance_range: int = 10

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

func spawn_grass_for_cell(value):
	if value == null:
		return
	
	grass_density = value.vegetation_density
	update_grass_density()
	
	if grass_multimesh and grass_multimesh.has_method("setup_multimesh"):
		grass_multimesh.setup_multimesh()

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

	var rng = parent_node.get_rng()

	if grass_density > 0.8:
			grass_instance_range = rng.randi_range(100, 500)
	elif grass_density > 0.6:
			grass_instance_range = rng.randi_range(30, 50)
	elif grass_density > 0.3:
			grass_instance_range = rng.randi_range(5, 20)
	else:
			grass_instance_range = rng.randi_range(0, 1)
