Ejemplo n.º 1
0
        public override void Update()
        {
            {
                if (KeysLeft.Any(key => Input.IsKeyDown(key)))
                {
                    rotationDirection += -Vector2.UnitX * speed * (float)Game.UpdateTime.Elapsed.TotalSeconds;
                }
                if (KeysRight.Any(key => Input.IsKeyDown(key)))
                {
                    rotationDirection += +Vector2.UnitX * speed * (float)Game.UpdateTime.Elapsed.TotalSeconds;
                }
                if (KeysUp.Any(key => Input.IsKeyDown(key)))
                {
                    rotationDirection += +Vector2.UnitY * speed * (float)Game.UpdateTime.Elapsed.TotalSeconds;
                }
                if (KeysDown.Any(key => Input.IsKeyDown(key)))
                {
                    rotationDirection += -Vector2.UnitY * speed * (float)Game.UpdateTime.Elapsed.TotalSeconds;
                }

                var rotation = Quaternion.RotationYawPitchRoll(rotationDirection.X, rotationDirection.Y, 0);

                Entity.Transform.Rotation = rotation;
            }
        }
Ejemplo n.º 2
0
        public override void Update()
        {
            // Character movement
            //  The character movement can be controlled by a game controller or a keyboard
            //  The character receives input in 3D world space, so that it can be controlled by an AI script as well
            //  For this reason we map the 2D user input to a 3D movement using the current camera
            {
                // Game controller: left stick
                var moveDirection  = Input.GetLeftThumbAny(DeadZone);
                var isDeadZoneLeft = moveDirection.Length() < DeadZone;
                if (isDeadZoneLeft)
                {
                    moveDirection = Vector2.Zero;
                }
                else
                {
                    moveDirection.Normalize();
                }

                // Keyboard
                if (KeysLeft.Any(key => Input.IsKeyDown(key)))
                {
                    moveDirection += -Vector2.UnitX;
                }
                if (KeysRight.Any(key => Input.IsKeyDown(key)))
                {
                    moveDirection += +Vector2.UnitX;
                }
                if (KeysUp.Any(key => Input.IsKeyDown(key)))
                {
                    moveDirection += +Vector2.UnitY;
                }
                if (KeysDown.Any(key => Input.IsKeyDown(key)))
                {
                    moveDirection += -Vector2.UnitY;
                }

                // Broadcast the movement vector as a world-space Vector3 to allow characters to be controlled
                var worldSpeed = (Camera != null)
                    ? Utils.LogicDirectionToWorldDirection(moveDirection, Camera, Vector3.UnitY)
                    : new Vector3(moveDirection.X, 0, moveDirection.Y); // If we don't have the correct camera attached we can send the directions anyway, but they probably won't match

                MoveDirectionEventKey.Broadcast(worldSpeed);
            }

            // Camera rotation
            //  Camera rotation is ALWAYS in camera space, so we don't need to account for View or Projection matrices
            {
                // Game controller: right stick
                var cameraDirection = Input.GetRightThumbAny(DeadZone);
                var isDeadZoneRight = cameraDirection.Length() < DeadZone;
                if (isDeadZoneRight)
                {
                    cameraDirection = Vector2.Zero;
                }
                else
                {
                    cameraDirection.Normalize();
                }

                // Mouse-based camera rotation.
                //  Only enabled after you click the screen to lock your cursor, pressing escape will cancel it.
                if (Input.IsMouseButtonDown(MouseButton.Left))
                {
                    Input.LockMousePosition(true);
                    Game.IsMouseVisible = false;
                }
                if (Input.IsKeyPressed(Keys.Escape))
                {
                    Input.UnlockMousePosition();
                    Game.IsMouseVisible = true;
                }
                if (Input.IsMousePositionLocked)
                {
                    cameraDirection += new Vector2(Input.MouseDelta.X, -Input.MouseDelta.Y) * MouseSensitivity;
                }

                // Broadcast the camera direction directly, as a screen-space Vector2
                CameraDirectionEventKey.Broadcast(cameraDirection);
            }

            {
                // Controller: Right trigger
                // Mouse: Left button, Tap events
                var didShoot = Input.GetRightTriggerAny(0.2f) > 0.2f;   // This will allow for continuous shooting

                if (Input.PointerEvents.Any(x => x.EventType == PointerEventType.Pressed))
                {
                    didShoot = true;
                }

                if (Input.HasMouse && Input.IsMouseButtonDown(MouseButton.Left))                  // This will allow for continuous shooting
                {
                    didShoot = true;
                }

                ShootEventKey.Broadcast(didShoot);
            }

            {
                // Reload weapon
                var isReloading = Input.IsGamePadButtonDownAny(GamePadButton.X);
                if (KeysReload.Any(key => Input.IsKeyDown(key)))
                {
                    isReloading = true;
                }

                ReloadEventKey.Broadcast(isReloading);
            }
        }
