Files
Northbound/Assets/Scripts/PlayerInventory.cs

74 lines
2.1 KiB
C#

using Unity.Netcode;
using UnityEngine;
namespace Northbound
{
public class PlayerInventory : NetworkBehaviour
{
[Header("Inventory Settings")]
public int maxResourceCapacity = 100;
private NetworkVariable<int> resourceCount = new NetworkVariable<int>(
0,
NetworkVariableReadPermission.Everyone,
NetworkVariableWritePermission.Owner
);
public int CurrentResourceAmount => resourceCount.Value;
public int MaxResourceCapacity => maxResourceCapacity;
public bool CanAddResource(int amount)
{
return resourceCount.Value + amount <= maxResourceCapacity;
}
public int GetAvailableSpace()
{
return maxResourceCapacity - resourceCount.Value;
}
public void AddResources(int amount)
{
if (amount <= 0) return;
int actualAmount = Mathf.Min(amount, maxResourceCapacity - resourceCount.Value);
resourceCount.Value += actualAmount;
Debug.Log($"Player {OwnerClientId} added resources: +{actualAmount}, total: {resourceCount.Value}/{maxResourceCapacity}");
}
public void RemoveResources(int amount)
{
if (amount <= 0) return;
int actualAmount = Mathf.Min(amount, resourceCount.Value);
resourceCount.Value -= actualAmount;
Debug.Log($"Player {OwnerClientId} used resources: -{actualAmount}, total: {resourceCount.Value}/{maxResourceCapacity}");
}
public void SetResources(int amount)
{
resourceCount.Value = Mathf.Clamp(amount, 0, maxResourceCapacity);
}
[Rpc(SendTo.Owner)]
public void AddResourcesRpc(int amount)
{
AddResources(amount);
}
[Rpc(SendTo.Owner)]
public void RemoveResourcesRpc(int amount)
{
RemoveResources(amount);
}
[Rpc(SendTo.Owner)]
public void SetResourcesRpc(int amount)
{
SetResources(amount);
}
}
}