Worker 네트워크 동기화

This commit is contained in:
2026-02-02 12:59:56 +09:00
parent b5f8943bcc
commit a0cb8499b0
5 changed files with 159 additions and 31 deletions

View File

@@ -163,26 +163,35 @@ namespace Northbound
[Rpc(SendTo.Server, InvokePermission = RpcInvokePermission.Everyone)]
private void AssignOrGatherResourceServerRpc(ulong playerId, ulong resourceId)
{
var playerObject = NetworkManager.Singleton.ConnectedClients[playerId].PlayerObject;
if (playerObject == null)
{
return;
}
Debug.Log($"[Resource] AssignOrGatherResourceServerRpc called - PlayerID: {playerId}, ResourceID: {resourceId}");
var playerInteraction = playerObject.GetComponent<PlayerInteraction>();
bool workerAssigned = false;
if (allowWorkerAssignment && playerInteraction != null && playerInteraction.assignedWorker != null)
if (allowWorkerAssignment)
{
var worker = playerInteraction.assignedWorker;
if (worker.OwnerPlayerId == playerId && (int)worker.CurrentState == 1)
// Find worker owned by player in Following state (server-side, not client-side)
Worker assignedWorker = FindWorkerForPlayer(playerId);
if (assignedWorker != null)
{
worker.AssignMiningTargetServerRpc(resourceId);
workerAssigned = true;
ShowGatheringEffectClientRpc();
Debug.Log($"[Resource] Worker found server-side - OwnerPlayerId: {assignedWorker.OwnerPlayerId}, CurrentState: {(int)assignedWorker.CurrentState}");
if ((int)assignedWorker.CurrentState == 1) // 1 = Following
{
Debug.Log($"[Resource] ✓ Assigning worker to resource!");
assignedWorker.AssignMiningTargetServerRpc(resourceId);
workerAssigned = true;
ShowGatheringEffectClientRpc();
}
else
{
Debug.LogWarning($"[Resource] Worker not in Following state! State: {(int)assignedWorker.CurrentState}");
}
}
else
{
Debug.Log($"[Resource] No worker found for player {playerId} in Following state");
}
}
@@ -192,7 +201,7 @@ namespace Northbound
{
return;
}
GatherResource(playerId);
}
}
@@ -274,5 +283,42 @@ namespace Northbound
{
return transform;
}
private Worker FindWorkerForPlayer(ulong playerId)
{
Debug.Log($"[Resource] FindWorkerForPlayer called - Looking for worker assigned to player {playerId}");
if (NetworkManager.Singleton == null || NetworkManager.Singleton.SpawnManager == null)
{
Debug.LogWarning($"[Resource] FindWorkerForPlayer - NetworkManager or SpawnManager is null");
return null;
}
var spawnedObjects = NetworkManager.Singleton.SpawnManager.SpawnedObjects;
int objectsChecked = 0;
int workersFound = 0;
foreach (var kvp in spawnedObjects)
{
var networkObj = kvp.Value;
var worker = networkObj.GetComponent<Worker>();
if (worker != null)
{
workersFound++;
Debug.Log($"[Resource] FindWorkerForPlayer - Found worker: {worker.gameObject.name}, OwnerPlayerId: {worker.OwnerPlayerId}, State: {(int)worker.CurrentState}");
// Use worker's internal OwnerPlayerId, NOT NetworkObject.OwnerClientId!
if (worker.OwnerPlayerId == playerId && (int)worker.CurrentState == 1) // 1 = Following
{
Debug.Log($"[Resource] FindWorkerForPlayer - ✓ Found worker: {worker.gameObject.name} for player {playerId}");
return worker;
}
}
}
Debug.LogWarning($"[Resource] FindWorkerForPlayer - Checked {objectsChecked} objects, found {workersFound} workers, no matching worker in Following state!");
return null;
}
}
}