네트워크 멀티플레이 환경 문제 수정

관련 문제가 다시 발생하면 이 커밋으로 돌아올 것
This commit is contained in:
2026-02-02 04:24:14 +09:00
parent 3e747a9d97
commit 10b496dfae
49 changed files with 2860 additions and 1792 deletions

View File

@@ -0,0 +1,101 @@
using Unity.Netcode;
using UnityEngine;
using System.Collections.Generic;
namespace Northbound
{
public class ServerResourceManager : NetworkBehaviour
{
public static ServerResourceManager Instance { get; private set; }
private Dictionary<ulong, int> _playerResources = new Dictionary<ulong, int>();
private NetworkVariable<int> _resourcesData = new NetworkVariable<int>();
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
}
public override void OnNetworkSpawn()
{
if (IsServer)
{
Instance = this;
NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected;
NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnected;
}
}
public override void OnNetworkDespawn()
{
if (IsServer)
{
if (NetworkManager.Singleton != null)
{
NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnected;
NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnected;
}
}
}
private void OnClientConnected(ulong clientId)
{
_playerResources[clientId] = 0;
}
private void OnClientDisconnected(ulong clientId)
{
_playerResources.Remove(clientId);
}
public int GetPlayerResourceAmount(ulong clientId)
{
if (_playerResources.TryGetValue(clientId, out var resource))
{
return resource;
}
return 0;
}
public bool CanAddResource(ulong clientId, int amount)
{
if (_playerResources.TryGetValue(clientId, out var resource))
{
return resource + amount <= 100;
}
return false;
}
public int GetAvailableSpace(ulong clientId)
{
if (_playerResources.TryGetValue(clientId, out var resource))
{
return 100 - resource;
}
return 0;
}
public void AddResource(ulong clientId, int amount)
{
if (_playerResources.TryGetValue(clientId, out var resource))
{
int actualAmount = Mathf.Min(amount, 100 - resource);
_playerResources[clientId] = resource + actualAmount;
}
}
public void RemoveResource(ulong clientId, int amount)
{
if (_playerResources.TryGetValue(clientId, out var resource))
{
int actualAmount = Mathf.Min(amount, resource);
_playerResources[clientId] = resource - actualAmount;
}
}
}
}