# A target that can be hit by gpu particles. the ProxyCollisionDetector 
# does the actual detection. Expects a MeshInstance3d child as a visual
# and an AudioStreamPlayer3d to vary the volume based on the number of
# particles hitting it. As well as a CollisionShape3d for the collision

class_name Target
# this is kind of silly, but I'd like to use the actual CollisionShape3d
# node for the collision shape since it has an editor gizmo thing already built.
# this doesn't really interact with the real cpu physics engine though.
extends StaticBody3D

signal occupancy_changed(value: float)

@export var lerp_speed: float = 5.0
@onready var mesh: MeshInstance3D = $MeshInstance3D
@onready var audio: AudioStreamPlayer3D = $AudioStreamPlayer3D

var _target_occupancy: float = 0.0
var _current_occupancy: float = 0.0

func _ready() -> void:
	pass

func _process(delta: float) -> void:
	_current_occupancy = lerp(_current_occupancy, _target_occupancy, lerp_speed * delta)
	
	var material = mesh.get_surface_override_material(0)
	if material:
		material.emission_energy_multiplier = _current_occupancy * 10
	if audio:
		audio.volume_db = linear_to_db(_current_occupancy)
	
	occupancy_changed.emit(_current_occupancy)

# called from the ProxyCollisionDetector
func set_occupancy(occupancy: float) -> void:
	_target_occupancy = occupancy