Ejemplo n.º 3
0
        public override void Update()
        {
            // Character movement: should be camera-aware
            {
                // Left stick: movement
                var moveDirection = Input.GetLeftThumbAny(DeadZone);

                // Keyboard: movement
                if (KeysLeft.Any(key => Input.IsKeyDown(key)))
                {
                    moveDirection += -Vector2.UnitX;
                }
                if (KeysRight.Any(key => Input.IsKeyDown(key)))
                {
                    moveDirection += +Vector2.UnitX;
                }
                if (KeysUp.Any(key => Input.IsKeyDown(key)))
                {
                    moveDirection += +Vector2.UnitY;
                }
                if (KeysDown.Any(key => Input.IsKeyDown(key)))
                {
                    moveDirection += -Vector2.UnitY;
                }

                // Broadcast the movement vector as a world-space Vector3 to allow characters to be controlled
                var worldSpeed = (Camera != null)
                    ? Utils.LogicDirectionToWorldDirection(moveDirection, Camera, Vector3.UnitY)
                    : new Vector3(moveDirection.X, 0, moveDirection.Y);

                // Adjust vector's magnitute - worldSpeed has been normalized
                var moveLength     = moveDirection.Length();
                var isDeadZoneLeft = moveLength < DeadZone;
                if (isDeadZoneLeft)
                {
                    worldSpeed = Vector3.Zero;
                }
                else
                {
                    if (moveLength > 1)
                    {
                        moveLength = 1;
                    }
                    else
                    {
                        moveLength = (moveLength - DeadZone) / (1f - DeadZone);
                    }

                    worldSpeed *= moveLength;
                }

                MoveDirectionEventKey.Broadcast(worldSpeed);
            }

            // Camera rotation: left-right rotates the camera horizontally while up-down controls its altitude
            {
                // Right stick: camera rotation
                var cameraDirection = Input.GetRightThumbAny(DeadZone);
                var isDeadZoneRight = cameraDirection.Length() < DeadZone;
                if (isDeadZoneRight)
                {
                    cameraDirection = Vector2.Zero;
                }
                else
                {
                    cameraDirection.Normalize();
                }

                // Mouse-based camera rotation. Only enabled after you click the screen to lock your cursor, pressing escape cancels this
                if (Input.IsMouseButtonDown(MouseButton.Left))
                {
                    Input.LockMousePosition(true);
                    Game.IsMouseVisible = false;
                }
                if (Input.IsKeyPressed(Keys.Escape))
                {
                    Input.UnlockMousePosition();
                    Game.IsMouseVisible = true;
                }
                if (Input.IsMousePositionLocked)
                {
                    cameraDirection += new Vector2(Input.MouseDelta.X, -Input.MouseDelta.Y) * MouseSensitivity;
                }

                // Broadcast the camera direction directly, as a screen-space Vector2
                CameraDirectionEventKey.Broadcast(cameraDirection);
            }

            // Jumping: don't bother with jump restrictions here, just pass the button states
            {
                // Controller: jumping
                var isJumpDown = Input.IsGamePadButtonDownAny(GamePadButton.A);
                var didJump    = (!jumpButtonDown && isJumpDown);
                jumpButtonDown = isJumpDown;

                // Keyboard: jumping
                didJump |= (KeysJump.Any(key => Input.IsKeyPressed(key)));

                JumpEventKey.Broadcast(didJump);
            }
        }
