Files
2026-07-07 16:43:51 +02:00

88 lines
2.7 KiB
C#

using UnityEngine;
using Ashwild.Inventory;
namespace Ashwild.Player
{
/// <summary>
/// Held-item logic for the build hammer, placed on its hand prefab. While equipped it
/// listens to the secondary-use input (right-click) and asks the construction UI to
/// open/close through the bus — it never reaches for the menu itself, so the hammer
/// stays a pure input source and the UI owns its own state. Left-click placement will
/// be added on top of this once the build menu exists.
/// </summary>
[DisallowMultipleComponent]
public class BuildHammerBehaviour : MonoBehaviour, IHeldItemBehaviour
{
#region Serialized Fields
[Header("Cooldown")]
/// <summary>
/// Minimum time, in seconds, between two menu toggles so a held right-click
/// does not flicker the menu open and shut.
/// </summary>
[SerializeField] private float toggleCooldown = 0.25f;
#endregion
#region State
/// <summary>
/// Earliest time, in seconds, the next menu toggle is allowed.
/// </summary>
private float nextToggleTime;
#endregion
#region Unity Lifecycle
/// <summary>
/// Subscribes to the secondary-use input for as long as the hammer is held.
/// </summary>
private void OnEnable()
{
PlayerEvents.SecondaryUsePressed += HandleSecondaryUse;
}
/// <summary>
/// Unsubscribes when the hammer is put away (the prefab is destroyed).
/// </summary>
private void OnDisable()
{
PlayerEvents.SecondaryUsePressed -= HandleSecondaryUse;
}
#endregion
#region IHeldItemBehaviour
/// <summary>
/// Nothing to link yet: toggling the menu needs no player refs. Present to satisfy
/// the held-item contract; placement logic added later will use the context.
/// </summary>
public void Setup(HeldItemContext context, ItemData item)
{
}
#endregion
#region Event Handlers
/// <summary>
/// Requests the construction menu to open/close on right-click, gated by lock and
/// cooldown. While a ghost is being positioned, right-click cancels the placement instead
/// (handled by the placement controller), so the menu is left alone here.
/// </summary>
private void HandleSecondaryUse()
{
if (PlayerEvents.InputLocked) return;
if (PlayerEvents.IsPlacingBuild) return;
if (Time.time < nextToggleTime) return;
nextToggleTime = Time.time + toggleCooldown;
PlayerEvents.RaiseBuildMenuToggleRequested();
}
#endregion
}
}