private void ProcessInput()
 {
     if (_characterComponent.IsGrounded && Input.IsKeyPressed(Keys.Space))
     {
         _characterComponent.Jump();
     }
 }
示例#2
0
 public override void Execute(GameObject actor)
 {
     if (_actor != actor)
     {
         _actor = actor.GetComponent <CharacterComponent>();
     }
     _actor.Jump();
 }
示例#3
0
        /// <summary>
        /// Jump makes the character jump and also accounts for the player's reaction time, making jumping feel more natural by
        ///  allowing jumps within some limit of the last time the character was on the ground
        /// </summary>
        private void Jump()
        {
            var dt = this.GetSimulation().FixedTimeStep;

            // Check if conditions allow the character to jump
            if (JumpReactionThreshold <= 0)
            {
                // No reaction threshold. The character can only jump if grounded
                if (!character.IsGrounded)
                {
                    IsGroundedEventKey.Broadcast(false);
                    return;
                }
            }
            else
            {
                // If there is still enough time left for jumping allow the character to jump even when not grounded
                if (jumpReactionRemaining > 0)
                {
                    jumpReactionRemaining -= dt;
                }

                // If the character on the ground reset the jumping reaction time
                if (character.IsGrounded)
                {
                    jumpReactionRemaining = JumpReactionThreshold;
                }

                // If there is no more reaction time left don't allow the character to jump
                if (jumpReactionRemaining <= 0)
                {
                    IsGroundedEventKey.Broadcast(character.IsGrounded);
                    return;
                }
            }

            // If the player didn't press a jump button we don't need to jump
            bool didJump;

            jumpEvent.TryReceive(out didJump);
            if (!didJump)
            {
                IsGroundedEventKey.Broadcast(true);
                return;
            }

            // Jump!!
            jumpReactionRemaining = 0;
            character.Jump();

            // Broadcast that the character is jumping!
            IsGroundedEventKey.Broadcast(false);
        }
示例#4
0
 private void PerformJumpIfNeeded()
 {
     if (ShouldJump())
     {
         characterComponent.Jump();
         IsJumping = true;
         HadMove   = true;
     }
     else
     {
         IsJumping = !characterComponent.IsGrounded;
     }
 }
示例#5
0
        public override async Task Execute()
        {
            CharPlayer = Entity.Get <CharacterComponent>() as CharacterComponent;
            while (Game.IsRunning)
            {
                await Script.NextFrame();

                if (Input.IsKeyDown(Keys.W) || Input.IsKeyDown(Keys.Space))
                {
                    if (CharPlayer.IsGrounded)
                    {
                        CharPlayer.Jump();
                    }
                }
            }
        }