Ejemplo n.º 4
0
        public override void Update()
        {
            MoveDirection = Vector2.Zero;
            AimDirection  = Vector2.Zero;

            // Left stick: movement
            var padDirection = Input.GetLeftThumb(ControllerIndex);
            var isDeadZone   = padDirection.Length() < DeadZone;

            if (!isDeadZone)
            {
                MoveDirection = padDirection;
            }
            MoveDirection.Normalize();

            // Right stick: aim
            padDirection = Input.GetRightThumb(ControllerIndex);
            var aimSpeed = padDirection.Length();

            isDeadZone = aimSpeed < DeadZone;
            // Make sure aim starts at 0 when outside deadzone
            aimSpeed = (aimSpeed - DeadZone) / (1.0f - DeadZone);
            // Clamp aim speed
            if (aimSpeed > 1.0f)
            {
                aimSpeed = 1.0f;
            }
            // Curve aim speed
            aimSpeed = (float)Math.Pow(aimSpeed, 1.6);
            if (!isDeadZone)
            {
                AimDirection = padDirection;
                AimDirection.Normalize();
                AimDirection *= aimSpeed;
            }

            // Keyboard move
            if (KeysLeft.Any(key => Input.IsKeyDown(key)))
            {
                MoveDirection += -Vector2.UnitX;
            }
            if (KeysRight.Any(key => Input.IsKeyDown(key)))
            {
                MoveDirection += +Vector2.UnitX;
            }
            if (KeysUp.Any(key => Input.IsKeyDown(key)))
            {
                MoveDirection += +Vector2.UnitY;
            }
            if (KeysDown.Any(key => Input.IsKeyDown(key)))
            {
                MoveDirection += -Vector2.UnitY;
            }

            var isAiming      = KeysAim.Any(key => Input.IsKeyDown(key)) || Input.GetLeftTrigger(ControllerIndex) >= DeadZone;
            var isFiring      = KeysShoot.Any(key => Input.IsKeyDown(key)) || Input.GetRightTrigger(ControllerIndex) >= DeadZone;
            var isStarting    = KeysStart.Any(key => Input.IsKeyPressed(key)) || Input.IsGamePadButtonPressed(ControllerIndex, GamePadButton.Start);
            var isReloading   = KeysReload.Any(key => Input.IsKeyPressed(key)) || Input.IsGamePadButtonPressed(ControllerIndex, GamePadButton.Y);
            var isInteracting = KeysInteract.Any(key => Input.IsKeyPressed(key)) || Input.IsGamePadButtonPressed(ControllerIndex, GamePadButton.A);

            if (isStarting)
            {
                OnStart?.Invoke(Entity);
            }

            if (isReloading)
            {
                OnReload?.Invoke(Entity);
            }

            if (isInteracting)
            {
                OnInteract?.Invoke(Entity);
            }

            // Mouse aim (after normalization of aim direction)
            // mouse aim is only enabled after you click the screen to lock your cursor, pressing escape cancels this
            if (Input.IsMouseButtonDown(MouseButton.Left))
            {
                Input.LockMousePosition(true);
            }
            if (Input.IsKeyPressed(Keys.Escape))
            {
                Input.UnlockMousePosition();
            }
            if (Input.IsMousePositionLocked)
            {
                // Mouse shooting
                if (Input.IsMouseButtonDown(MouseButton.Left))
                {
                    isFiring = true;
                }

                // Mouse aiming
                if (Input.IsMouseButtonDown(MouseButton.Right))
                {
                    isAiming = true;
                }

                AimDirection += new Vector2(Input.MouseDelta.X, -Input.MouseDelta.Y) * MouseSensitivity;
            }

            if (InvertXAxis)
            {
                AimDirection = new Vector2(-AimDirection.X, AimDirection.Y);
            }
            if (InvertYAxis)
            {
                AimDirection = new Vector2(AimDirection.X, -AimDirection.Y);
            }

            AimState  = isAiming;
            FireState = isFiring;
        }