50 lines
1.7 KiB
C#
50 lines
1.7 KiB
C#
using AfterHours.Data;
|
|
using AfterHours.Interaction;
|
|
using AfterHours.Networking;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
namespace AfterHours.Shop
|
|
{
|
|
/// <summary>
|
|
/// Lagerkiste im Backroom: hält Nachschub einer einzigen Produktsorte.
|
|
/// Spieler nehmen daraus in die Hände auf und tragen die Ware zum Regal
|
|
/// (siehe ShelfSlot.Interact).
|
|
/// </summary>
|
|
public sealed class StorageCrate : NetworkBehaviour, IInteractable
|
|
{
|
|
[SerializeField] private ProductCatalog _catalog;
|
|
[SerializeField] private int _productId = -1;
|
|
[SerializeField] private int _startingQuantity = 200;
|
|
|
|
private readonly NetworkVariable<int> _quantity = new(0);
|
|
|
|
public int ProductId => _productId;
|
|
public int Quantity => _quantity.Value;
|
|
public bool IsEmpty => _quantity.Value <= 0;
|
|
|
|
public override void OnNetworkSpawn()
|
|
{
|
|
if (IsServer) _quantity.Value = _startingQuantity;
|
|
}
|
|
|
|
public string GetPrompt(ulong clientId)
|
|
{
|
|
var product = _catalog.Get(_productId);
|
|
string name = product != null ? product.DisplayName : "?";
|
|
return IsEmpty ? $"{name} — Lager leer" : $"{name} nehmen — {_quantity.Value} im Lager";
|
|
}
|
|
|
|
/// <summary>Server: Spieler nimmt Ware in die Hände auf.</summary>
|
|
public void Interact(NetworkObject actor)
|
|
{
|
|
if (!IsServer || IsEmpty) return;
|
|
if (!actor.TryGetComponent<PlayerHands>(out var hands)) return;
|
|
|
|
int want = Mathf.Min(_quantity.Value, PlayerHands.MaxCarry);
|
|
int taken = hands.TryTake(_productId, want);
|
|
_quantity.Value -= taken;
|
|
}
|
|
}
|
|
}
|