using UnityEngine; using Game; using Music; using Player; namespace Player { /// /// This class manages the player's animations, including setting animation states, /// handling directional changes, and triggering specific animations like punching. /// [RequireComponent(typeof(Animator))] public class AnimationPlayer : MonoBehaviour { /// /// Represents the different animation states the player can be in. /// public enum AnimationState { /// /// The idle state, when the player is not moving. /// Idle, /// /// The running state, when the player is moving quickly. /// Run, /// /// The jumping state, when the player is in the air. /// Jump, /// /// The walking state, when the player is moving slowly. /// Walk } /// /// The current animation state of the player. /// public AnimationState state; /// /// Indicates whether the player is facing backwards. /// public bool backwards; /// /// Indicates whether the player is currently blocking. /// public bool block = false; /// /// The animation clip to play when the script starts. /// public AnimationClip clip; /// /// Reference to the Animator component that controls the player's animations. /// private Animator animator; /// /// Initializes the Animator component and plays the specified animation clip. /// private void Start() { animator = GetComponent(); // Play the initial animation clip if (clip != null) { animator.Play(clip.name); } } /// /// Updates the player's animation state and direction every frame. /// private void LateUpdate() { // Set the animation state in the Animator animator.SetInteger("state", (int)state); // Adjust the player's scale to reflect their facing direction transform.localScale = new Vector3( Mathf.Sign(backwards ? -1 : 1) * Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z ); // Update the blocking state in the Animator animator.SetBool("block", block); } /// /// Sets the player's animation state. /// /// The new animation state to set. public void SetState(AnimationState state) { this.state = state; } /// /// Triggers the punch animation. /// public void Punch() { animator.SetTrigger("punch"); } } }