initial
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AfterHours.Networking
|
||||
{
|
||||
/// <summary>
|
||||
/// Was der Spieler gerade trägt. Server-authoritativ – der Client darf
|
||||
/// weder Produkt noch Menge selbst setzen.
|
||||
/// </summary>
|
||||
public sealed class PlayerHands : NetworkBehaviour
|
||||
{
|
||||
public const int MaxCarry = 12;
|
||||
|
||||
private readonly NetworkVariable<int> _productId = new(-1);
|
||||
private readonly NetworkVariable<int> _quantity = new(0);
|
||||
|
||||
public int ProductId => _productId.Value;
|
||||
public int Quantity => _quantity.Value;
|
||||
public bool IsEmpty => _quantity.Value <= 0;
|
||||
|
||||
/// <summary>Serverseitig: nimmt so viel wie möglich auf, gibt die tatsächliche Menge zurück.</summary>
|
||||
public int TryTake(int productId, int quantity)
|
||||
{
|
||||
if (!IsServer) return 0;
|
||||
if (quantity <= 0) return 0;
|
||||
|
||||
if (!IsEmpty && _productId.Value != productId)
|
||||
return 0; // Hände sind mit etwas anderem belegt
|
||||
|
||||
int free = MaxCarry - _quantity.Value;
|
||||
int taken = Mathf.Min(free, quantity);
|
||||
if (taken <= 0) return 0;
|
||||
|
||||
_productId.Value = productId;
|
||||
_quantity.Value += taken;
|
||||
return taken;
|
||||
}
|
||||
|
||||
/// <summary>Serverseitig: gibt bis zu <paramref name="quantity"/> ab.</summary>
|
||||
public int TryGive(int quantity)
|
||||
{
|
||||
if (!IsServer || IsEmpty) return 0;
|
||||
|
||||
int given = Mathf.Min(_quantity.Value, quantity);
|
||||
_quantity.Value -= given;
|
||||
if (_quantity.Value == 0) _productId.Value = -1;
|
||||
return given;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
if (!IsServer) return;
|
||||
_productId.Value = -1;
|
||||
_quantity.Value = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user