using System.Collections.Generic;
using UnityEngine;

public class CharacterVoiceObject : MonoBehaviour
{
    public CharacterData Character;
    public AudioSource VoiceSource;

    private static readonly Dictionary<CharacterData, CharacterVoiceObject> _registry = new();

    private void OnEnable()  => _registry[Character] = this;
    private void OnDisable() => _registry.Remove(Character);

    public static CharacterVoiceObject Find(CharacterData data)
        => _registry.TryGetValue(data, out var obj) ? obj : null;

    public void Play(AudioClip clip) => VoiceSource.PlayOneShot(clip);
}
