Files
Northbound/Assets/Scripts/PlayerResourceInventory.cs
dal4segno cc475bce3e 업그레이드 데이터 입력 로직 및 기능 추가
캐릭터 스탯을 PlayerStats 컴포넌트에서 모아서 관리하도록 변경
코드에서도 마찬가지
2026-02-23 00:21:44 +09:00

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;
}
}
}