Ejemplo n.º 1
0
        protected override void Update(float deltaTime)
        {
            if (Input.GetControlDown("DropIntel"))
            {
                DropIntel();
            }

            // Check if aiming
            IsAiming = AllowUserInput && ItemManager.SelectedItem != null &&
                       (ItemManager.SelectedItem.Type.HasFlag(ItemType.Gun) ||
                        ItemManager.SelectedItem.Type.HasFlag(ItemType.MelonLauncher)) &&
                       ItemManager.SelectedItem.CanSecondaryFire()
                ? Input.GetControl("SecondaryFire") : false;

            IsSprinting = AllowUserInput && CharacterController.IsMoving && CharacterController.DeltaPosition.Length > 0 &&
                          !IsAiming && !CharacterController.IsCrouching && !Input.GetControl("Walk") && Input.GetControl("Sprint");

            // Update the currently selected item
            if (AllowUserInput)
            {
                ItemManager.Update(
                    Input.GetControlDown("PrimaryFire"), Input.GetControl("PrimaryFire"),
                    Input.GetControlDown("SecondaryFire"), Input.GetControl("SecondaryFire"),
                    Input.GetControlDown("Reload"), deltaTime);
            }
            else
            {
                ItemManager.Update(false, false, false, false, false, deltaTime);
            }

            // Move the player
            Vector3 move = Vector3.Zero;

            if (AllowUserInput)
            {
                if (Input.GetControl("MoveForward"))
                {
                    move.Z -= 1;
                }
                if (Input.GetControl("MoveBackward"))
                {
                    move.Z += 1;
                }
                if (Input.GetControl("MoveLeft"))
                {
                    move.X -= 1;
                }
                if (Input.GetControl("MoveRight"))
                {
                    move.X += 1;
                }

                UpdateMoveVector(move, Input.GetControl("Jump"), Input.GetControl("Sprint"), IsWalking = Input.GetControl("Walk"));
            }
            else
            {
                UpdateMoveVector(move, false, false, false);
            }

            if (Input.GetControlDown("Crouch") && AllowUserInput)
            {
                CharacterController.IsCrouching = true;
            }
            else if (Input.GetControlUp("Crouch") ||
                     (!Input.GetControl("Crouch") && AllowUserInput) && CharacterController.IsCrouching)
            {
                CharacterController.TryUncrouch(World);
            }

            // Toggle thirdperson, mouse, and flashlight
            if (AllowUserInput)
            {
                if (Input.GetKeyDown(Key.C))
                {
                    IsRenderingThirdperson = !IsRenderingThirdperson;
                }

                if (Input.GetControlDown("ToggleFlashlight"))
                {
                    flashlight.Visible = !flashlight.Visible;
                    flashlightAudioSource?.Play();
                }
            }

            // Handle landing
            if (CharacterController.IsGrounded && !lastGrounded)
            {
                landAudioSource?.Play();
            }

            // Handle walking/running
            walkingAudioSource.IsPlaying       = CharacterController.IsGrounded && CharacterController.IsMoving && !IsSprinting;
            walkingAudioSource.IterationLength = (1f / Viewbob.GetSpeed()) * 2f;
            walkingAudioSource.Update(deltaTime);

            runningAudioSource.IsPlaying       = CharacterController.IsGrounded && CharacterController.IsMoving && IsSprinting;
            runningAudioSource.IterationLength = (1f / Viewbob.GetSpeed()) * 2f;
            runningAudioSource.Update(deltaTime);

            // Ensure the firstperson offset is correct (this changes when crouching)
            camera.FirstPersonLockOffset = new Vector3(0, Size.Y / 2f - 1.1f, 0);

            // Update the flashlight
            flashlight.Position = IsRenderingThirdperson
                ? Transform.Position + Camera.Active.FirstPersonLockOffset
                : camera.Position;
            flashlight.Direction = -camera.LookVector;

            // Store grounded state
            lastGrounded = CharacterController.IsGrounded;

            // Process refresh cooldown
            if (refreshCooldown > 0)
            {
                refreshCooldown -= deltaTime;
            }

            base.Update(deltaTime);

            // Update camera effects
            camfx.Update(deltaTime);

            // Update the viewbob
            Viewbob.Update(deltaTime);

            if (Transform.Position.Y < -200)
            {
                Transform.Position = startLocation;
            }
        }
        protected override void Update(float deltaTime)
        {
            if (StateInfo != null)
            {
                // Get user input
                bool inputM1        = AllowUserInput && Input.GetControlDown("PrimaryFire");
                bool inputM1Held    = AllowUserInput && Input.GetControl("PrimaryFire");
                bool inputM2        = AllowUserInput && Input.GetControlDown("SecondaryFire");
                bool inputM2Held    = AllowUserInput && Input.GetControl("SecondaryFire");
                bool inputReload    = AllowUserInput && Input.GetControlDown("Reload");
                bool inputJump      = AllowUserInput && Input.GetControl("Jump");
                bool inputSprint    = AllowUserInput && Input.GetControl("Sprint");
                bool inputWalk      = AllowUserInput && Input.GetControl("Walk");
                bool inputCrouch    = AllowUserInput && Input.GetControl("Crouch");
                bool inputCrouchUp  = AllowUserInput && Input.GetControlUp("Crouch");
                bool inputDropIntel = AllowUserInput && Input.GetControlDown("DropIntel");

                // Check if aiming
                IsAiming = ItemManager.SelectedItem != null &&
                           (ItemManager.SelectedItem.Type.HasFlag(ItemType.Gun) ||
                            ItemManager.SelectedItem.Type.HasFlag(ItemType.MelonLauncher)) &&
                           ItemManager.SelectedItem.CanSecondaryFire()
                    ? inputM2Held : false;

                IsSprinting = CharacterController.IsMoving && CharacterController.DeltaPosition.Length > 0 && !IsAiming &&
                              !CharacterController.IsCrouching && !inputWalk && inputSprint;

                // Update the selected item
                ItemManager.Update(inputM1, inputM1Held, inputM2, inputM2Held, inputReload, deltaTime);

                // Move the character
                Vector3 move = Vector3.Zero;
                if (AllowUserInput)
                {
                    if (Input.GetControl("MoveForward"))
                    {
                        move.Z -= 1;
                    }
                    if (Input.GetControl("MoveBackward"))
                    {
                        move.Z += 1;
                    }
                    if (Input.GetControl("MoveLeft"))
                    {
                        move.X -= 1;
                    }
                    if (Input.GetControl("MoveRight"))
                    {
                        move.X += 1;
                    }
                }

                UpdateMoveVector(move, inputJump, inputSprint, IsWalking = inputWalk);

                if (inputCrouch)
                {
                    CharacterController.IsCrouching = true;
                }
                else if (inputCrouchUp || !inputCrouch && CharacterController.IsCrouching)
                {
                    CharacterController.TryUncrouch(World);
                }

                // Toggle the mouse and flashlight
                if (AllowUserInput)
                {
                    if (Input.GetControlDown("ToggleFlashlight"))
                    {
                        flashlight.Visible = !flashlight.Visible;
                        flashlightAudioSource?.Play();
                    }
                }

                // Handle landing
                if (CharacterController.IsGrounded && !lastGrounded)
                {
                    landAudioSource?.Play();
                }

                // Handle walking/running
                walkingAudioSource.IsPlaying       = CharacterController.IsGrounded && CharacterController.IsMoving && !IsSprinting;
                walkingAudioSource.IterationLength = (1f / Viewbob.GetSpeed()) * 2f;
                walkingAudioSource.Update(deltaTime);

                runningAudioSource.IsPlaying       = CharacterController.IsGrounded && CharacterController.IsMoving && IsSprinting;
                runningAudioSource.IterationLength = (1f / Viewbob.GetSpeed()) * 2f;
                runningAudioSource.Update(deltaTime);

                // Update viewbob
                Viewbob.Update(deltaTime);

                // Ensure camera firstperson offset is accurate (changes when crouched)
                Camera.Active.FirstPersonLockOffset = new Vector3(0, Size.Y / 2f - 1.1f, 0);

                // Update the camera effects
                camfx.Update(deltaTime);

                // Update flashlight transformation
                flashlight.Position = IsRenderingThirdperson
                    ? Transform.Position + Camera.Active.FirstPersonLockOffset
                    : Camera.Active.Position;
                flashlight.Direction = -Camera.Active.LookVector;

                // Update the snapshot
                ClientSnapshot.X        = Transform.Position.X;
                ClientSnapshot.Y        = Transform.Position.Y;
                ClientSnapshot.Z        = Transform.Position.Z;
                ClientSnapshot.CamYaw   = camera.Yaw;
                ClientSnapshot.CamPitch = camera.Pitch;

                ClientSnapshot.IsFlashlightVisible = flashlight.Visible;
                ClientSnapshot.Reload    = inputReload || ClientSnapshot.Reload;
                ClientSnapshot.DropIntel = inputDropIntel || ClientSnapshot.DropIntel;

                ClientSnapshot.IsCrouching = CharacterController.IsCrouching;
                ClientSnapshot.IsSprinting = IsSprinting;
                ClientSnapshot.IsMoving    = CharacterController.IsMoving;
                ClientSnapshot.IsAiming    = IsAiming;
                ClientSnapshot.IsGrounded  = CharacterController.IsGrounded;
                ClientSnapshot.Jump        = jumped || ClientSnapshot.Jump;

                ClientSnapshot.SelectedItem = (byte)ItemManager.SelectedItemIndex;

                if (ItemManager.SelectedItem is BlockItem blockItem)
                {
                    ClientSnapshot.ColorR = blockItem.BlockColor.R;
                    ClientSnapshot.ColorG = blockItem.BlockColor.G;
                    ClientSnapshot.ColorB = blockItem.BlockColor.B;
                }
            }

            lastGrounded = CharacterController.IsGrounded;
            jumped       = false;

            base.Update(deltaTime);
        }
        protected override void Update(float deltaTime)
        {
            if (StateInfo != null)
            {
                // Update viewbob
                Viewbob.UpdateReplicated(deltaTime);

                // Step each value animation
                float interpStep = DashCMD.GetCVar <float>("cl_interp_rep") * deltaTime;
                interpPos.Step(interpStep);
                yawAnim.Step(interpStep);
                pitchAnim.Step(interpStep);

                // Update each property affected by interpolation
                Transform.Position = interpPos.Value;

                camera.Yaw   = yawAnim.Value;
                camera.Pitch = pitchAnim.Value;

                // Update our "fake" camera
                camera.Position = Transform.Position + new Vector3(0, Size.Y / 2f - 1.1f, 0);
                camera.Update(deltaTime);

                // Update flashlight
                flashlight.Position  = camera.Position;
                flashlight.Direction = -camera.LookVector;

                // Update the item manager
                ItemManager.UpdateReplicated(deltaTime);

                // Handle jump
                if (jumpAudioSource != null)
                {
                    jumpAudioSource.Position = Transform.Position;
                }

                if (jump)
                {
                    jump = false;
                    jumpAudioSource?.Play();
                }

                // Handle landing
                if (landAudioSource != null)
                {
                    landAudioSource.Position = Transform.Position;
                }

                if (land)
                {
                    land = false;
                    landAudioSource?.Play();
                }

                // Handle walking/running
                walkingAudioSource.IsPlaying       = isGrounded && isMoving && !IsSprinting;
                walkingAudioSource.IterationLength = (1f / GetFootstepIterationSpeed()) * 2f;
                walkingAudioSource.Update(deltaTime, Transform.Position);

                runningAudioSource.IsPlaying       = isGrounded && isMoving && IsSprinting;
                runningAudioSource.IterationLength = (1f / GetFootstepIterationSpeed()) * 2f;
                runningAudioSource.Update(deltaTime, Transform.Position);

                // Handle reload
                if (reload)
                {
                    reload = false;

                    if (ItemManager.SelectedItem is Gun gun)
                    {
                        gun.OnReplicatedReload();
                    }
                }
            }

            base.Update(deltaTime);
        }