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:
@@ -0,0 +1,54 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace AfterHours.Shop
|
||||
{
|
||||
/// <summary>
|
||||
/// Rein kosmetisches, nicht genetzwertes Flug-Visual für ein Stück Ware,
|
||||
/// das vom Karton ins Regal wandert. Jeder Peer erzeugt seine eigene
|
||||
/// Instanz (ausgelöst über PlayerCarry.PlayFlightRpc) – keine
|
||||
/// Server-Autorität nötig, der eigentliche Bestand ist längst per
|
||||
/// NetworkVariable synchron.
|
||||
/// </summary>
|
||||
public sealed class FlyingPackage : MonoBehaviour
|
||||
{
|
||||
private const float ArcHeight = 0.35f;
|
||||
|
||||
private Vector3 _start;
|
||||
private Vector3 _end;
|
||||
private Quaternion _startRotation;
|
||||
private Quaternion _endRotation;
|
||||
private float _duration;
|
||||
private float _elapsed;
|
||||
|
||||
public static void Spawn(GameObject prefab, Vector3 start, Vector3 end, Quaternion endRotation, float duration)
|
||||
{
|
||||
if (prefab == null) return;
|
||||
|
||||
var instance = Instantiate(prefab, start, Quaternion.identity);
|
||||
var flight = instance.AddComponent<FlyingPackage>();
|
||||
flight.Init(start, end, endRotation, duration);
|
||||
}
|
||||
|
||||
private void Init(Vector3 start, Vector3 end, Quaternion endRotation, float duration)
|
||||
{
|
||||
_start = start;
|
||||
_end = end;
|
||||
_startRotation = transform.rotation;
|
||||
_endRotation = endRotation;
|
||||
_duration = Mathf.Max(0.05f, duration);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_elapsed += Time.deltaTime;
|
||||
float t = Mathf.Clamp01(_elapsed / _duration);
|
||||
|
||||
Vector3 flat = Vector3.Lerp(_start, _end, t);
|
||||
float arc = ArcHeight * Mathf.Sin(t * Mathf.PI);
|
||||
transform.position = flat + Vector3.up * arc;
|
||||
transform.rotation = Quaternion.Slerp(_startRotation, _endRotation, t);
|
||||
|
||||
if (t >= 1f) Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3084ee344a724f9458cc369f42643325
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AfterHours.Shop
|
||||
{
|
||||
/// <summary>
|
||||
/// Zeigt eine Stückzahl als einzelne Verpackungs-Prefabs unter einem
|
||||
/// Wurzel-Transform an (Regalfach, Lieferkarton) – ordentlich in einem
|
||||
/// Raster (Spalten x Reihen) gestapelt, weitere Lagen stapeln sich nach
|
||||
/// oben. Rein kosmetisch, läuft auf jedem Peer. Baut bei Mengenänderung
|
||||
/// nur das Delta neu, nicht die komplette Anzeige – wichtig beim
|
||||
/// Stück-für-Stück-Ausschütten.
|
||||
/// </summary>
|
||||
public sealed class PackageDisplay
|
||||
{
|
||||
private readonly Transform _root;
|
||||
private readonly int _columns;
|
||||
private readonly int _rows;
|
||||
private readonly Vector3 _spacing;
|
||||
private readonly List<GameObject> _instances = new();
|
||||
private GameObject _prefab;
|
||||
|
||||
public PackageDisplay(Transform root, int columns, int rows, Vector3 spacing)
|
||||
{
|
||||
_root = root;
|
||||
_columns = Mathf.Max(1, columns);
|
||||
_rows = Mathf.Max(1, rows);
|
||||
_spacing = spacing;
|
||||
}
|
||||
|
||||
/// <summary>Baut die Anzeige auf <paramref name="count"/> Instanzen an (gedeckelt bei maxVisible).</summary>
|
||||
public void SetTarget(GameObject prefab, int count, int maxVisible)
|
||||
{
|
||||
if (_root == null) return;
|
||||
|
||||
if (prefab != _prefab)
|
||||
{
|
||||
Clear();
|
||||
_prefab = prefab;
|
||||
}
|
||||
|
||||
if (_prefab == null) return;
|
||||
|
||||
int target = Mathf.Clamp(count, 0, Mathf.Max(0, maxVisible));
|
||||
|
||||
while (_instances.Count < target)
|
||||
{
|
||||
var instance = Object.Instantiate(_prefab, _root);
|
||||
instance.transform.localPosition = SlotLocalPosition(_instances.Count);
|
||||
instance.transform.localRotation = Quaternion.identity;
|
||||
_instances.Add(instance);
|
||||
}
|
||||
|
||||
while (_instances.Count > target)
|
||||
{
|
||||
int last = _instances.Count - 1;
|
||||
if (_instances[last] != null) Object.Destroy(_instances[last]);
|
||||
_instances.RemoveAt(last);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Lokale Rasterposition für einen Index – auch als Flug-Ziel genutzt (siehe PlayerCarry).</summary>
|
||||
public Vector3 SlotLocalPosition(int index)
|
||||
{
|
||||
int perLayer = _columns * _rows;
|
||||
int layer = index / perLayer;
|
||||
int rem = index % perLayer;
|
||||
int row = rem / _columns;
|
||||
int col = rem % _columns;
|
||||
return new Vector3(col * _spacing.x, layer * _spacing.y, row * _spacing.z);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
foreach (var instance in _instances)
|
||||
if (instance != null) Object.Destroy(instance);
|
||||
|
||||
_instances.Clear();
|
||||
_prefab = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e0025e09fe99fd443aff717e718ffc70
|
||||
@@ -10,7 +10,7 @@ namespace AfterHours.Shop
|
||||
/// Ein Regalfach: hält genau eine Produktsorte. Auffüllen geht über
|
||||
/// Interact(), Entnahme durch Kunden serverseitig über TakeOne().
|
||||
/// </summary>
|
||||
public sealed class ShelfSlot : NetworkBehaviour, IInteractable
|
||||
public sealed class ShelfSlot : NetworkBehaviour, IInteractable, IPourTarget
|
||||
{
|
||||
[SerializeField] private ProductCatalog _catalog;
|
||||
[SerializeField] private int _capacityUnits = 12;
|
||||
@@ -20,6 +20,11 @@ namespace AfterHours.Shop
|
||||
|
||||
[SerializeField] private Transform _displayRoot;
|
||||
|
||||
[Header("Anzeige-Raster (sauber in Reihen eingeräumt)")]
|
||||
[SerializeField] private int _displayColumns = 4;
|
||||
[SerializeField] private int _displayRows = 3;
|
||||
[SerializeField] private Vector3 _displaySpacing = new(0.1f, 0.1f, 0.1f);
|
||||
|
||||
private readonly NetworkVariable<int> _productId = new(-1);
|
||||
private readonly NetworkVariable<int> _quantity = new(0);
|
||||
private readonly NetworkVariable<float> _price = new(0f);
|
||||
@@ -29,6 +34,20 @@ namespace AfterHours.Shop
|
||||
public float Price => _price.Value;
|
||||
public bool ExposedToStreet => _exposedToStreet;
|
||||
public bool IsEmpty => _quantity.Value <= 0;
|
||||
public bool IsFull => _quantity.Value >= Capacity();
|
||||
|
||||
// Explizite Implementierung, weil "transform" (Component) den Namen schon belegt.
|
||||
Transform IPourTarget.Transform => transform;
|
||||
|
||||
private PackageDisplay _packageDisplay;
|
||||
|
||||
private void Awake() => _packageDisplay = new PackageDisplay(_displayRoot, _displayColumns, _displayRows, _displaySpacing);
|
||||
|
||||
/// <summary>Weltposition des Rasterplatzes mit gegebenem Index – Ziel fürs Flug-Visual (siehe PlayerCarry).</summary>
|
||||
public Vector3 GetSlotWorldPosition(int index)
|
||||
{
|
||||
return _displayRoot != null ? _displayRoot.TransformPoint(_packageDisplay.SlotLocalPosition(index)) : transform.position;
|
||||
}
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
@@ -99,6 +118,24 @@ namespace AfterHours.Shop
|
||||
Debug.Log($"[ShelfSlot] Eingeräumt: {moved} Stück, jetzt {_quantity.Value}/{Capacity()}.");
|
||||
}
|
||||
|
||||
/// <summary>Server: ein Stück aus einem IPourSource einräumen (siehe PlayerCarry).</summary>
|
||||
public bool TryAddOne(int productId)
|
||||
{
|
||||
if (!IsServer) return false;
|
||||
if (!IsEmpty && _productId.Value != productId) return false;
|
||||
if (_quantity.Value >= Capacity()) return false;
|
||||
|
||||
if (IsEmpty)
|
||||
{
|
||||
_productId.Value = productId;
|
||||
var def = _catalog.Get(productId);
|
||||
if (def != null && _price.Value <= 0f) _price.Value = def.RecommendedPrice;
|
||||
}
|
||||
|
||||
_quantity.Value++;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Server: Kunde entnimmt ein Stück.</summary>
|
||||
public bool TakeOne()
|
||||
{
|
||||
@@ -126,8 +163,9 @@ namespace AfterHours.Shop
|
||||
|
||||
private void RefreshVisuals()
|
||||
{
|
||||
// TODO: Verpackungs-Prefabs nach Menge ein-/ausblenden (Object Pool).
|
||||
// Läuft auf allen Peers, rein kosmetisch.
|
||||
var product = _catalog != null ? _catalog.Get(_productId.Value) : null;
|
||||
GameObject prefab = product != null ? product.PackagePrefab : null;
|
||||
_packageDisplay.SetTarget(prefab, _quantity.Value, Capacity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ namespace AfterHours.Shop
|
||||
{
|
||||
if (!IsServer || IsEmpty) return;
|
||||
if (!actor.TryGetComponent<PlayerHands>(out var hands)) return;
|
||||
if (actor.TryGetComponent<PlayerCarry>(out var carry) && carry.IsCarrying) return;
|
||||
|
||||
int want = Mathf.Min(_quantity.Value, PlayerHands.MaxCarry);
|
||||
int taken = hands.TryTake(_productId, want);
|
||||
|
||||
Reference in New Issue
Block a user