109 lines
3.2 KiB
C#
109 lines
3.2 KiB
C#
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
namespace Northbound
|
|
{
|
|
public class PlayerResourceInventory : NetworkBehaviour
|
|
{
|
|
private int _displayAmount = 0;
|
|
|
|
public int CurrentResourceAmount => _displayAmount;
|
|
public int MaxResourceCapacity => _playerStats?.GetCapacity() ?? 100;
|
|
|
|
private NetworkPlayerController _networkPlayerController;
|
|
private PlayerStats _playerStats;
|
|
|
|
private bool IsLocalPlayer => _networkPlayerController != null && _networkPlayerController.IsLocalPlayer;
|
|
private ulong LocalPlayerId => _networkPlayerController != null ? _networkPlayerController.OwnerPlayerId : OwnerClientId;
|
|
|
|
private void Awake()
|
|
{
|
|
_networkPlayerController = GetComponent<NetworkPlayerController>();
|
|
_playerStats = GetComponent<PlayerStats>();
|
|
}
|
|
|
|
[Rpc(SendTo.Server)]
|
|
private void SetMaxCapacityServerRpc(int maxCapacity, ulong playerId)
|
|
{
|
|
var resourceManager = ServerResourceManager.Instance;
|
|
if (resourceManager != null)
|
|
{
|
|
resourceManager.SetPlayerMaxCapacity(playerId, maxCapacity);
|
|
}
|
|
}
|
|
|
|
public override void OnNetworkSpawn()
|
|
{
|
|
if (_networkPlayerController != null)
|
|
{
|
|
_networkPlayerController.OnOwnerChanged += OnOwnerPlayerIdChanged;
|
|
}
|
|
|
|
TryInitialize();
|
|
}
|
|
|
|
private void OnOwnerPlayerIdChanged(ulong newOwnerId)
|
|
{
|
|
TryInitialize();
|
|
}
|
|
|
|
private void TryInitialize()
|
|
{
|
|
if (!IsLocalPlayer) return;
|
|
|
|
SetMaxCapacityServerRpc(MaxResourceCapacity, LocalPlayerId);
|
|
RequestResourceUpdateServerRpc(LocalPlayerId);
|
|
}
|
|
|
|
public override void OnNetworkDespawn()
|
|
{
|
|
if (_networkPlayerController != null)
|
|
{
|
|
_networkPlayerController.OnOwnerChanged -= OnOwnerPlayerIdChanged;
|
|
}
|
|
}
|
|
|
|
[Rpc(SendTo.Server)]
|
|
public void RequestResourceUpdateServerRpc(ulong playerId)
|
|
{
|
|
var resourceManager = ServerResourceManager.Instance;
|
|
if (resourceManager != null)
|
|
{
|
|
int amount = resourceManager.GetPlayerResourceAmount(playerId);
|
|
UpdateResourceAmountClientRpc(amount);
|
|
}
|
|
}
|
|
|
|
[Rpc(SendTo.ClientsAndHost)]
|
|
private void UpdateResourceAmountClientRpc(int amount)
|
|
{
|
|
_displayAmount = amount;
|
|
}
|
|
|
|
public void UpdateResourceAmountDirectly(int amount)
|
|
{
|
|
_displayAmount = amount;
|
|
}
|
|
|
|
public bool CanAddResource(int amount)
|
|
{
|
|
var resourceManager = ServerResourceManager.Instance;
|
|
if (resourceManager != null)
|
|
{
|
|
return resourceManager.CanAddResource(LocalPlayerId, amount);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public int GetAvailableSpace()
|
|
{
|
|
var resourceManager = ServerResourceManager.Instance;
|
|
if (resourceManager != null)
|
|
{
|
|
return resourceManager.GetAvailableSpace(LocalPlayerId);
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
}
|