(Feat) Add Remove Building

This commit is contained in:
2026-07-09 21:38:14 +02:00
parent 24b15d23a3
commit f5de33eac2
13 changed files with 520 additions and 242 deletions
@@ -25,6 +25,8 @@ namespace Ashwild.Player
public static bool IsBuildMenuOpen { get; private set; }
// True while a build ghost is being positioned in the world (menu closed, no structure placed yet).
public static bool IsPlacingBuild { get; private set; }
// True while the build hammer is in demolition mode (right-click held, menu closed, not placing).
public static bool IsDemolishing { get; private set; }
public static bool IsPaused { get; private set; }
// True when a networked session is live (host or client).
@@ -52,11 +54,13 @@ namespace Ashwild.Player
public static event Action CrouchPressed;
public static event Action AttackPressed;
public static event Action SecondaryUsePressed; // right-click / secondary use (build menu, aim, block, ...)
public static event Action<bool> SecondaryUseHeld; // right-click pressed (true) / released (false) — for tap-vs-hold gestures
public static event Action InteractPressed;
public static event Action DropPressed;
public static event Action InventoryTogglePressed;
public static event Action<int> HotbarSlotPressed;
public static event Action<float> HotbarScroll;
public static event Action BuildRotatePressed; // rotate the build ghost one step (R key) while placing
public static event Action CancelPressed; // UI "Cancel" (Escape) — back / pause / close
// ============================================================
@@ -128,6 +132,7 @@ namespace Ashwild.Player
public static event Action BuildMenuToggleRequested; // the build hammer asks to open/close the construction menu
public static event Action<bool> BuildMenuOpenChanged; // the construction menu opened (true) or closed (false)
public static event Action<bool> BuildPlacingChanged; // a build ghost started (true) / stopped (false) being positioned
public static event Action<bool> DemolishModeChanged; // the hammer entered (true) / left (false) demolition mode (right-click held)
// ============================================================
// Cooking events
@@ -182,11 +187,13 @@ namespace Ashwild.Player
public static void RaiseCrouchPressed() { Log(nameof(CrouchPressed)); CrouchPressed?.Invoke(); }
public static void RaiseAttackPressed() { Log(nameof(AttackPressed)); AttackPressed?.Invoke(); }
public static void RaiseSecondaryUsePressed() { Log(nameof(SecondaryUsePressed)); SecondaryUsePressed?.Invoke(); }
public static void RaiseSecondaryUseHeld(bool held) { Log(nameof(SecondaryUseHeld)); SecondaryUseHeld?.Invoke(held); }
public static void RaiseInteractPressed() { Log(nameof(InteractPressed)); InteractPressed?.Invoke(); }
public static void RaiseDropPressed() { Log(nameof(DropPressed)); DropPressed?.Invoke(); }
public static void RaiseInventoryTogglePressed() { Log(nameof(InventoryTogglePressed)); InventoryTogglePressed?.Invoke(); }
public static void RaiseHotbarSlotPressed(int i) { Log(nameof(HotbarSlotPressed)); HotbarSlotPressed?.Invoke(i); }
public static void RaiseHotbarScroll(float f) { HotbarScroll?.Invoke(f); }
public static void RaiseBuildRotatePressed() { Log(nameof(BuildRotatePressed)); BuildRotatePressed?.Invoke(); }
public static void RaiseCancelPressed() { Log(nameof(CancelPressed)); CancelPressed?.Invoke(); }
public static void RaiseGroundedChanged(bool grounded, float impactSpeed)
@@ -294,6 +301,17 @@ namespace Ashwild.Player
BuildPlacingChanged?.Invoke(placing);
}
/// <summary>
/// Toggles demolition mode (right-click held with the hammer out). Owner-side only; the
/// hammer raises it and BuildManager drives the world-facing targeting/destroy from it.
/// </summary>
public static void RaiseDemolishModeChanged(bool on)
{
IsDemolishing = on;
Log(nameof(DemolishModeChanged));
DemolishModeChanged?.Invoke(on);
}
public static void RaiseFoodPlacedToCook(ItemData raw) { Log(nameof(FoodPlacedToCook)); FoodPlacedToCook?.Invoke(raw); }
public static void RaiseFoodCookedReady(ItemData cooked) { Log(nameof(FoodCookedReady)); FoodCookedReady?.Invoke(cooked); }
public static void RaiseFuelAdded(ItemData fuel) { Log(nameof(FuelAdded)); FuelAdded?.Invoke(fuel); }
@@ -376,6 +394,7 @@ namespace Ashwild.Player
IsChestOpen = false;
IsBuildMenuOpen = false;
IsPlacingBuild = false;
IsDemolishing = false;
IsPaused = false;
HoveredInteractable = null;
}
@@ -4,21 +4,28 @@ 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.
/// Held-item logic for the build hammer, placed on its hand prefab. It turns the single
/// right-click into two gestures without any extra keybind: a quick <b>tap</b> opens/closes the
/// construction menu through the bus, while <b>holding</b> right-click past a short threshold puts
/// the hammer into demolition mode for as long as it is held (releasing leaves it). The hammer
/// stays a pure input source — it only raises bus requests; BuildManager owns the menu and the
/// world-facing targeting/destroy, so this never reaches for either.
/// </summary>
[DisallowMultipleComponent]
public class BuildHammerBehaviour : MonoBehaviour, IHeldItemBehaviour
{
#region Serialized Fields
[Header("Cooldown")]
[Header("Gestures")]
/// <summary>
/// Minimum time, in seconds, between two menu toggles so a held right-click
/// does not flicker the menu open and shut.
/// How long, in seconds, right-click must stay held before it counts as a demolition hold
/// instead of a menu tap. A release before this opens/closes the menu.
/// </summary>
[SerializeField] private float holdThreshold = 0.2f;
/// <summary>
/// Minimum time, in seconds, between two menu toggles so a burst of taps does not flicker the
/// menu open and shut.
/// </summary>
[SerializeField] private float toggleCooldown = 0.25f;
@@ -31,24 +38,58 @@ namespace Ashwild.Player
/// </summary>
private float nextToggleTime;
/// <summary>
/// True between an accepted press and its release. A press ignored at the gate (input locked
/// or already placing) never sets this, so its release is ignored too.
/// </summary>
private bool secondaryDown;
/// <summary>
/// Time, in seconds, the current accepted press began — measured against <see cref="holdThreshold"/>.
/// </summary>
private float secondaryDownTime;
/// <summary>
/// True while the hold has crossed into demolition mode, so the release ends the mode rather
/// than toggling the menu.
/// </summary>
private bool demoActive;
#endregion
#region Unity Lifecycle
/// <summary>
/// Subscribes to the secondary-use input for as long as the hammer is held.
/// Subscribes to the right-click hold stream for as long as the hammer is held.
/// </summary>
private void OnEnable()
{
PlayerEvents.SecondaryUsePressed += HandleSecondaryUse;
PlayerEvents.SecondaryUseHeld += HandleSecondaryUse;
}
/// <summary>
/// Unsubscribes when the hammer is put away (the prefab is destroyed).
/// Unsubscribes and leaves demolition mode cleanly when the hammer is put away (its prefab is
/// destroyed mid-hold), so the mode never sticks on with no hammer to release it.
/// </summary>
private void OnDisable()
{
PlayerEvents.SecondaryUsePressed -= HandleSecondaryUse;
PlayerEvents.SecondaryUseHeld -= HandleSecondaryUse;
if (demoActive) ExitDemolition();
secondaryDown = false;
}
/// <summary>
/// Promotes a sustained press into demolition mode once it outlasts the tap threshold. Held
/// out of Update (not a timer) so the mode drops instantly if input locks or a placement
/// begins mid-hold.
/// </summary>
private void Update()
{
if (!secondaryDown || demoActive) return;
if (PlayerEvents.InputLocked || PlayerEvents.IsPlacingBuild) return;
if (Time.time - secondaryDownTime < holdThreshold) return;
EnterDemolition();
}
#endregion
@@ -56,8 +97,8 @@ namespace Ashwild.Player
#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.
/// Nothing to link: the hammer only raises bus requests. Present to satisfy the held-item
/// contract; BuildManager holds the player/world refs the build UX needs.
/// </summary>
public void Setup(HeldItemContext context, ItemData item)
{
@@ -68,20 +109,60 @@ namespace Ashwild.Player
#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.
/// The single right-click gesture, split by press and release: a press is tracked (unless
/// input is locked or a ghost is being placed, where right-click means "cancel" and is left to
/// BuildManager); its release either ends demolition mode (if the hold engaged it) or, for a
/// quick tap, toggles the construction menu under the cooldown.
/// </summary>
private void HandleSecondaryUse()
private void HandleSecondaryUse(bool held)
{
if (PlayerEvents.InputLocked) return;
if (PlayerEvents.IsPlacingBuild) return;
if (Time.time < nextToggleTime) return;
if (held)
{
if (PlayerEvents.InputLocked || PlayerEvents.IsPlacingBuild) return;
secondaryDown = true;
secondaryDownTime = Time.time;
return;
}
if (!secondaryDown) return;
secondaryDown = false;
if (demoActive) ExitDemolition();
else ToggleMenu();
}
#endregion
#region Internal Helpers
/// <summary>
/// Requests the construction menu to open/close, gated by the toggle cooldown.
/// </summary>
private void ToggleMenu()
{
if (Time.time < nextToggleTime) return;
nextToggleTime = Time.time + toggleCooldown;
PlayerEvents.RaiseBuildMenuToggleRequested();
}
/// <summary>
/// Enters demolition mode and announces it on the bus.
/// </summary>
private void EnterDemolition()
{
demoActive = true;
PlayerEvents.RaiseDemolishModeChanged(true);
}
/// <summary>
/// Leaves demolition mode and announces it on the bus.
/// </summary>
private void ExitDemolition()
{
demoActive = false;
PlayerEvents.RaiseDemolishModeChanged(false);
}
#endregion
}
}
@@ -200,10 +200,12 @@ namespace Ashwild.Player
WireButton(playerMap, "Crouch", PlayerEvents.RaiseCrouchPressed);
WireButton(playerMap, "Attack", PlayerEvents.RaiseAttackPressed);
WireButton(playerMap, "SecondaryUse", PlayerEvents.RaiseSecondaryUsePressed);
WireHold(playerMap, "SecondaryUse", PlayerEvents.RaiseSecondaryUseHeld);
WireButton(playerMap, "Interact", PlayerEvents.RaiseInteractPressed);
WireButton(playerMap, "Drop", PlayerEvents.RaiseDropPressed);
WireButton(playerMap, "Inventory", PlayerEvents.RaiseInventoryTogglePressed);
WireScrollY(playerMap, "HotbarScroll", PlayerEvents.RaiseHotbarScroll);
WireButton(playerMap, "BuildRotate", PlayerEvents.RaiseBuildRotatePressed);
for (int i = 0; i < 10; i++)
{