// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //

#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.

using UnityEngine;

namespace Animancer.Samples.Events
{
    /// <summary>Uses Animancer Events to play a sound randomly selected from an array.</summary>
    /// 
    /// <remarks>
    /// <strong>Sample:</strong>
    /// <see href="https://kybernetik.com.au/animancer/docs/samples/events/footsteps">
    /// Footstep Events</see>
    /// </remarks>
    /// 
    /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Events/FootstepEvents
    /// 
    [AddComponentMenu(Strings.SamplesMenuPrefix + "Footstep Events - Footstep Events")]
    [AnimancerHelpUrl(typeof(FootstepEvents))]
    public class FootstepEvents : MonoBehaviour
    {
        /************************************************************************************************************************/

        [SerializeField] private AnimancerComponent _Animancer;
        [SerializeField] private ClipTransition _Walk;
        [SerializeField] private AudioClip[] _Sounds;

        /************************************************************************************************************************/

        protected virtual void OnEnable()
        {
            _Animancer.Play(_Walk);
        }

        /************************************************************************************************************************/

        // Called by Animancer Events.
        public void PlaySound(AudioSource source)
        {
            source.clip = _Sounds[Random.Range(0, _Sounds.Length)];
            source.Play();

            // Create a sphere on the foot to show where the sound is coming from.
            Transform sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
            sphere.parent = source.transform;
            sphere.localPosition = Vector3.zero;
            sphere.localScale = Vector3.one * 0.2f;
            Destroy(sphere.gameObject, 0.1f);// Destroy after 0.1 seconds.
        }

        /************************************************************************************************************************/
    }
}
