Files
2026-06-22 16:18:34 +02:00

84 lines
2.3 KiB
C#

using UnityEngine;
using Ashwild.Inventory;
namespace Ashwild.Player
{
/// <summary>
/// Held-item logic for consumables, placed on the food/drink hand prefab. While
/// the prefab is equipped it listens to the use input and eats/drinks the item via
/// PlayerInventory.UseItem, which applies the restore effects, raises ItemConsumed
/// and removes one. No animation here — the FPS arm model plays its own.
/// </summary>
[DisallowMultipleComponent]
public class ConsumableBehaviour : MonoBehaviour, IHeldItemBehaviour
{
#region Serialized Fields
[Header("Cooldown")]
/// <summary>
/// Minimum time, in seconds, between two consumptions.
/// </summary>
[SerializeField] private float useCooldown = 0.5f;
#endregion
#region State
/// <summary>
/// Earliest time, in seconds, the next consumption is allowed.
/// </summary>
private float nextUseTime;
#endregion
#region Unity Lifecycle
/// <summary>
/// Subscribes to the use input for as long as this item is held.
/// </summary>
private void OnEnable()
{
PlayerEvents.AttackPressed += HandleAttackPressed;
}
/// <summary>
/// Unsubscribes when the item is put away (the prefab is destroyed).
/// </summary>
private void OnDisable()
{
PlayerEvents.AttackPressed -= HandleAttackPressed;
}
#endregion
#region IHeldItemBehaviour
/// <summary>
/// Nothing to link: consuming reads the selected hotbar slot directly. Present
/// to satisfy the held-item contract.
/// </summary>
public void Setup(HeldItemContext context, ItemData item)
{
}
#endregion
#region Event Handlers
/// <summary>
/// Consumes one unit on use input, gated by lock and cooldown.
/// </summary>
private void HandleAttackPressed()
{
if (PlayerEvents.InputLocked) return;
if (Time.time < nextUseTime) return;
if (PlayerInventory.Instance == null) return;
nextUseTime = Time.time + useCooldown;
PlayerInventory.Instance.UseItem(PlayerInventory.Instance.SelectedHotbarIndex);
}
#endregion
}
}