feat(shop): add box pickup/carry, pour-into-shelf, and package flight visuals
- Carryable: generic pickup/carry component, cosmetic per-peer follow of the carrier's HandSocket instead of NGO reparenting (camera pitch isn't networked, so root-level reparenting wouldn't tilt with the view) - PlayerCarry: mutual exclusion with PlayerHands, server-timed hold-to-pour tick (client only signals start/stop, never per-unit requests) - IPourSource/IPourTarget: decouple PlayerInteractor from concrete Shop types, same pattern as IInteractable - Lieferkarton: gains product data (productId/quantity), two-stage Interact() (open, then pick up), IPourSource - ShelfSlot: implements IPourTarget, fills the previous RefreshVisuals TODO - PackageDisplay: renders quantity as a proper column x row grid that stacks into further layers as space runs out, reused by both containers - FlyingPackage: small non-networked cosmetic visual that arcs a package from the carried box to its exact landing slot in the shelf grid on every successful pour tick, broadcast via Rpc(SendTo.Everyone) - StorageCrate: rejects pickup while the player is carrying a box Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
using AfterHours.Data;
|
||||
using AfterHours.Interaction;
|
||||
using AfterHours.Networking;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AfterHours.Shop
|
||||
{
|
||||
[RequireComponent(typeof(NetworkObject))]
|
||||
public class Lieferkarton : NetworkBehaviour, IInteractable
|
||||
[RequireComponent(typeof(Carryable))]
|
||||
public class Lieferkarton : NetworkBehaviour, IInteractable, IPourSource
|
||||
{
|
||||
[Header("Deckel Objekte")]
|
||||
[SerializeField] private Transform deckelLinks;
|
||||
@@ -15,6 +18,18 @@ namespace AfterHours.Shop
|
||||
[SerializeField] private float oeffnungsWinkel = 110f; // Winkel in Grad nach außen
|
||||
[SerializeField] private float geschwindigkeit = 5f; // Geschwindigkeit des Aufklappens
|
||||
|
||||
[Header("Inhalt")]
|
||||
[SerializeField] private ProductCatalog _catalog;
|
||||
[SerializeField] private int _productId = -1;
|
||||
[SerializeField] private int _startingQuantity = 20;
|
||||
[SerializeField] private Transform _contentsRoot;
|
||||
[SerializeField] private int _maxVisiblePackages = 12;
|
||||
|
||||
[Header("Anzeige-Raster (sauber gestapelt, so wie Platz ist)")]
|
||||
[SerializeField] private int _displayColumns = 3;
|
||||
[SerializeField] private int _displayRows = 3;
|
||||
[SerializeField] private Vector3 _displaySpacing = new(0.12f, 0.1f, 0.12f);
|
||||
|
||||
// Eine synchronisierte Variable, die den Zustand des Kartons speichert
|
||||
private NetworkVariable<bool> istOffen = new NetworkVariable<bool>(
|
||||
false,
|
||||
@@ -22,14 +37,31 @@ namespace AfterHours.Shop
|
||||
NetworkVariableWritePermission.Server // Nur der Server darf den Zustand ändern
|
||||
);
|
||||
|
||||
private readonly NetworkVariable<int> _quantity = new(
|
||||
0,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Server
|
||||
);
|
||||
|
||||
public bool IsOpen => istOffen.Value;
|
||||
public int ProductId => _productId;
|
||||
public int Quantity => _quantity.Value;
|
||||
public bool IsEmpty => _quantity.Value <= 0;
|
||||
|
||||
// Interne Variablen zum Speichern der Ziel-Rotationen
|
||||
private Quaternion linksGeschlossen;
|
||||
private Quaternion linksOffen;
|
||||
private Quaternion rechtsGeschlossen;
|
||||
private Quaternion rechtsOffen;
|
||||
|
||||
private Carryable _carryable;
|
||||
private PackageDisplay _packageDisplay;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_carryable = GetComponent<Carryable>();
|
||||
_packageDisplay = new PackageDisplay(_contentsRoot, _displayColumns, _displayRows, _displaySpacing);
|
||||
|
||||
// Start-Rotationen (Geschlossen) direkt beim Laden sichern
|
||||
if (deckelLinks != null) linksGeschlossen = deckelLinks.localRotation;
|
||||
if (deckelRechts != null) rechtsGeschlossen = deckelRechts.localRotation;
|
||||
@@ -39,6 +71,15 @@ namespace AfterHours.Shop
|
||||
rechtsOffen = rechtsGeschlossen * Quaternion.Euler(oeffnungsWinkel, 0, 0);
|
||||
}
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
if (IsServer) _quantity.Value = _startingQuantity;
|
||||
|
||||
_quantity.OnValueChanged += (_, _) => RefreshVisuals();
|
||||
istOffen.OnValueChanged += (_, _) => RefreshVisuals();
|
||||
RefreshVisuals();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Beide Deckel weich auf jedem Client rotieren lassen basierend auf der NetworkVariable
|
||||
@@ -55,23 +96,53 @@ namespace AfterHours.Shop
|
||||
}
|
||||
}
|
||||
|
||||
// Wird von deinem Interaktionssystem aufgerufen (z. B. wenn "E" gedrückt wird)
|
||||
/// <summary>
|
||||
/// Wird von deinem Interaktionssystem aufgerufen (z. B. wenn "E" gedrückt wird).
|
||||
/// Geschlossen -> öffnen. Offen und nicht getragen -> aufheben.
|
||||
/// </summary>
|
||||
public void Interact(NetworkObject actor)
|
||||
{
|
||||
ToggleKartonServerRpc();
|
||||
if (!IsServer) return;
|
||||
|
||||
if (!istOffen.Value)
|
||||
{
|
||||
istOffen.Value = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_carryable.IsCarried && actor.TryGetComponent<PlayerCarry>(out var carry))
|
||||
carry.TryPickUp(_carryable);
|
||||
}
|
||||
|
||||
// Der Server verarbeitet den Aufruf und synchronisiert den Zustand im Netzwerk
|
||||
[Rpc(SendTo.Server)]
|
||||
private void ToggleKartonServerRpc()
|
||||
/// <summary>Server: nimmt ein Stück Ware aus dem Karton. Für das Ausschütten (siehe PlayerCarry).</summary>
|
||||
public bool TryTakeOne()
|
||||
{
|
||||
istOffen.Value = !istOffen.Value;
|
||||
if (!IsServer || IsEmpty) return false;
|
||||
_quantity.Value--;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Gibt den dynamischen Text für das UI-Prompt zurück
|
||||
public string GetPrompt(ulong clientId)
|
||||
{
|
||||
return istOffen.Value ? "Karton schließen" : "Karton öffnen";
|
||||
if (!istOffen.Value) return "Karton öffnen";
|
||||
|
||||
var product = _catalog != null ? _catalog.Get(_productId) : null;
|
||||
string name = product != null ? product.DisplayName : "?";
|
||||
return IsEmpty ? $"{name} — leer" : $"{name} aufheben ({_quantity.Value} Stk.)";
|
||||
}
|
||||
|
||||
/// <summary>Verpackungs-Prefab des aktuellen Produkts (auch für das Flug-Visual beim Ausschütten, siehe PlayerCarry).</summary>
|
||||
public GameObject GetPackagePrefab()
|
||||
{
|
||||
var product = _catalog != null ? _catalog.Get(_productId) : null;
|
||||
return product != null ? product.PackagePrefab : null;
|
||||
}
|
||||
|
||||
private void RefreshVisuals()
|
||||
{
|
||||
int visibleCount = istOffen.Value ? _quantity.Value : 0;
|
||||
_packageDisplay.SetTarget(GetPackagePrefab(), visibleCount, _maxVisiblePackages);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user