// 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.StateMachines
{
    /// <summary>Uses player input to control a <see cref="Character"/>.</summary>
    /// 
    /// <remarks>
    /// <strong>Sample:</strong>
    /// <see href="https://kybernetik.com.au/animancer/docs/samples/fsm/brains">
    /// Brains</see>
    /// </remarks>
    /// 
    /// https://kybernetik.com.au/animancer/api/Animancer.Samples.StateMachines/MovingCharacterBrain
    /// 
    [AddComponentMenu(Strings.SamplesMenuPrefix + "Brains - Moving Character Brain")]
    [AnimancerHelpUrl(typeof(MovingCharacterBrain))]
    public class MovingCharacterBrain : MonoBehaviour
    {
        /************************************************************************************************************************/

        [SerializeField] private Character _Character;
        [SerializeField] private CharacterState _Move;
        [SerializeField] private CharacterState _Action;

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

        protected virtual void Update()
        {
            UpdateMovement();
            UpdateAction();
        }

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

        protected virtual void UpdateMovement()
        {
            Vector2 input = SampleInput.WASD;
            if (input != Vector2.zero)
            {
                // Get the camera's forward and right vectors and flatten them onto the XZ plane.
                Transform camera = Camera.main.transform;

                Vector3 forward = camera.forward;
                forward.y = 0;
                forward.Normalize();

                Vector3 right = camera.right;
                right.y = 0;
                right.Normalize();

                // Build the movement vector by multiplying the input by those axes.
                _Character.Parameters.MovementDirection =
                   right * input.x +
                   forward * input.y;

                // Enter the movement state if we aren't already in it.
                _Character.StateMachine.TrySetState(_Move);
            }
            else// If we aren't trying to move, clear the movement vector and return to idle.
            {
                _Character.Parameters.MovementDirection = Vector3.zero;
                _Character.StateMachine.TrySetDefaultState();
            }

            // Indicate whether the character wants to run or not.
            _Character.Parameters.WantsToRun = SampleInput.LeftShiftHold;
        }

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

        protected virtual void UpdateAction()
        {
            if (SampleInput.LeftMouseUp)
                _Character.StateMachine.TryResetState(_Action);
        }

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