using UnityEngine;
using Game;
using Music;
using Player;
using UnityEngine.InputSystem;
namespace Player
{
///
/// This class handles the player's ability to block and parry incoming attacks.
/// Blocking reduces damage, while parrying reflects attacks if timed correctly.
///
[RequireComponent(typeof(PlayerInput))]
public class Block : MonoBehaviour
{
///
/// Indicates whether the player is currently blocking.
///
public bool blocking = false;
///
/// The input actions associated with the player.
///
private InputActionAsset actions;
///
/// The time when the block button was pressed.
///
private float blockPressTime = 0f;
///
/// The maximum time (in seconds) for a successful parry after pressing the block button.
///
[SerializeField] private float parryThreshold = 0.2f;
///
/// Indicates whether the player is currently parrying.
///
private bool isParrying = false;
///
/// Initializes the player's input actions.
///
private void Start()
{
actions = GetComponent().actions;
}
///
/// Updates the player's blocking and parrying state every frame based on input.
///
private void Update()
{
// Get the block action from the input system
InputAction blockAction = actions.FindAction("Block");
// Check if the block button is being pressed
if (blockAction.ReadValue() == 1f)
{
if (!blocking)
{
// Start the parry timer when the block button is first pressed
blockPressTime = Time.time;
}
blocking = true;
}
else
{
// Handle the release of the block button
if (blocking)
{
// Calculate how long the block button was held
float pressDuration = Time.time - blockPressTime;
// If the button was released within the parry threshold, trigger a parry
if (pressDuration <= parryThreshold)
{
Parry();
}
else
{
isParrying = false;
}
}
blocking = false;
}
// Update the blocking state in the animation system
GetComponent().block = blocking;
}
///
/// Activates the parry state, allowing the player to reflect attacks.
///
private void Parry()
{
isParrying = true;
}
///
/// Checks if the player is currently parrying.
///
/// True if the player is parrying, false otherwise.
public bool IsParrying()
{
return isParrying;
}
}
}