online fix
This commit is contained in:
65
Game/Scripts/Networking/NetworkIdAllocator.cs
Normal file
65
Game/Scripts/Networking/NetworkIdAllocator.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace MegaKoop.Game.Networking
|
||||
{
|
||||
internal static class NetworkIdAllocator
|
||||
{
|
||||
public const int PlayerIdStart = 1;
|
||||
public const int EnemyIdStart = 10000;
|
||||
private const int EnemyIdRange = 10000;
|
||||
|
||||
private static int nextEnemyId = EnemyIdStart;
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
||||
private static void ResetOnLoad()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
public static void Reset()
|
||||
{
|
||||
nextEnemyId = EnemyIdStart;
|
||||
}
|
||||
|
||||
public static int AllocateEnemyId()
|
||||
{
|
||||
if (nextEnemyId < EnemyIdStart)
|
||||
{
|
||||
nextEnemyId = EnemyIdStart;
|
||||
}
|
||||
|
||||
const int maxAttempts = EnemyIdRange;
|
||||
int attempts = 0;
|
||||
while ((NetworkIdRegistry.IsIdRegistered(nextEnemyId) || NetworkIdRegistry.IsIdReserved(nextEnemyId)) && attempts < maxAttempts)
|
||||
{
|
||||
AdvanceEnemyCursor();
|
||||
attempts++;
|
||||
}
|
||||
|
||||
int allocated = nextEnemyId;
|
||||
AdvanceEnemyCursor();
|
||||
return allocated;
|
||||
}
|
||||
|
||||
public static bool IsPlayerId(int id) => id >= PlayerIdStart && id < EnemyIdStart;
|
||||
|
||||
public static bool IsEnemyId(int id) => id >= EnemyIdStart;
|
||||
|
||||
public static void SyncEnemyCursor(int id)
|
||||
{
|
||||
if (id >= EnemyIdStart && id >= nextEnemyId)
|
||||
{
|
||||
nextEnemyId = id + 1;
|
||||
}
|
||||
}
|
||||
|
||||
private static void AdvanceEnemyCursor()
|
||||
{
|
||||
nextEnemyId++;
|
||||
if (nextEnemyId >= EnemyIdStart + EnemyIdRange)
|
||||
{
|
||||
nextEnemyId = EnemyIdStart;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user