示例#1
0
    public void Update()
    {
        Camera.GetComponent <Camera>().fieldOfView = GetCurrentFieldOfViewY();

        ClientPeer.Update();

        if (ClientPeer.IsConnectedToServer)
        {
            if (PlayerId != null)
            {
                var playerObjectComponent = PlayerObjectSystem.Instance.FindPlayerObjectComponent(PlayerId.Value);

                if (Input.GetKeyDown(KeyCode.Alpha1))
                {
                    if (playerObjectComponent != null)
                    {
                        RequestSwitchWeapons(playerObjectComponent, 0);
                    }
                }

                if (Input.GetKeyDown(KeyCode.Alpha2))
                {
                    if (playerObjectComponent != null)
                    {
                        RequestSwitchWeapons(playerObjectComponent, 1);
                    }
                }

                var mouseScrollDirection = Input.GetAxis("Mouse ScrollWheel");
                if (mouseScrollDirection > 0)
                {
                    if (playerObjectComponent != null)
                    {
                        var newWeaponIndex = MathfExtensions.Wrap(
                            playerObjectComponent.State.CurrentWeaponIndex + 1,
                            0, playerObjectComponent.State.Weapons.Length - 1
                            );
                        RequestSwitchWeapons(
                            playerObjectComponent,
                            newWeaponIndex
                            );
                    }
                }
                else if (mouseScrollDirection < 0)
                {
                    if (playerObjectComponent != null)
                    {
                        var newWeaponIndex = MathfExtensions.Wrap(
                            playerObjectComponent.State.CurrentWeaponIndex - 1,
                            0, playerObjectComponent.State.Weapons.Length - 1
                            );
                        RequestSwitchWeapons(
                            playerObjectComponent,
                            newWeaponIndex
                            );
                    }
                }

                if (Input.GetButtonDown("Jump"))
                {
                    if ((playerObjectComponent != null) && PlayerObjectSystem.Instance.IsPlayerGrounded(playerObjectComponent))
                    {
                        PlayerObjectSystem.Instance.Jump(playerObjectComponent);
                        ClientPeer.CallRpcOnServer("ServerOnPlayerTryJump", ClientPeer.reliableChannelId, new
                        {
                            playerId = PlayerId.Value
                        });
                    }
                }

                // Pickup Weapon
                if (playerObjectComponent != null)
                {
                    var playerId = playerObjectComponent.State.Id;
                    var playersClosestWeaponInfo = WeaponSystem.Instance.ClosestWeaponInfoByPlayerId
                                                   .GetValueOrDefault(playerId);

                    if (playersClosestWeaponInfo != null)
                    {
                        var closestWeaponId        = playersClosestWeaponInfo.Item1;
                        var closestWeaponComponent = WeaponSystem.Instance.FindWeaponComponent(closestWeaponId);
                        if (closestWeaponComponent != null)
                        {
                            var closestWeaponType = closestWeaponComponent.State.Type;

                            var playersWeaponOfSameType = playerObjectComponent.State.Weapons.FirstOrDefault(
                                w => w?.Type == closestWeaponType
                                );
                            var playerHasWeaponOfTypeWithRoomForAmmo =
                                (playersWeaponOfSameType != null) &&
                                (playersWeaponOfSameType.BulletsUsed > 0);
                            var playerHasEmptyWeaponSlot = playerObjectComponent.State.Weapons.Any(
                                w => w == null
                                );

                            if (playerHasWeaponOfTypeWithRoomForAmmo || playerHasEmptyWeaponSlot || Input.GetButtonDown("Pickup Weapon"))
                            {
                                ClientPeer.CallRpcOnServer("ServerOnPlayerTryPickupWeapon", ClientPeer.reliableChannelId, new
                                {
                                    playerId = playerId,
                                    weaponId = closestWeaponId
                                });
                            }
                        }
                    }
                }

                if (Input.GetButtonDown("Zoom"))
                {
                    var changedZoomLevel = TryToChangeZoomLevel();

                    if (changedZoomLevel)
                    {
                        var equippedWeaponComponent = PlayerObjectSystem.Instance.GetEquippedWeaponComponent(playerObjectComponent);
                        var weaponAudioSource       = equippedWeaponComponent.GetComponent <AudioSource>();
                        weaponAudioSource?.PlayOneShot(OsFps.Instance.SniperZoomSound);
                    }
                }

                if (playerObjectComponent?.State.IsAlive ?? false)
                {
                    ShieldBar.gameObject.SetActive(true);

                    var shieldPercent = playerObjectComponent.State.Shield / OsFps.MaxPlayerShield;
                    ShieldBar.HealthPercent = shieldPercent;

                    HealthBar.gameObject.SetActive(true);

                    var healthPercent = playerObjectComponent.State.Health / OsFps.MaxPlayerHealth;
                    HealthBar.HealthPercent = healthPercent;
                }
                else
                {
                    ShieldBar.gameObject.SetActive(false);
                    HealthBar.gameObject.SetActive(false);
                }
            }

            if (Input.GetButtonDown("Chat"))
            {
                if (!_isShowingChatMessageInput)
                {
                    SetChatBoxIsVisible(true);
                    ChatBox.MessageInputField.Select();
                    ChatBox.MessageInputField.ActivateInputField();

                    Cursor.lockState = CursorLockMode.None;
                    Cursor.visible   = true;
                }
                else
                {
                    ConfirmChatMessage();
                }
            }

            if (Input.GetButtonDown("Toggle Menu"))
            {
                if (!_isShowingMenu)
                {
                    var pauseScreenComponent = GameObject.Instantiate(
                        OsFps.Instance.PauseScreenPrefab, OsFps.Instance.CanvasObject.transform
                        ).GetComponent <PauseScreenComponent>();
                    OsFps.Instance.MenuStack.Push(pauseScreenComponent);

                    Cursor.lockState = CursorLockMode.None;
                    Cursor.visible   = true;
                }
                else
                {
                    OsFps.Instance.MenuStack.Pop();
                }
            }

            if (!_isShowingChatMessageInput && !_isShowingMenu)
            {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible   = false;
            }

            var chatMessagesText = string.Join("\n", _chatMessages);
            ChatBox.VisualMessagesText.text     = chatMessagesText;
            ChatBox.ScrollableMessagesText.text = chatMessagesText;

            SendInputPeriodicFunction.TryToCall();

            if (PlayerId != null)
            {
                if (!OsFps.Instance.IsRemoteClient && (Camera.transform.parent == null))
                {
                    var playerObjectComponent = PlayerObjectSystem.Instance.FindPlayerObjectComponent(PlayerId.Value);
                    if (playerObjectComponent != null)
                    {
                        AttachCameraToPlayer(PlayerId.Value);
                    }
                }
            }
        }
    }