refactor: consolidate prefab and spawn handlers (#817)

This commit is contained in:
Paul Pacheco 2019-04-17 09:00:03 -05:00 committed by vis2k
parent b5ff43ada3
commit b4c9c6fdc8

View File

@ -18,6 +18,7 @@ public static class ClientScene
public static bool ready { get; internal set; } public static bool ready { get; internal set; }
public static NetworkConnection readyConnection { get; private set; } public static NetworkConnection readyConnection { get; private set; }
[Obsolete]
public static Dictionary<Guid, GameObject> prefabs = new Dictionary<Guid, GameObject>(); public static Dictionary<Guid, GameObject> prefabs = new Dictionary<Guid, GameObject>();
// scene id to NetworkIdentity // scene id to NetworkIdentity
public static Dictionary<ulong, NetworkIdentity> spawnableObjects; public static Dictionary<ulong, NetworkIdentity> spawnableObjects;
@ -175,29 +176,12 @@ static NetworkIdentity SpawnSceneObject(ulong sceneId)
return null; return null;
} }
// spawn handlers and prefabs
static bool GetPrefab(Guid assetId, out GameObject prefab)
{
prefab = null;
return assetId != Guid.Empty &&
prefabs.TryGetValue(assetId, out prefab) && prefab != null;
}
// this assigns the newAssetId to the prefab. This is for registering dynamically created game objects for already know assetIds. // this assigns the newAssetId to the prefab. This is for registering dynamically created game objects for already know assetIds.
public static void RegisterPrefab(GameObject prefab, Guid newAssetId) public static void RegisterPrefab(GameObject prefab, Guid newAssetId)
{ {
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>(); RegisterSpawnHandler(newAssetId,
if (identity) (position, assetId) => Object.Instantiate(prefab),
{ (spawned) => Object.Destroy(spawned));
identity.assetId = newAssetId;
if (LogFilter.Debug) Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + identity.assetId);
prefabs[identity.assetId] = prefab;
}
else
{
Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component");
}
} }
public static void RegisterPrefab(GameObject prefab) public static void RegisterPrefab(GameObject prefab)
@ -205,8 +189,7 @@ public static void RegisterPrefab(GameObject prefab)
NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>(); NetworkIdentity identity = prefab.GetComponent<NetworkIdentity>();
if (identity) if (identity)
{ {
if (LogFilter.Debug) Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + identity.assetId); RegisterPrefab(prefab, identity.assetId);
prefabs[identity.assetId] = prefab;
NetworkIdentity[] identities = prefab.GetComponentsInChildren<NetworkIdentity>(); NetworkIdentity[] identities = prefab.GetComponentsInChildren<NetworkIdentity>();
if (identities.Length > 1) if (identities.Length > 1)
@ -236,16 +219,7 @@ public static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler,
return; return;
} }
if (identity.assetId == Guid.Empty) RegisterSpawnHandler(identity.assetId, spawnHandler, unspawnHandler);
{
Debug.LogError("RegisterPrefab game object " + prefab.name + " has no prefab. Use RegisterSpawnHandler() instead?");
return;
}
if (LogFilter.Debug) Debug.Log("Registering custom prefab '" + prefab.name + "' as asset:" + identity.assetId + " " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
spawnHandlers[identity.assetId] = spawnHandler;
unspawnHandlers[identity.assetId] = unspawnHandler;
} }
public static void UnregisterPrefab(GameObject prefab) public static void UnregisterPrefab(GameObject prefab)
@ -256,8 +230,7 @@ public static void UnregisterPrefab(GameObject prefab)
Debug.LogError("Could not unregister '" + prefab.name + "' since it contains no NetworkIdentity component"); Debug.LogError("Could not unregister '" + prefab.name + "' since it contains no NetworkIdentity component");
return; return;
} }
spawnHandlers.Remove(identity.assetId); UnregisterSpawnHandler(identity.assetId);
unspawnHandlers.Remove(identity.assetId);
} }
public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler) public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler)
@ -268,6 +241,11 @@ public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler
return; return;
} }
if (assetId == Guid.Empty)
{
Debug.LogError($"Registering invalid asset id {assetId}");
return;
}
if (LogFilter.Debug) Debug.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName()); if (LogFilter.Debug) Debug.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName());
spawnHandlers[assetId] = spawnHandler; spawnHandlers[assetId] = spawnHandler;
@ -282,7 +260,6 @@ public static void UnregisterSpawnHandler(Guid assetId)
public static void ClearSpawners() public static void ClearSpawners()
{ {
prefabs.Clear();
spawnHandlers.Clear(); spawnHandlers.Clear();
unspawnHandlers.Clear(); unspawnHandlers.Clear();
} }
@ -380,25 +357,7 @@ internal static void OnSpawnPrefab(NetworkConnection conn, SpawnPrefabMessage ms
return; return;
} }
if (GetPrefab(msg.assetId, out GameObject prefab)) if (spawnHandlers.TryGetValue(msg.assetId, out SpawnDelegate handler))
{
GameObject obj = Object.Instantiate(prefab, msg.position, msg.rotation);
if (LogFilter.Debug)
{
Debug.Log("Client spawn handler instantiating [netId:" + msg.netId + " asset ID:" + msg.assetId + " pos:" + msg.position + " rotation: " + msg.rotation + "]");
}
localObject = obj.GetComponent<NetworkIdentity>();
if (localObject == null)
{
Debug.LogError("Client object spawned for " + msg.assetId + " does not have a NetworkIdentity");
return;
}
localObject.Reset();
ApplySpawnPayload(localObject, msg.position, msg.rotation, msg.scale, msg.payload, msg.netId);
}
// lookup registered factory for type:
else if (spawnHandlers.TryGetValue(msg.assetId, out SpawnDelegate handler))
{ {
GameObject obj = handler(msg.position, msg.assetId); GameObject obj = handler(msg.position, msg.assetId);
if (obj == null) if (obj == null)