// 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 Animancer.Units;
using UnityEngine;

namespace Animancer.Samples.Sprites
{
    /// <summary>
    /// A more complex version of the <see cref="DirectionalBasics"/> which adds
    /// running and pushing animations as well as the ability to actually move around.
    /// </summary>
    /// 
    /// <remarks>
    /// <strong>Sample:</strong>
    /// <see href="https://kybernetik.com.au/animancer/docs/samples/sprites/character">
    /// Directional Character</see>
    /// </remarks>
    /// 
    /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Sprites/DirectionalCharacter
    /// 
    [AddComponentMenu(Strings.SamplesMenuPrefix + "Sprites - Directional Character")]
    [AnimancerHelpUrl(typeof(DirectionalCharacter))]
    public class DirectionalCharacter : MonoBehaviour
    {
        /************************************************************************************************************************/

        [Header("Physics")]
        [SerializeField] private CapsuleCollider2D _Collider;
        [SerializeField] private Rigidbody2D _Rigidbody;
        [SerializeField, MetersPerSecond] private float _WalkSpeed = 1;
        [SerializeField, MetersPerSecond] private float _RunSpeed = 2;

        [Header("Animations")]
        [SerializeField] private AnimancerComponent _Animancer;
        [SerializeField] private DirectionalAnimationSet _Idle;
        [SerializeField] private DirectionalAnimationSet _Walk;
        [SerializeField] private DirectionalAnimationSet _Run;
        [SerializeField] private DirectionalAnimationSet _Push;
        [SerializeField] private Vector2 _Facing = Vector2.down;

        private Vector2 _Movement;
        private DirectionalAnimationSet _CurrentAnimationSet;

        private readonly TimeSynchronizer<AnimationGroup>
            TimeSynchronizer = new();

        public enum AnimationGroup
        {
            Other,
            Movement,
        }

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

        protected virtual void Update()
        {
            _Movement = SampleInput.WASD;
            if (_Movement != Vector2.zero)
            {
                _Facing = _Movement;
                UpdateMovementState();

                // Snap the movement to the exact directions we have animations for.
                // When using DirectionalAnimationSets this means the character will only move up/right/down/left.
                // But DirectionalAnimationSet8s will allow diagonal movement as well.
                _Movement = _CurrentAnimationSet.Snap(_Movement);
                _Movement = Vector2.ClampMagnitude(_Movement, 1);
            }
            else
            {
                Play(_Idle, AnimationGroup.Other);
            }
        }

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

        private void Play(DirectionalAnimationSet animations, AnimationGroup group)
        {
            // Store the current time.
            TimeSynchronizer.StoreTime(_Animancer);

            _CurrentAnimationSet = animations;
            _Animancer.Play(animations.GetClip(_Facing));

            // If the new animation is in the synchronization group, give it the same time the previous animation had.
            TimeSynchronizer.SyncTime(_Animancer, group);
        }

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

        // Pre-allocate an array of contact points so Unity doesn't need to allocate a new one every time we call
        // _Collider.GetContacts. This sample will never have more than 4 contact points, but you might consider a
        // higher number in a real game. Even a large number like 64 would be better than making new ones every time.
        private static readonly ContactPoint2D[] Contacts = new ContactPoint2D[4];

        protected virtual void UpdateMovementState()
        {
            int contactCount = _Collider.GetContacts(Contacts);
            for (int i = 0; i < contactCount; i++)
            {
                // If we are moving directly towards an object (or within 30 degrees of it), we are pushing it.
                if (Vector2.Angle(Contacts[i].normal, _Movement) > 180 - 30)
                {
                    Play(_Push, AnimationGroup.Movement);
                    return;
                }
            }

            bool isRunning = SampleInput.LeftShiftHold;
            Play(isRunning ? _Run : _Walk, AnimationGroup.Movement);
        }

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

        protected virtual void FixedUpdate()
        {
            // Determine the desired speed based on the current animation.
            float speed = _CurrentAnimationSet == _Run ? _RunSpeed : _WalkSpeed;
            _Rigidbody.velocity = _Movement * speed;
        }

        /************************************************************************************************************************/
#if UNITY_EDITOR
        /************************************************************************************************************************/

        /// <summary>[Editor-Only]
        /// Sets the character's starting sprite in Edit Mode so you can see it while working in the scene.
        /// </summary>
        /// <remarks>Called in Edit Mode whenever this script is loaded or a value is changed in the Inspector.</remarks>
        protected virtual void OnValidate()
        {
            if (_Idle != null)
                _Idle.GetClip(_Facing).EditModePlay(_Animancer);
        }

        /************************************************************************************************************************/
#endif
        /************************************************************************************************************************/
    }
}
