online fix
This commit is contained in:
@@ -9,6 +9,7 @@ namespace MegaKoop.Game.Networking
|
||||
private const int EnemyIdRange = 10000;
|
||||
|
||||
private static int nextEnemyId = EnemyIdStart;
|
||||
private static readonly System.Collections.Generic.HashSet<int> ActiveEnemyIds = new();
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
private static void ResetOnLoad()
|
||||
@@ -19,6 +20,7 @@ namespace MegaKoop.Game.Networking
|
||||
public static void Reset()
|
||||
{
|
||||
nextEnemyId = EnemyIdStart;
|
||||
ActiveEnemyIds.Clear();
|
||||
}
|
||||
|
||||
public static int AllocateEnemyId()
|
||||
@@ -30,7 +32,7 @@ namespace MegaKoop.Game.Networking
|
||||
|
||||
const int maxAttempts = EnemyIdRange;
|
||||
int attempts = 0;
|
||||
while ((NetworkIdRegistry.IsIdRegistered(nextEnemyId) || NetworkIdRegistry.IsIdReserved(nextEnemyId)) && attempts < maxAttempts)
|
||||
while ((ActiveEnemyIds.Contains(nextEnemyId) || NetworkIdRegistry.IsIdRegistered(nextEnemyId) || NetworkIdRegistry.IsIdReserved(nextEnemyId)) && attempts < maxAttempts)
|
||||
{
|
||||
AdvanceEnemyCursor();
|
||||
attempts++;
|
||||
@@ -38,6 +40,7 @@ namespace MegaKoop.Game.Networking
|
||||
|
||||
int allocated = nextEnemyId;
|
||||
AdvanceEnemyCursor();
|
||||
ActiveEnemyIds.Add(allocated);
|
||||
return allocated;
|
||||
}
|
||||
|
||||
@@ -51,6 +54,18 @@ namespace MegaKoop.Game.Networking
|
||||
{
|
||||
nextEnemyId = id + 1;
|
||||
}
|
||||
if (id >= EnemyIdStart)
|
||||
{
|
||||
ActiveEnemyIds.Add(id);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ReleaseEnemyId(int id)
|
||||
{
|
||||
if (id >= EnemyIdStart)
|
||||
{
|
||||
ActiveEnemyIds.Remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
private static void AdvanceEnemyCursor()
|
||||
|
||||
@@ -22,7 +22,15 @@ namespace MegaKoop.Game.Networking
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
NetworkIdRegistry.Unregister(networkId);
|
||||
if (networkId != 0)
|
||||
{
|
||||
if (NetworkIdAllocator.IsEnemyId(networkId))
|
||||
{
|
||||
NetworkIdAllocator.ReleaseEnemyId(networkId);
|
||||
}
|
||||
NetworkIdRegistry.Unregister(networkId);
|
||||
networkId = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void Register()
|
||||
@@ -32,6 +40,11 @@ namespace MegaKoop.Game.Networking
|
||||
return;
|
||||
}
|
||||
|
||||
if (NetworkIdAllocator.IsEnemyId(networkId))
|
||||
{
|
||||
NetworkIdAllocator.SyncEnemyCursor(networkId);
|
||||
}
|
||||
|
||||
if (!NetworkIdRegistry.TryRegister(this))
|
||||
{
|
||||
Debug.LogError($"[NetworkIdentity] Failed to register {name} with ID {networkId}. " +
|
||||
@@ -68,11 +81,21 @@ namespace MegaKoop.Game.Networking
|
||||
// Unregister old ID if it was registered
|
||||
if (networkId != 0)
|
||||
{
|
||||
if (NetworkIdAllocator.IsEnemyId(networkId))
|
||||
{
|
||||
NetworkIdAllocator.ReleaseEnemyId(networkId);
|
||||
}
|
||||
|
||||
NetworkIdRegistry.Unregister(networkId);
|
||||
}
|
||||
|
||||
networkId = id;
|
||||
|
||||
if (NetworkIdAllocator.IsEnemyId(networkId))
|
||||
{
|
||||
NetworkIdAllocator.SyncEnemyCursor(networkId);
|
||||
}
|
||||
|
||||
// Register with new ID
|
||||
if (!NetworkIdRegistry.TryRegister(this))
|
||||
{
|
||||
@@ -85,5 +108,25 @@ namespace MegaKoop.Game.Networking
|
||||
/// Gets the current network ID without triggering registration.
|
||||
/// </summary>
|
||||
public int GetNetworkId() => networkId;
|
||||
|
||||
/// <summary>
|
||||
/// Clears the current network ID and unregisters without emitting duplicate warnings.
|
||||
/// </summary>
|
||||
public void ClearNetworkId()
|
||||
{
|
||||
if (networkId == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int id = networkId;
|
||||
NetworkIdRegistry.Unregister(id);
|
||||
if (NetworkIdAllocator.IsEnemyId(id))
|
||||
{
|
||||
NetworkIdAllocator.ReleaseEnemyId(id);
|
||||
}
|
||||
|
||||
networkId = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,6 +279,19 @@ namespace MegaKoop.Game.Networking
|
||||
hasPendingRemoteSpawn = false;
|
||||
return;
|
||||
}
|
||||
EnemyDefinitionRegistry.Register(definition);
|
||||
|
||||
if (pendingRemoteSpawn.NetworkId != 0 && NetworkIdentity.TryGet(pendingRemoteSpawn.NetworkId, out var existingIdentity) && existingIdentity != null)
|
||||
{
|
||||
var existingInstance = existingIdentity.gameObject;
|
||||
if (existingInstance != null)
|
||||
{
|
||||
existingInstance.SetActive(true);
|
||||
existingInstance.transform.SetPositionAndRotation(pendingRemoteSpawn.Position + definition.PrefabPivotOffset, pendingRemoteSpawn.Rotation);
|
||||
hasPendingRemoteSpawn = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
bool success = spawner != null && spawner.TrySpawn(definition, pendingRemoteSpawn.Position);
|
||||
if (success)
|
||||
@@ -348,7 +361,6 @@ namespace MegaKoop.Game.Networking
|
||||
var identity = instance.GetComponent<NetworkIdentity>();
|
||||
if (identity != null && pendingRemoteSpawn.NetworkId != 0 && identity.NetworkId != pendingRemoteSpawn.NetworkId)
|
||||
{
|
||||
NetworkIdAllocator.SyncEnemyCursor(pendingRemoteSpawn.NetworkId);
|
||||
identity.SetNetworkId(pendingRemoteSpawn.NetworkId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using Game.Scripts.Runtime.Abstractions;
|
||||
using Game.Scripts.Runtime.Data;
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
using MegaKoop.Game.Networking;
|
||||
|
||||
namespace Game.Scripts.Runtime.Pooling
|
||||
{
|
||||
@@ -144,6 +145,8 @@ namespace Game.Scripts.Runtime.Pooling
|
||||
{
|
||||
Destroy(netObj);
|
||||
}
|
||||
var identity = instance.GetComponent<NetworkIdentity>();
|
||||
identity?.ClearNetworkId();
|
||||
bucket.Available.Enqueue(instance);
|
||||
item.Handle?.NotifyDespawned();
|
||||
InstanceDespawned?.Invoke(instance, item.Definition);
|
||||
@@ -183,6 +186,9 @@ namespace Game.Scripts.Runtime.Pooling
|
||||
go.name = $"{definition.Prefab.name}_Pooled";
|
||||
go.SetActive(false);
|
||||
|
||||
var identity = go.GetComponent<NetworkIdentity>();
|
||||
identity?.ClearNetworkId();
|
||||
|
||||
var handle = go.GetComponent<PooledInstance>();
|
||||
if (handle == null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user