Files

109 lines
3.3 KiB
C#
Raw Permalink Normal View History

2025-04-18 15:54:50 -04:00
using UnityEngine;
using Game;
using Music;
using Player;
2025-02-09 13:49:39 -05:00
using UnityEngine.InputSystem;
2025-04-18 15:54:50 -04:00
2025-04-16 19:57:54 -04:00
namespace Player
{
2025-04-18 15:54:50 -04:00
/// <summary>
/// This class handles the player's ability to block and parry incoming attacks.
/// Blocking reduces damage, while parrying reflects attacks if timed correctly.
/// </summary>
[RequireComponent(typeof(PlayerInput))]
public class Block : MonoBehaviour
{
/// <summary>
/// Indicates whether the player is currently blocking.
/// </summary>
public bool blocking = false;
2025-02-09 13:49:39 -05:00
2025-04-18 15:54:50 -04:00
/// <summary>
/// The input actions associated with the player.
/// </summary>
private InputActionAsset actions;
2025-02-09 13:49:39 -05:00
2025-04-18 15:54:50 -04:00
/// <summary>
/// The time when the block button was pressed.
/// </summary>
private float blockPressTime = 0f;
2025-02-09 13:49:39 -05:00
2025-04-18 15:54:50 -04:00
/// <summary>
/// The maximum time (in seconds) for a successful parry after pressing the block button.
/// </summary>
[SerializeField] private float parryThreshold = 0.2f;
/// <summary>
/// Indicates whether the player is currently parrying.
/// </summary>
private bool isParrying = false;
/// <summary>
/// Initializes the player's input actions.
/// </summary>
private void Start()
2025-02-09 13:49:39 -05:00
{
2025-04-18 15:54:50 -04:00
actions = GetComponent<PlayerInput>().actions;
2025-02-09 13:49:39 -05:00
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Updates the player's blocking and parrying state every frame based on input.
/// </summary>
private void Update()
2025-02-09 13:49:39 -05:00
{
2025-04-18 15:54:50 -04:00
// Get the block action from the input system
InputAction blockAction = actions.FindAction("Block");
// Check if the block button is being pressed
if (blockAction.ReadValue<float>() == 1f)
2025-02-26 20:17:19 -05:00
{
2025-04-18 15:54:50 -04:00
if (!blocking)
2025-02-26 20:17:19 -05:00
{
2025-04-18 15:54:50 -04:00
// Start the parry timer when the block button is first pressed
blockPressTime = Time.time;
2025-02-26 20:17:19 -05:00
}
2025-04-18 15:54:50 -04:00
blocking = true;
}
else
{
// Handle the release of the block button
if (blocking)
2025-02-26 20:17:19 -05:00
{
2025-04-18 15:54:50 -04:00
// 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;
}
2025-02-26 20:17:19 -05:00
}
2025-04-18 15:54:50 -04:00
blocking = false;
2025-02-26 20:17:19 -05:00
}
2025-04-18 15:54:50 -04:00
// Update the blocking state in the animation system
GetComponent<AnimationPlayer>().block = blocking;
2025-02-09 13:49:39 -05:00
}
2025-02-26 20:17:19 -05:00
2025-04-18 15:54:50 -04:00
/// <summary>
/// Activates the parry state, allowing the player to reflect attacks.
/// </summary>
private void Parry()
{
isParrying = true;
}
2025-02-26 20:17:19 -05:00
2025-04-18 15:54:50 -04:00
/// <summary>
/// Checks if the player is currently parrying.
/// </summary>
/// <returns>True if the player is parrying, false otherwise.</returns>
public bool IsParrying()
{
return isParrying;
}
2025-02-26 20:17:19 -05:00
}
2025-04-16 19:57:54 -04:00
}