네트워크 멀티플레이 대응
This commit is contained in:
73
Assets/Scripts/PlayerInventory.cs
Normal file
73
Assets/Scripts/PlayerInventory.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user