Support avatar possession for network instantiate

This commit is contained in:
Pascal Serrarens 2022-02-21 17:38:50 +01:00
parent f60f7a5634
commit 2c25f7832b

View File

@ -137,6 +137,7 @@ namespace Passer.Humanoid {
public string name; public string name;
public string avatarPrefabName; public string avatarPrefabName;
public bool physics; public bool physics;
public string possessionLocation = null;
#if RemoteAvatarBundles #if RemoteAvatarBundles
public int remoteAvatarBundleSize; public int remoteAvatarBundleSize;
public byte[] remoteAvatarBundle; public byte[] remoteAvatarBundle;
@ -155,8 +156,13 @@ namespace Passer.Humanoid {
if (humanoid.remoteAvatar == null) if (humanoid.remoteAvatar == null)
avatarPrefabName = humanoid.avatarRig.name; // .Substring(0, humanoid.avatarRig.name.Length - 7); avatarPrefabName = humanoid.avatarRig.name; // .Substring(0, humanoid.avatarRig.name.Length - 7);
else else {
Possessable avatarPossessable = humanoid.remoteAvatar.GetComponent<Possessable>();
if (avatarPossessable != null)
possessionLocation = avatarPossessable.siteLocation;
avatarPrefabName = humanoid.remoteAvatar.name; avatarPrefabName = humanoid.remoteAvatar.name;
}
#if RemoteAvatarBundles #if RemoteAvatarBundles
/* Get the remoteAvatarBundle from the streaming assets */ /* Get the remoteAvatarBundle from the streaming assets */
@ -176,6 +182,7 @@ namespace Passer.Humanoid {
bw.Write(name); bw.Write(name);
bw.Write(avatarPrefabName); bw.Write(avatarPrefabName);
bw.Write(physics); bw.Write(physics);
bw.Write(possessionLocation);
#if RemoteAvatarBundles #if RemoteAvatarBundles
bw.Write(remoteAvatarBundleSize); bw.Write(remoteAvatarBundleSize);
bw.Write(remoteAvatarBundle); bw.Write(remoteAvatarBundle);
@ -194,6 +201,7 @@ namespace Passer.Humanoid {
name = br.ReadString(); name = br.ReadString();
avatarPrefabName = br.ReadString(); avatarPrefabName = br.ReadString();
physics = br.ReadBoolean(); physics = br.ReadBoolean();
possessionLocation = br.ReadString();
#if RemoteAvatarBundles #if RemoteAvatarBundles
remoteAvatarBundleSize = br.ReadInt32(); remoteAvatarBundleSize = br.ReadInt32();
remoteAvatarBundle = br.ReadBytes(remoteAvatarBundleSize); remoteAvatarBundle = br.ReadBytes(remoteAvatarBundleSize);
@ -222,12 +230,12 @@ namespace Passer.Humanoid {
Receive(networking, data); Receive(networking, data);
} }
public static void Receive(this IHumanoidNetworking receivingNetworking, InstantiateHumanoid data) { public static void Receive(this IHumanoidNetworking receivingNetworking, InstantiateHumanoid msg) {
GameObject networkingObj = receivingNetworking.GetGameObject(data.nwId); GameObject networkingObj = receivingNetworking.GetGameObject(msg.nwId);
if (networkingObj == null) { if (networkingObj == null) {
if (receivingNetworking.debug <= DebugLevel.Error) if (receivingNetworking.debug <= DebugLevel.Error)
receivingNetworking.DebugLog("Could not find Networking for Instantiate Humanoid " + data.nwId + "/" + data.humanoidId); receivingNetworking.DebugLog("Could not find Networking for Instantiate Humanoid " + msg.nwId + "/" + msg.humanoidId);
return; return;
} }
@ -235,24 +243,24 @@ namespace Passer.Humanoid {
if (networking.isLocal && !networking.createLocalRemotes) { if (networking.isLocal && !networking.createLocalRemotes) {
if (networking.debug <= DebugLevel.Debug) if (networking.debug <= DebugLevel.Debug)
networking.DebugLog("Remote Humanoid " + data.nwId + "/" + data.humanoidId + " is local and local remotes are not created"); networking.DebugLog("Remote Humanoid " + msg.nwId + "/" + msg.humanoidId + " is local and local remotes are not created");
return; return;
} }
HumanoidControl remoteHumanoid = FindRemoteHumanoid(networking.humanoids, data.humanoidId); HumanoidControl remoteHumanoid = FindRemoteHumanoid(networking.humanoids, msg.humanoidId);
if (remoteHumanoid != null) { if (remoteHumanoid != null) {
if (networking.debug <= DebugLevel.Warning) if (networking.debug <= DebugLevel.Warning)
networking.DebugLog("Remote Humanoid " + data.nwId + "/" + data.humanoidId + " already exists"); networking.DebugLog("Remote Humanoid " + msg.nwId + "/" + msg.humanoidId + " already exists");
// This remote humanoid already exists // This remote humanoid already exists
return; return;
} }
if (networking.debug <= DebugLevel.Info) if (networking.debug <= DebugLevel.Info)
networking.DebugLog("Receive Instantiate Humanoid " + data.nwId + "/" + data.humanoidId); networking.DebugLog("Receive Instantiate Humanoid " + msg.nwId + "/" + msg.humanoidId);
remoteHumanoid = InstantiateRemoteHumanoid(data.name, Vector3.zero, Quaternion.identity); //, position, rotation); remoteHumanoid = InstantiateRemoteHumanoid(msg.name, Vector3.zero, Quaternion.identity); //, position, rotation);
remoteHumanoid.nwId = data.nwId; remoteHumanoid.nwId = msg.nwId;
remoteHumanoid.humanoidId = data.humanoidId; remoteHumanoid.humanoidId = msg.humanoidId;
if (networking.debug <= DebugLevel.Info) if (networking.debug <= DebugLevel.Info)
networking.DebugLog("Remote Humanoid " + remoteHumanoid.nwId + "/" + remoteHumanoid.humanoidId + " Added"); networking.DebugLog("Remote Humanoid " + remoteHumanoid.nwId + "/" + remoteHumanoid.humanoidId + " Added");
@ -266,14 +274,26 @@ namespace Passer.Humanoid {
return; return;
} }
#else #else
GameObject remoteAvatar = (GameObject)Resources.Load(data.avatarPrefabName); if (msg.possessionLocation != null) {
Debug.Log("Need to download instantiate avatar possession from " + msg.possessionLocation);
remoteHumanoid.physics = msg.physics;
HumanoidPlayer.instance.StartCoroutine(
VisitorPossessions.RetrievePossessableAsync(msg.possessionLocation, msg.avatarPrefabName,
retrievedAvatar => InstantiateRetrievedAvatar(networking, remoteHumanoid, retrievedAvatar))
);
return;
}
GameObject remoteAvatar = (GameObject)Resources.Load(msg.avatarPrefabName);
if (remoteAvatar == null) { if (remoteAvatar == null) {
if (networking.debug <= DebugLevel.Error) if (networking.debug <= DebugLevel.Error)
networking.DebugError("Could not load remote avatar " + data.avatarPrefabName + ". Is it located in a Resources folder?"); networking.DebugError("Could not load remote avatar " + msg.avatarPrefabName + ". Is it located in a Resources folder?");
return; return;
} }
#endif #endif
remoteHumanoid.physics = data.physics; remoteHumanoid.physics = msg.physics;
remoteHumanoid.LocalChangeAvatar(remoteAvatar); remoteHumanoid.LocalChangeAvatar(remoteAvatar);
networking.humanoids.Add(remoteHumanoid); networking.humanoids.Add(remoteHumanoid);
@ -281,6 +301,25 @@ namespace Passer.Humanoid {
OnNewRemoteHumanoid(remoteHumanoid); OnNewRemoteHumanoid(remoteHumanoid);
} }
private static void InstantiateRetrievedAvatar(IHumanoidNetworking networking, HumanoidControl remoteHumanoid, GameObject retrievedAvatar) {
if (retrievedAvatar == null) {
if (networking.debug <= DebugLevel.Error)
Debug.LogError("Could not retrieve avatar.");
return;
}
if (networking.debug <= DebugLevel.Info)
networking.DebugLog("Receive Change Possessesable Avatar " + retrievedAvatar);
remoteHumanoid.LocalChangeAvatar(retrievedAvatar);
networking.humanoids.Add(remoteHumanoid);
if (OnNewRemoteHumanoid != null)
OnNewRemoteHumanoid(remoteHumanoid);
}
public static HumanoidControl FindRemoteHumanoid(List<HumanoidControl> humanoids, ulong nwId, int humanoidId) { public static HumanoidControl FindRemoteHumanoid(List<HumanoidControl> humanoids, ulong nwId, int humanoidId) {
foreach (HumanoidControl humanoid in humanoids) { foreach (HumanoidControl humanoid in humanoids) {
if (humanoid.isRemote && humanoid.nwId == nwId && humanoid.humanoidId == humanoidId) if (humanoid.isRemote && humanoid.nwId == nwId && humanoid.humanoidId == humanoidId)