using UnityEngine; using Ashwild.Inventory; namespace Ashwild.Player { /// /// 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. /// [DisallowMultipleComponent] public class ConsumableBehaviour : MonoBehaviour, IHeldItemBehaviour { #region Serialized Fields [Header("Cooldown")] /// /// Minimum time, in seconds, between two consumptions. /// [SerializeField] private float useCooldown = 0.5f; #endregion #region State /// /// Earliest time, in seconds, the next consumption is allowed. /// private float nextUseTime; #endregion #region Unity Lifecycle /// /// Subscribes to the use input for as long as this item is held. /// private void OnEnable() { PlayerEvents.AttackPressed += HandleAttackPressed; } /// /// Unsubscribes when the item is put away (the prefab is destroyed). /// private void OnDisable() { PlayerEvents.AttackPressed -= HandleAttackPressed; } #endregion #region IHeldItemBehaviour /// /// Nothing to link: consuming reads the selected hotbar slot directly. Present /// to satisfy the held-item contract. /// public void Setup(HeldItemContext context, ItemData item) { } #endregion #region Event Handlers /// /// Consumes one unit on use input, gated by lock and cooldown. /// 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 } }