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