Ejemplo n.º 1
0
        IEnumerator Start()
        {
            netUpdateHandler = NetworkUpdateHandler.Instance;

            if (netUpdateHandler == null)
            {
                throw new System.Exception("NetworkUpdateHandler instance not found.");
            }

            //WebGLMemoryStats.LogMoreStats("MainClientUpdater Start BEFORE");
            entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

            //if we are missing our avatarentitygroup in editor try to get it from our game object
            if (!mainClientAvatarEntityGroup)
            {
                mainClientAvatarEntityGroup = GetComponent <AvatarEntityGroup>();
            }

            if (!mainClientAvatarEntityGroup)
            {
                Debug.LogError("No mainclientAvatarEntityGroup found in  MainClientUpdater.cs to use to send updates to network", gameObject);
            }

            //cashe our avatar transform references
            (headEntityTransform, leftHandEntityTransform, rightHandEntityTransform) = (mainClientAvatarEntityGroup.avatarComponent_Head.transform, mainClientAvatarEntityGroup.avatarComponent_hand_L.transform, mainClientAvatarEntityGroup.avatarComponent_hand_R.transform);

            //get references to AnimControllers to send anim state info
            (leftHandAnimator, rightHandAnimator) = (leftHandEntityTransform.GetComponent <Animator>(), rightHandEntityTransform.GetComponent <Animator>());

            if (!rightHandAnimator || !leftHandAnimator)
            {
                Debug.LogError("We are missing our Animator Controller from our hands in MainClientUpdater");
            }

            //WebGLMemoryStats.LogMoreStats("MainClientUpdater Start IN_MIDDLE");

            //Wait for the avatar to finish loading to allow us to continue and start sending updates
            yield return(new WaitUntil(() =>
            {
                return GameStateManager.Instance.isAvatarLoadingFinished;
            }));

            //Register our OnUpdate funcion to start sending updates
            GameStateManager.Instance.RegisterUpdatableObject(this);

            //Grab current position of hands to detect if they have moved to avoid rendering them when they havent;
            leftHandOriginalLocalPosition = mainClientAvatarEntityGroup.avatarComponent_hand_L.transform.localPosition;

            rightHandOriginalLocalPosition = mainClientAvatarEntityGroup.avatarComponent_hand_R.transform.localPosition;
        }
        public void InitializeAvatar(int clientID)
        {
            AvatarEntityGroup avatarEntityGroup = gameObjects[nextAvailableSlot].GetComponentInChildren <AvatarEntityGroup>();

            avatarEntityGroup.clientID = clientID;

            if (!avatarEntityGroupFromClientId.ContainsKey(clientID))
            {
                avatarEntityGroupFromClientId.Add(clientID, avatarEntityGroup);
            }
            else
            {
                avatarEntityGroupFromClientId[clientID] = avatarEntityGroup;
            }

            if (!avatarIndexFromClientId.ContainsKey(clientID))
            {
                avatarIndexFromClientId.Add(clientID, nextAvailableSlot);
            }
            else
            {
                avatarIndexFromClientId[clientID] = nextAvailableSlot;
            }
        }
        public void AddNewClient(int clientID, bool isMainPlayer = false)
        {
            if (clientID == NetworkUpdateHandler.Instance.client_id && !isMainPlayer)
            {
                Debug.LogError($"AddNewClient was requested for own client ID ({clientID}) when isMainPlayer was false. Skipping.");

                return;
            }

            if (GameStateManager.IsAlive && !GameStateManager.Instance.isAvatarLoadingFinished)
            {
                return;
            }

            //setup newclient
            if (clientIDs.Contains(clientID))
            {
                return;
            }

            clientIDs.Add(clientID);

            string nameLabel = NetworkUpdateHandler.Instance.GetPlayerNameFromClientID(clientID);

            if (clientIDs.Count >= clientReserveCount)
            {
                Debug.LogWarning("REACHED MAX CLIENTS IN GAME - ADD TO CLIENT RESERVE COUNT FOR MORE USERS");

                return;
            }

            //go through all available slots
            for (int i = 0; i < clientReserveCount - 1; i++)
            {
                //skip avatars that are already on, and your reserved spot
                if (gameObjects[i].activeInHierarchy || mainClientIndex == i)
                {
                    continue;
                }

                AvatarEntityGroup avatarEntityGroup = gameObjects[i].GetComponentInChildren <AvatarEntityGroup>();
                avatarEntityGroup.clientID = clientID;

                // entityManager.AddComponentData(avatarEntityGroup.rootEntity, new NetworkEntityIdentificationComponentData {clientID = clientID });

                if (!avatarEntityGroupFromClientId.ContainsKey(clientID))
                {
                    avatarEntityGroupFromClientId.Add(clientID, avatarEntityGroup);
                }
                else
                {
                    avatarEntityGroupFromClientId[clientID] = avatarEntityGroup;
                }

                if (!avatarIndexFromClientId.ContainsKey(clientID))
                {
                    avatarIndexFromClientId.Add(clientID, i);
                }
                else
                {
                    avatarIndexFromClientId[clientID] = i;
                }

                if (!usernameFromClientId.ContainsKey(clientID))
                {
                    usernameFromClientId.Add(clientID, nameLabel);
                }
                else
                {
                    usernameFromClientId[clientID] = nameLabel;
                }

                //create main ui tag
                if (!UIManager.IsAlive)
                {
                    Debug.LogWarning("AddNewClient: UIManager.IsAlive was false");
                }
                else
                {
                    UIManager.Instance.clientTagSetup.CreateTextFromString(nameLabel, clientID);
                }

                //select how to handle avatars
                if (isMainPlayer)
                {
                    gameObjects[i].SetActive(false);

                    mainClientIndex = i;

                    var temp = avatarEntityGroupFromClientId[clientID].transform;

                    var ROT = entityManager.GetComponentData <Rotation>(avatarEntityGroupFromClientId[clientID].rootEntity).Value.value;//.entity_data.rot;

                    //To prevent offset issues when working with editor
#if UNITY_WEBGL && !UNITY_EDITOR || TESTING_BEFORE_BUILDING
                    mainPlayer.transform.position = temp.position;

                    mainPlayer.transform.rotation = new Quaternion(ROT.x, ROT.y, ROT.z, ROT.w);

                    handsParent.transform.position = temp.position;

                    handsParent.transform.rotation = new Quaternion(ROT.x, ROT.y, ROT.z, ROT.w);
#endif
                    //Turn Off Dummy
                    var parObject = temp.parent.parent.gameObject;

                    parObject.name = "Main_Client";
                }
                else
                {
                    gameObjects[i].SetActive(true);

                    //setAnimatorReferences what I use to reference other peoples hands
                    int idForLeftHand  = (clientID * 100) + (2);
                    int idForRightHand = (clientID * 100) + (3);

                    if (!animatorFromClientId.ContainsKey(idForLeftHand))
                    {
                        animatorFromClientId.Add(idForLeftHand, avatarEntityGroupFromClientId[clientID].avatarComponent_hand_L.GetComponent <Animator>());
                        animatorFromClientId.Add(idForRightHand, avatarEntityGroupFromClientId[clientID].avatarComponent_hand_R.GetComponent <Animator>());
                    }
                    else
                    {
                        animatorFromClientId[idForLeftHand]  = avatarEntityGroupFromClientId[clientID].avatarComponent_hand_L.GetComponent <Animator>();
                        animatorFromClientId[idForRightHand] = avatarEntityGroupFromClientId[clientID].avatarComponent_hand_R.GetComponent <Animator>();
                    }

                    //set text label
                    SpeechToTextSnippet newText = new SpeechToTextSnippet
                    {
                        stringType = (int)STRINGTYPE.CLIENT_NAME,
                        target     = clientID,//clientIDToAvatarIndex[clientID],
                        text       = nameLabel,
                    };

                    ProcessSpeechToTextSnippet(newText);
                }
                break;
            }
        }