using UnityEngine;
using Game;
using Music;
using Player;
using UnityEngine.InputSystem;
namespace Player
{
///
/// This class handles the punching mechanic for the player, including triggering animations,
/// enabling and disabling the hurtbox, and managing player speed during a punch.
///
[RequireComponent(typeof(PlayerInput))]
[RequireComponent(typeof(AnimationPlayer))]
public class Punch : MonoBehaviour
{
///
/// Determines whether the player can cancel their punch action.
///
public bool cancelable = true;
///
/// The hurtbox used to detect collisions with other players or objects during a punch.
///
[SerializeField] private BoxCollider2D hurtbox;
///
/// The input actions associated with the player.
///
private InputActionAsset actions;
///
/// Initializes the player's input actions.
///
private void Start()
{
actions = GetComponent().actions;
}
///
/// Checks for punch input every frame and executes the punch if the action is triggered.
///
private void Update()
{
// Executes punch when the "Punch" action is pressed
if (actions.FindAction("Punch").WasPressedThisFrame())
{
if (!cancelable) return; // Prevents punching if the action is not cancelable
ExecutePunch();
}
}
///
/// Executes the punch action, triggering the punch animation and slowing the player down.
///
private void ExecutePunch()
{
// Trigger the punch animation
GetComponent().Punch();
// Disable the ability to cancel the punch
DisableCancellation();
// Temporarily reduce the player's movement speed during the punch
GetComponent().maxSpeedOverride = 1f;
}
///
/// Enables the hurtbox, allowing the punch to interact with other objects.
///
public void EnableHurtbox()
{
if (hurtbox != null) hurtbox.enabled = true;
}
///
/// Disables the hurtbox, preventing the punch from interacting with other objects.
///
public void DisableHurtbox()
{
if (hurtbox != null) hurtbox.enabled = false;
}
///
/// Disables the ability to cancel the punch action.
///
public void DisableCancellation()
{
cancelable = false;
}
///
/// Enables the ability to cancel the punch action.
///
public void EnableCancellation()
{
cancelable = true;
}
///
/// Resets the player's movement speed to its maximum value after the punch is complete.
///
public void ReturnToMaxSpeed()
{
GetComponent().maxSpeedOverride = GetComponent().maxSpeed;
}
}
}