示例#6
0
        public override void Update()
        {
            var playerState     = PlayerState.Idle;
            var playerDirection = Vector3.Zero;

            // -- Keyboard Inputs

            // Space bar = jump
            if (Input.IsKeyDown(Keys.Space))
            {
                playerState |= PlayerState.Jump;
            }

            // Left - right = run
            if (Input.IsKeyDown(Keys.Right))
            {
                movingToTarget  = false;
                playerState    |= PlayerState.Run;
                playerDirection = Vector3.UnitX * speed;
            }
            else if (Input.IsKeyDown(Keys.Left))
            {
                movingToTarget  = false;
                playerState    |= PlayerState.Run;
                playerDirection = -Vector3.UnitX * speed;
            }

            // -- Pointer (mouse/touch)
            foreach (var pointerEvent in Input.PointerEvents.Where(pointerEvent => pointerEvent.EventType == PointerEventType.Pressed))
            {
                if (!movingToTarget)
                {
                    var screenX = (pointerEvent.Position.X - 0.5f) * 2.0f;
                    screenX *= 8.75f;

                    autoPilotTarget = new Vector3(screenX, 0, 0);

                    movingToTarget = true;
                }
                else
                {
                    playerState |= PlayerState.Jump;
                }
            }

            // -- Logic

            // are we autopiloting?
            if (movingToTarget)
            {
                var direction = autoPilotTarget - Entity.Transform.Position;
                direction.Y = 0;

                //should we stop?
                var length = direction.Length();
                if (length < speed)
                {
                    movingToTarget = false;

                    playerDirection = Vector3.Zero;

                    playerState = PlayerState.Idle;
                }
                else
                {
                    direction.Normalize();

                    playerDirection = (direction.X > 0 ? Vector3.UnitX : -Vector3.UnitX) * speed;

                    playerState |= PlayerState.Run;
                }
            }

            // did we start jumping?
            if (playerState.HasFlag(PlayerState.Jump) && !oldState.HasFlag(PlayerState.Jump))
            {
                playerController.Jump();
            }

            // did we just land?
            if (oldState.HasFlag(PlayerState.Jump))
            {
                if (!playerController.IsGrounded)
                {
                    //force set jump flag
                    if (!playerState.HasFlag(PlayerState.Jump))
                    {
                        playerState |= PlayerState.Jump;
                        // Mantain motion
                        playerDirection = oldDirection;
                    }
                }
                else if (playerController.IsGrounded)
                {
                    //force clear jump flag
                    if (playerState.HasFlag(PlayerState.Jump))
                    {
                        playerState ^= PlayerState.Jump;
                    }
                }
            }

            // did we start running?
            if (playerState.HasFlag(PlayerState.Run) && !oldState.HasFlag(PlayerState.Run))
            {
                PlayRun();
            }
            // did we stop running?
            else if (!playerState.HasFlag(PlayerState.Run) && oldState.HasFlag(PlayerState.Run))
            {
                PlayIdle();
            }

            // movement logic
            if (oldDirection != playerDirection)
            {
                playerController.Move(playerDirection);

                if (playerState.HasFlag(PlayerState.Run))
                {
                    if ((playerDirection.X > 0 && Entity.Transform.Scale.X < 0) ||
                        (playerDirection.X < 0 && Entity.Transform.Scale.X > 0))
                    {
                        Entity.Transform.Scale.X *= -1.0f;
                    }
                }
            }

            // Store current state for next frame
            oldState     = playerState;
            oldDirection = playerDirection;
        }
示例#7
0
        public override void Update()
        {
            var rotationDelta = Input.MouseDelta;

            // Compute translation speed according to framerate and modifiers
            var translationSpeed = Speed * (float)Game.UpdateTime.Elapsed.TotalSeconds;

            // Take shortest path
            var deltaPitch = desiredPitch - pitch;
            var deltaYaw   = (desiredYaw - yaw) % MathUtil.TwoPi;

            if (deltaYaw < 0)
            {
                deltaYaw += MathUtil.TwoPi;
            }
            if (deltaYaw > MathUtil.Pi)
            {
                deltaYaw -= MathUtil.TwoPi;
            }
            desiredYaw = yaw + deltaYaw;

            // Perform orientation transition
            var rotationAdaptation = (float)Game.UpdateTime.Elapsed.TotalSeconds * RotationAdaptationSpeed;

            yaw   = Math.Abs(deltaYaw) < rotationAdaptation ? desiredYaw : yaw + rotationAdaptation * Math.Sign(deltaYaw);
            pitch = Math.Abs(deltaPitch) < rotationAdaptation ? desiredPitch : pitch + rotationAdaptation * Math.Sign(deltaPitch);

            desiredYaw   = yaw -= 1.333f * rotationDelta.X * RotationSpeed; // we want to rotate faster Horizontally and Vertically
            desiredPitch = pitch = MathUtil.Clamp(pitch - rotationDelta.Y * RotationSpeed, -MathUtil.PiOverTwo, MathUtil.PiOverTwo);

            if (CameraEntity != null)
            {
                //we need to pitch only the camera node
                CameraEntity.Transform.Rotation = baseCameraRotation * Quaternion.RotationYawPitchRoll(0, pitch, 0);
            }
            Entity.Transform.Rotation = Quaternion.RotationYawPitchRoll(yaw, 0, 0); //do not apply pitch to our controller

            var move = new Vector3();

            var forward          = Vector3.Transform(ForwardVector, Entity.Transform.Rotation);
            var projectedForward = Vector3.Normalize(new Vector3(forward.X, 0, forward.Z));
            var up    = Vector3.TransformNormal(UpVector, Matrix.RotationQuaternion(Entity.Transform.Rotation));
            var right = Vector3.Cross(forward, up);

            if (Input.IsKeyDown(Keys.A) || Input.IsKeyDown(Keys.Left))
            {
                move += -right;
            }
            if (Input.IsKeyDown(Keys.D) || Input.IsKeyDown(Keys.Right))
            {
                move += right;
            }
            if (Input.IsKeyDown(Keys.W) || Input.IsKeyDown(Keys.Up))
            {
                move += projectedForward;
            }
            if (Input.IsKeyDown(Keys.S) || Input.IsKeyDown(Keys.Down))
            {
                move += -projectedForward;
            }

            move.Normalize();

            move *= translationSpeed;

            //please note that the default character controller ignores rotation, in the case of complex collisions you would have more kinematic elements within your model anyway.
            character.Move(move);

            if (Input.IsKeyPressed(Keys.Space))
            {
                character.Jump();
            }

            if (Input.IsKeyReleased(Keys.Escape))
            {
                var game = (Game)Game;
                game.Exit();
            }
        }
