/// <summary> /// Handles the event of a client disconnecting. /// </summary> private void handleClientDisconnected(short clientID) { if (clientAvatarMappings.ContainsKey(clientID)) //Client requested an avatar { ClientAvatar avatar = clientAvatarMappings[clientID]; avatar.connected = false; avatar.gameObject.SetActive(false); StartCoroutine(destroyAvatar(clientID, disconnectedDestructionDelay)); //Destroy avatar after delay Debug.Log("<b><color=teal>[AvatarManager]</color></b> Client disconnected, deactivated avatar for client: " + clientID + "."); } }
/// <summary> /// Destroys a client avatar with the ID, clientID. /// </summary> private void destroyAvatar(short clientID) { if (clientAvatarMappings.ContainsKey(clientID)) { ClientAvatar clientAvatar = clientAvatarMappings[clientID]; clientAvatarMappings.Remove(clientID); //Remove references to client avatar clientAvatarRequests.Remove(clientID); //Remove record of avatar request OnClientAvatarDestroyed.Invoke(clientAvatar); //Notify observers Destroy(clientAvatar.gameObject); //Destroy avatar Debug.Log("<b><color=teal>[AvatarManager]</color></b> Destroyed avatar for client: " + clientID + "."); } }
/// <summary> /// Handles the event of a client reconnecting. /// </summary> private void handleClientReconnected(short clientID) { if (clientAvatarRequests.Contains(clientID)) //Client requested an avatar { Debug.Log("<b><color=teal>[AvatarManager]</color></b> Client " + clientID + " reconnected, reconnecting avatar."); if (clientAvatarMappings.ContainsKey(clientID)) //Client avatar still exists { ClientAvatar avatar = clientAvatarMappings[clientID]; avatar.connected = true; avatar.gameObject.SetActive(true); } else //Client avatar was already destroyed, create new one { handleAvatarRequest(clientID); } } }
/// <summary> /// Destroys a client avatar with the ID, clientID after the given delay if it doesn't reconnect. /// </summary> private IEnumerator destroyAvatar(short clientID, float delay) { if (clientAvatarMappings.ContainsKey(clientID)) { float startTime = Time.time; ClientAvatar clientAvatar = clientAvatarMappings[clientID]; while (Time.time - startTime < delay && !clientAvatar.connected) { yield return(null); } //Only destroy avatar if it is still disconnected if (!clientAvatar.connected) { destroyAvatar(clientID); } } }
/// <summary> /// Handles a client request for an avatar. /// </summary> /// <param name="clientID">ID of the client who requested the avatar.</param> internal void handleAvatarRequest(short clientID) { //Avatar request is for this client, ignore it if (NetworkIdentity.isClient && clientID == ClientBehaviour.Instance.ClientID) { return; } if (!clientAvatarMappings.ContainsKey(clientID)) //Make sure the client doesn't already have an avatar { clientAvatarRequests.Add(clientID); //Record client's request for an avatar ClientAvatar newAvatar = Instantiate(clientAvatarPrefab).AddComponent <ClientAvatar>(); newAvatar.clientID = clientID; newAvatar.name = getAvatarName(clientID); clientAvatarMappings.Add(clientID, newAvatar); Debug.Log("<b><color=teal>[AvatarManager]</color></b> Created an avatar for client: " + clientID); OnClientAvatarCreated.Invoke(newAvatar); //Notify observers } }