online fix

This commit is contained in:
2025-10-27 14:34:26 +01:00
parent 3c5507dd87
commit cf44edc19a
4 changed files with 80 additions and 4 deletions

View File

@@ -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()