示例#8
0
        public override void Update()
        {
            var playerState     = PlayerState.Idle;
            var playerDirection = Vector3.Zero;

            // -- Keyboard Inputs

            // Space bar = jump
            if (Input.IsKeyDown(Keys.Space))
            {
                playerState |= PlayerState.Jump;
            }

            // Left - right = run
            if (Input.IsKeyDown(Keys.Right))
            {
                playerState    |= PlayerState.Run;
                playerDirection = Vector3.UnitX * speed;
            }
            else if (Input.IsKeyDown(Keys.Left))
            {
                playerState    |= PlayerState.Run;
                playerDirection = -Vector3.UnitX * speed;
            }

            // -- Logic

            // did we start jumping?
            if (playerState.HasFlag(PlayerState.Jump) && !oldState.HasFlag(PlayerState.Jump))
            {
                playerController.Jump();
            }

            // did we just land?
            if (oldState.HasFlag(PlayerState.Jump))
            {
                if (!playerController.IsGrounded)
                {
                    //force set jump flag
                    if (!playerState.HasFlag(PlayerState.Jump))
                    {
                        playerState |= PlayerState.Jump;
                        // Mantain motion
                        playerDirection = oldDirection;
                    }
                }
                else if (playerController.IsGrounded)
                {
                    //force clear jump flag
                    if (playerState.HasFlag(PlayerState.Jump))
                    {
                        playerState ^= PlayerState.Jump;
                    }
                }
            }

            // did we start running?
            if (playerState.HasFlag(PlayerState.Run) && !oldState.HasFlag(PlayerState.Run))
            {
                // PlayRun();
            }
            // did we stop running?
            else if (!playerState.HasFlag(PlayerState.Run) && oldState.HasFlag(PlayerState.Run))
            {
                // PlayIdle();
            }

            // movement logic
            if (oldDirection != playerDirection)
            {
                playerController.SetVelocity(playerDirection);

                if (playerState.HasFlag(PlayerState.Run))
                {
                    if ((playerDirection.X > 0 && Entity.Transform.Scale.X < 0) ||
                        (playerDirection.X < 0 && Entity.Transform.Scale.X > 0))
                    {
                        Entity.Transform.Scale.X *= -1.0f;
                    }
                }
            }

            // Store current state for next frame
            oldState     = playerState;
            oldDirection = playerDirection;
        }