Characters

This commit is contained in:
2025-10-05 18:21:16 +02:00
parent b52b3aa830
commit 174a399ee7
77 changed files with 14406 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
using System.Collections.Generic;
using UnityEngine;
namespace MegaKoop.Game.Networking
{
[DisallowMultipleComponent]
public class NetworkIdentity : MonoBehaviour
{
private static readonly Dictionary<int, NetworkIdentity> registry = new();
private static int nextId = 1;
[SerializeField] private int networkId;
[SerializeField] private bool assignOnAwake = true;
public int NetworkId => networkId;
private void Awake()
{
if (assignOnAwake && networkId == 0)
{
networkId = nextId++;
}
Register();
}
private void OnDestroy()
{
if (registry.TryGetValue(networkId, out NetworkIdentity existing) && existing == this)
{
registry.Remove(networkId);
}
}
private void Register()
{
if (networkId == 0)
{
Debug.LogWarning($"[NetworkIdentity] {name} has no network id and won't be tracked.");
return;
}
if (registry.TryGetValue(networkId, out NetworkIdentity existing) && existing != this)
{
Debug.LogWarning($"[NetworkIdentity] Duplicate network id {networkId} detected. Overwriting reference.");
}
registry[networkId] = this;
}
public static bool TryGet(int id, out NetworkIdentity identity) => registry.TryGetValue(id, out identity);
}
}