Exemplo n.º 1
0
 // Use this for initialization
 void Start()
 {
     animator = target.GetComponent <Animator>();
 }
    private void Update()
    {
        if (exitingWorld)
        {
            return;
        }
        if (kickFromWorld)
        {
            ExitWorld();
            MainManager.Instance.LoadScene(MainManager.LOGIN_SCENE);
            return;
        }

        lock (updateMethodLock)
        {
            // Distance check.
            foreach (GameObject obj in gameObjects.Values)
            {
                if (obj != null && CalculateDistance(obj.transform.position) > VISIBILITY_RADIUS)
                {
                    WorldObject worldObject = obj.GetComponent <WorldObject>();
                    if (worldObject != null && !deleteQueue.Contains(worldObject.objectId))
                    {
                        deleteQueue.Add(worldObject.objectId);
                    }
                }
            }

            // Delete pending objects.
            foreach (long objectId in deleteQueue)
            {
                if (gameObjects.ContainsKey(objectId))
                {
                    GameObject obj = gameObjects[objectId];
                    if (obj != null)
                    {
                        // Disable.
                        obj.GetComponent <WorldObjectText>().nameMesh.gameObject.SetActive(false);
                        obj.SetActive(false);

                        // Remove from objects list.
                        ((IDictionary <long, GameObject>)gameObjects).Remove(obj.GetComponent <WorldObject>().objectId);

                        // Delete game object from world with a delay.
                        StartCoroutine(DelayedDestroy(obj));
                    }
                }

                // If object was current target, unselect it.
                if (targetWorldObject != null && targetWorldObject.objectId == objectId)
                {
                    SetTarget(null);
                }
            }
            if (deleteQueue.Count > 0)
            {
                deleteQueue.Clear();
            }

            // Move pending objects.
            foreach (KeyValuePair <long, MovementHolder> entry in moveQueue)
            {
                Vector3 position = new Vector3(entry.Value.posX, entry.Value.posY, entry.Value.posZ);
                if (gameObjects.ContainsKey(entry.Key))
                {
                    GameObject obj = gameObjects[entry.Key];
                    if (obj != null)
                    {
                        WorldObject worldObject = obj.GetComponent <WorldObject>();
                        if (worldObject != null)
                        {
                            if (CalculateDistance(position) > VISIBILITY_RADIUS) // Moved out of sight.
                            {
                                // Broadcast self position, object out of sight.
                                NetworkManager.ChannelSend(new LocationUpdateRequest(MovementController.storedPosition.x, MovementController.storedPosition.y, MovementController.storedPosition.z, MovementController.storedRotation));
                                deleteQueue.Add(worldObject.objectId);
                            }
                            else
                            {
                                worldObject.MoveObject(position, entry.Value.heading);
                            }
                        }
                    }
                }
                // Check self teleporting.
                else if (entry.Key == 0)
                {
                    activeCharacter.transform.localPosition             = position;
                    activeCharacter.GetComponent <Rigidbody>().position = position;
                }
                // Request unknown object info from server.
                else if (CalculateDistance(position) <= VISIBILITY_RADIUS)
                {
                    NetworkManager.ChannelSend(new ObjectInfoRequest(entry.Key));
                    // Broadcast self position, in case player is not moving.
                    NetworkManager.ChannelSend(new LocationUpdateRequest(MovementController.storedPosition.x, MovementController.storedPosition.y, MovementController.storedPosition.z, MovementController.storedRotation));
                }

                ((IDictionary <long, MovementHolder>)moveQueue).Remove(entry.Key);
            }

            // Animate pending objects.
            foreach (KeyValuePair <long, AnimationHolder> entry in animationQueue)
            {
                if (gameObjects.ContainsKey(entry.Key))
                {
                    GameObject obj = gameObjects[entry.Key];
                    if (obj != null)
                    {
                        WorldObject worldObject = obj.GetComponent <WorldObject>();
                        if (worldObject != null)
                        {
                            if (worldObject.GetDistance() <= VISIBILITY_RADIUS) // Object is in sight radius.
                            {
                                worldObject.AnimateObject(entry.Value.velocityX, entry.Value.velocityZ, entry.Value.triggerJump, entry.Value.isInWater, entry.Value.isGrounded);
                            }
                        }
                    }
                }

                ((IDictionary <long, AnimationHolder>)animationQueue).Remove(entry.Key);
            }

            // Update pending characters.
            foreach (KeyValuePair <long, CharacterDataHolder> entry in characterUpdateQueue)
            {
                if (gameObjects.ContainsKey(entry.Key))
                {
                    GameObject obj = gameObjects[entry.Key];
                    if (obj != null)
                    {
                        WorldObject worldObject = obj.GetComponent <WorldObject>();
                        if (worldObject != null)
                        {
                            if (worldObject.GetDistance() <= VISIBILITY_RADIUS) // Object is in sight radius.
                            {
                                DynamicCharacterAvatar avatar = obj.GetComponent <DynamicCharacterAvatar>();
                                if (avatar != null)
                                {
                                    // TODO: Manage more things than just item updates.
                                    CharacterDataHolder oldData = worldObject.characterData;
                                    CharacterDataHolder newData = entry.Value;

                                    int headItem = newData.GetHeadItem();
                                    if (headItem != oldData.GetHeadItem())
                                    {
                                        if (headItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.HEAD);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, headItem);
                                        }
                                    }

                                    int chestItem = newData.GetChestItem();
                                    if (chestItem != oldData.GetChestItem())
                                    {
                                        if (chestItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.CHEST);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, chestItem);
                                        }
                                    }

                                    int legsItem = newData.GetLegsItem();
                                    if (legsItem != oldData.GetLegsItem())
                                    {
                                        if (legsItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.LEGS);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, legsItem);
                                        }
                                    }

                                    int handsItem = newData.GetHandsItem();
                                    if (handsItem != oldData.GetHandsItem())
                                    {
                                        if (handsItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.HANDS);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, handsItem);
                                        }
                                    }

                                    int feetItem = newData.GetFeetItem();
                                    if (feetItem != oldData.GetFeetItem())
                                    {
                                        if (feetItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.FEET);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, feetItem);
                                        }
                                    }

                                    int leftHandItem = newData.GetLeftHandItem();
                                    if (leftHandItem != oldData.GetLeftHandItem())
                                    {
                                        if (leftHandItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.LEFT_HAND);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, leftHandItem);
                                        }
                                    }

                                    int rightHandItem = newData.GetRightHandItem();
                                    if (rightHandItem != oldData.GetRightHandItem())
                                    {
                                        if (rightHandItem == 0)
                                        {
                                            CharacterManager.Instance.UnEquipItem(avatar, ItemSlot.RIGHT_HAND);
                                        }
                                        else
                                        {
                                            CharacterManager.Instance.EquipItem(avatar, rightHandItem);
                                        }
                                    }

                                    // Update world object with new data.
                                    worldObject.characterData = newData;
                                }
                            }
                        }
                    }
                }

                ((IDictionary <long, CharacterDataHolder>)characterUpdateQueue).Remove(entry.Key);
            }
        }
    }