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,86 @@
|
||||
using AfterHours.Networking;
|
||||
using Unity.Netcode;
|
||||
using Unity.Netcode.Components;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AfterHours.Interaction
|
||||
{
|
||||
/// <summary>
|
||||
/// Macht ein Objekt aufheb- und tragbar. Reines Trage-Handwerkszeug ohne
|
||||
/// Fachwissen über Inhalt/Produkte – das bleibt beim jeweiligen Objekt
|
||||
/// (z. B. Lieferkarton). Positionierung beim Tragen ist rein kosmetisch
|
||||
/// und läuft auf jedem Peer selbst (siehe ShelfSlot.RefreshVisuals-Muster),
|
||||
/// kein NGO-Reparenting – siehe Plan-Begründung in Lieferkarton.cs.
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(NetworkObject))]
|
||||
public sealed class Carryable : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] private Collider[] _collidersToDisableWhileCarried;
|
||||
[SerializeField] private NetworkTransform _networkTransform;
|
||||
[SerializeField] private Rigidbody _rigidbody;
|
||||
|
||||
private readonly NetworkVariable<bool> _isCarried = new(
|
||||
false,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Server
|
||||
);
|
||||
|
||||
private readonly NetworkVariable<NetworkObjectReference> _carriedBy = new(
|
||||
default,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Server
|
||||
);
|
||||
|
||||
public bool IsCarried => _isCarried.Value;
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
_isCarried.OnValueChanged += (_, current) => ApplyCarriedState(current);
|
||||
ApplyCarriedState(_isCarried.Value);
|
||||
}
|
||||
|
||||
/// <summary>Server: Träger übernimmt das Objekt. False, wenn schon getragen.</summary>
|
||||
public bool TryPickUp(NetworkObject carrier)
|
||||
{
|
||||
if (!IsServer || _isCarried.Value) return false;
|
||||
|
||||
_carriedBy.Value = carrier;
|
||||
_isCarried.Value = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Server: Objekt an fester Position/Rotation ablegen.</summary>
|
||||
public void Drop(Vector3 position, Quaternion rotation)
|
||||
{
|
||||
if (!IsServer || !_isCarried.Value) return;
|
||||
|
||||
transform.SetPositionAndRotation(position, rotation);
|
||||
_isCarried.Value = false;
|
||||
_carriedBy.Value = default;
|
||||
}
|
||||
|
||||
private void ApplyCarriedState(bool carried)
|
||||
{
|
||||
foreach (var collider in _collidersToDisableWhileCarried)
|
||||
if (collider != null) collider.enabled = !carried;
|
||||
|
||||
if (_networkTransform != null) _networkTransform.enabled = !carried;
|
||||
|
||||
// Sonst kämpft die Physik gegen das kosmetische Tragen (LateUpdate unten).
|
||||
if (_rigidbody != null)
|
||||
{
|
||||
_rigidbody.isKinematic = carried;
|
||||
if (!carried) _rigidbody.linearVelocity = Vector3.zero;
|
||||
}
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (!_isCarried.Value) return;
|
||||
if (!_carriedBy.Value.TryGet(out var carrier)) return;
|
||||
if (!carrier.TryGetComponent<PlayerCarry>(out var carry) || carry.HandSocket == null) return;
|
||||
|
||||
transform.SetPositionAndRotation(carry.HandSocket.position, carry.HandSocket.rotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b73b32288703e1c4995e220e80249f35
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace AfterHours.Interaction
|
||||
{
|
||||
/// <summary>
|
||||
/// Alles, was per Klick-halten Stück für Stück ausgeschüttet werden kann
|
||||
/// (z. B. Lieferkarton, später Lagerregal-Karton). TryTakeOne() läuft
|
||||
/// serverseitig und muss selbst validieren, genau wie IPourTarget.TryAddOne().
|
||||
/// </summary>
|
||||
public interface IPourSource
|
||||
{
|
||||
bool IsOpen { get; }
|
||||
int ProductId { get; }
|
||||
bool IsEmpty { get; }
|
||||
bool TryTakeOne();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7117f9f519059394889cc6edda07b9e1
|
||||
@@ -0,0 +1,19 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace AfterHours.Interaction
|
||||
{
|
||||
/// <summary>
|
||||
/// Alles, was per Klick-halten Stück für Stück befüllt werden kann
|
||||
/// (z. B. ShelfSlot, später Lagerregal). TryAddOne() läuft serverseitig
|
||||
/// und muss selbst validieren, genau wie IInteractable.Interact().
|
||||
/// </summary>
|
||||
public interface IPourTarget
|
||||
{
|
||||
Transform Transform { get; }
|
||||
int ProductId { get; }
|
||||
int Quantity { get; }
|
||||
bool IsEmpty { get; }
|
||||
bool IsFull { get; }
|
||||
bool TryAddOne(int productId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 024acb3d9dadf024b9739a835debdbaa
|
||||
@@ -1,3 +1,4 @@
|
||||
using AfterHours.Networking;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -19,6 +20,8 @@ namespace AfterHours.Interaction
|
||||
private const float ServerReachTolerance = 1.5f;
|
||||
|
||||
private IInteractable _current;
|
||||
private IPourTarget _currentPourTarget;
|
||||
private IPourTarget _pouringTarget;
|
||||
public string CurrentPrompt { get; private set; }
|
||||
|
||||
private void Update()
|
||||
@@ -27,24 +30,38 @@ namespace AfterHours.Interaction
|
||||
|
||||
RefreshTarget();
|
||||
|
||||
if (_current != null && Input.GetKeyDown(KeyCode.E))
|
||||
if (Input.GetKeyDown(KeyCode.E))
|
||||
{
|
||||
var target = ((MonoBehaviour)_current).GetComponent<NetworkObject>();
|
||||
if (target != null) RequestInteractRpc(target);
|
||||
if (_current != null)
|
||||
{
|
||||
var target = ((MonoBehaviour)_current).GetComponent<NetworkObject>();
|
||||
if (target != null) RequestInteractRpc(target);
|
||||
}
|
||||
else if (TryGetComponent<PlayerCarry>(out var carry) && carry.IsCarrying)
|
||||
{
|
||||
carry.RequestDropRpc();
|
||||
}
|
||||
}
|
||||
|
||||
HandlePourInput();
|
||||
}
|
||||
|
||||
private void RefreshTarget()
|
||||
{
|
||||
var previous = _current;
|
||||
_current = null;
|
||||
_currentPourTarget = null;
|
||||
CurrentPrompt = null;
|
||||
|
||||
if (Physics.Raycast(_eyes.position, _eyes.forward, out var hit, _reach, _interactableMask)
|
||||
&& hit.collider.TryGetComponent<IInteractable>(out var interactable))
|
||||
if (Physics.Raycast(_eyes.position, _eyes.forward, out var hit, _reach, _interactableMask))
|
||||
{
|
||||
_current = interactable;
|
||||
CurrentPrompt = interactable.GetPrompt(OwnerClientId);
|
||||
if (hit.collider.TryGetComponent<IInteractable>(out var interactable))
|
||||
{
|
||||
_current = interactable;
|
||||
CurrentPrompt = interactable.GetPrompt(OwnerClientId);
|
||||
}
|
||||
|
||||
hit.collider.TryGetComponent<IPourTarget>(out _currentPourTarget);
|
||||
}
|
||||
|
||||
// TODO: entfernen, sobald ein echter Prompt im UI angezeigt wird.
|
||||
@@ -56,21 +73,62 @@ namespace AfterHours.Interaction
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Maustaste halten, um ein IPourTarget im Blick Stück für Stück zu befüllen.</summary>
|
||||
private void HandlePourInput()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0) && _currentPourTarget != null)
|
||||
{
|
||||
var targetNo = ((MonoBehaviour)_currentPourTarget).GetComponent<NetworkObject>();
|
||||
if (targetNo != null)
|
||||
{
|
||||
_pouringTarget = _currentPourTarget;
|
||||
RequestPourStartRpc(targetNo);
|
||||
}
|
||||
}
|
||||
|
||||
if (_pouringTarget != null && (Input.GetMouseButtonUp(0) || _currentPourTarget != _pouringTarget))
|
||||
{
|
||||
_pouringTarget = null;
|
||||
RequestPourStopRpc();
|
||||
}
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server)]
|
||||
private void RequestInteractRpc(NetworkObjectReference targetRef)
|
||||
{
|
||||
if (!targetRef.TryGet(out var target)) return;
|
||||
|
||||
// Server-Validierung: ist der Spieler überhaupt in Reichweite?
|
||||
float distance = Vector3.Distance(transform.position, target.transform.position);
|
||||
if (distance > _reach + ServerReachTolerance)
|
||||
if (!IsInReach(target.transform.position))
|
||||
{
|
||||
Debug.LogWarning($"[PlayerInteractor] Client {OwnerClientId} zu weit entfernt ({distance:F1} m).");
|
||||
Debug.LogWarning($"[PlayerInteractor] Client {OwnerClientId} zu weit entfernt.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (target.TryGetComponent<IInteractable>(out var interactable))
|
||||
interactable.Interact(NetworkObject);
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server)]
|
||||
private void RequestPourStartRpc(NetworkObjectReference targetRef)
|
||||
{
|
||||
if (!targetRef.TryGet(out var target) || !target.TryGetComponent<IPourTarget>(out var pourTarget)) return;
|
||||
if (!IsInReach(target.transform.position)) return;
|
||||
|
||||
if (TryGetComponent<PlayerCarry>(out var carry)) carry.BeginPour(pourTarget);
|
||||
}
|
||||
|
||||
[Rpc(SendTo.Server)]
|
||||
private void RequestPourStopRpc()
|
||||
{
|
||||
if (TryGetComponent<PlayerCarry>(out var carry)) carry.StopPour();
|
||||
}
|
||||
|
||||
/// <summary>Server-Validierung: ist der Spieler überhaupt in Reichweite?</summary>
|
||||
private bool IsInReach(Vector3 point)
|
||||
{
|
||||
float distance = Vector3.Distance(transform.position, point);
|
||||
return distance <= _reach + ServerReachTolerance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
using AfterHours.Interaction;
|
||||
using AfterHours.Shop;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AfterHours.Networking
|
||||
{
|
||||
/// <summary>
|
||||
/// Was der Spieler gerade als physisches Objekt trägt (z. B. ein
|
||||
/// Lieferkarton) sowie das serverseitig getaktete Ausschütten daraus in
|
||||
/// ein IPourTarget. Getrennt von PlayerHands (abstrakte Stückzahl) –
|
||||
/// beide sind gegenseitig exklusiv, siehe TryPickUp/StorageCrate.Interact.
|
||||
/// </summary>
|
||||
public sealed class PlayerCarry : NetworkBehaviour
|
||||
{
|
||||
[SerializeField] private Transform _handSocket;
|
||||
[SerializeField] private float _pourInterval = 0.4f;
|
||||
[SerializeField] private float _maxPourReach = 2.5f;
|
||||
[SerializeField] private float _flightDuration = 0.35f;
|
||||
|
||||
private readonly NetworkVariable<bool> _isCarrying = new(
|
||||
false,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Server
|
||||
);
|
||||
|
||||
private readonly NetworkVariable<NetworkObjectReference> _carriedObject = new(
|
||||
default,
|
||||
NetworkVariableReadPermission.Everyone,
|
||||
NetworkVariableWritePermission.Server
|
||||
);
|
||||
|
||||
public Transform HandSocket => _handSocket;
|
||||
public bool IsCarrying => _isCarrying.Value;
|
||||
|
||||
private IPourTarget _pourTarget;
|
||||
private IPourSource _pourSource;
|
||||
private NetworkObjectReference _pourTargetRef;
|
||||
private float _pourTimer;
|
||||
|
||||
/// <summary>Server: versucht, <paramref name="target"/> aufzuheben.</summary>
|
||||
public bool TryPickUp(Carryable target)
|
||||
{
|
||||
if (!IsServer || _isCarrying.Value || target == null) return false;
|
||||
if (TryGetComponent<PlayerHands>(out var hands) && !hands.IsEmpty) return false;
|
||||
if (!target.TryPickUp(NetworkObject)) return false;
|
||||
|
||||
_carriedObject.Value = target.NetworkObject;
|
||||
_isCarrying.Value = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Server: legt das getragene Objekt vor dem Spieler ab.</summary>
|
||||
[Rpc(SendTo.Server)]
|
||||
public void RequestDropRpc()
|
||||
{
|
||||
if (!_isCarrying.Value) return;
|
||||
|
||||
StopPour();
|
||||
|
||||
if (_carriedObject.Value.TryGet(out var carried) && carried.TryGetComponent<Carryable>(out var carryable))
|
||||
{
|
||||
Vector3 dropPosition = transform.position + transform.forward * 1f;
|
||||
carryable.Drop(dropPosition, transform.rotation);
|
||||
}
|
||||
|
||||
_isCarrying.Value = false;
|
||||
_carriedObject.Value = default;
|
||||
}
|
||||
|
||||
/// <summary>Server: startet das Ausschütten in <paramref name="target"/>, falls möglich.</summary>
|
||||
public bool BeginPour(IPourTarget target)
|
||||
{
|
||||
if (!IsServer || target == null) return false;
|
||||
if (!_isCarrying.Value || !_carriedObject.Value.TryGet(out var carried)) return false;
|
||||
if (!carried.TryGetComponent<IPourSource>(out var source)) return false;
|
||||
if (!source.IsOpen || source.IsEmpty || target.IsFull) return false;
|
||||
if (!target.IsEmpty && target.ProductId != source.ProductId) return false;
|
||||
if (!((Component)target).TryGetComponent<NetworkObject>(out var targetNo)) return false;
|
||||
|
||||
_pourSource = source;
|
||||
_pourTarget = target;
|
||||
_pourTargetRef = targetNo;
|
||||
_pourTimer = 0f; // erste Einheit sofort, danach im Takt von _pourInterval
|
||||
return true;
|
||||
}
|
||||
|
||||
public void StopPour()
|
||||
{
|
||||
_pourSource = null;
|
||||
_pourTarget = null;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!IsServer || _pourSource == null || _pourTarget == null) return;
|
||||
|
||||
if (!_isCarrying.Value || _pourSource.IsEmpty || _pourTarget.IsFull)
|
||||
{
|
||||
StopPour();
|
||||
return;
|
||||
}
|
||||
|
||||
if (Vector3.Distance(transform.position, _pourTarget.Transform.position) > _maxPourReach)
|
||||
{
|
||||
StopPour();
|
||||
return;
|
||||
}
|
||||
|
||||
_pourTimer -= Time.deltaTime;
|
||||
if (_pourTimer > 0f) return;
|
||||
_pourTimer = _pourInterval;
|
||||
|
||||
int landingIndex = _pourTarget.Quantity;
|
||||
int productId = _pourSource.ProductId;
|
||||
if (_pourSource.TryTakeOne() && _pourTarget.TryAddOne(productId))
|
||||
PlayFlightRpc(_carriedObject.Value, _pourTargetRef, landingIndex);
|
||||
}
|
||||
|
||||
/// <summary>Läuft auf jedem Peer: rein kosmetisches Flug-Visual für die soeben transferierte Einheit.</summary>
|
||||
[Rpc(SendTo.Everyone)]
|
||||
private void PlayFlightRpc(NetworkObjectReference sourceRef, NetworkObjectReference targetRef, int landingIndex)
|
||||
{
|
||||
if (!sourceRef.TryGet(out var sourceObj) || !sourceObj.TryGetComponent<Lieferkarton>(out var box)) return;
|
||||
if (!targetRef.TryGet(out var targetObj) || !targetObj.TryGetComponent<ShelfSlot>(out var shelf)) return;
|
||||
|
||||
GameObject prefab = box.GetPackagePrefab();
|
||||
Vector3 start = box.transform.position + Vector3.up * 0.3f;
|
||||
Vector3 end = shelf.GetSlotWorldPosition(landingIndex);
|
||||
|
||||
FlyingPackage.Spawn(prefab, start, end, shelf.transform.rotation, _flightDuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ed618ff659f6684f8c2ce58ba032c35
|
||||
@@ -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