Пример #1
0
        private void ProcessScheduledJump_()
        {
            if (this.scheduledJumpState_ == ScheduledState.STARTING)
            {
                if (this.StateMachine.CanJumpFromGround)
                {
                    var isLongjump = this.StateMachine.State == PlayerState.SLIDING;
                    var isBackflip = this.StateMachine.State == PlayerState.TURNING &&
                                     FinMath.Abs(this.Rigidbody.XVelocity) >
                                     PlayerConstants.UPRIGHT_MAX_SLOW_XSPD;
                    var newYVel =
                        isLongjump
                  ? PlayerConstants.LONGJUMP_SPEED
                  : isBackflip
                      ? PlayerConstants.BACKFLIP_JUMP_SPEED
                      : PlayerConstants.JUMP_SPEED;
                    var newState =
                        isLongjump ? PlayerState.LONGJUMPING :
                        isBackflip ? PlayerState.BACKFLIPPING : PlayerState.JUMPING;

                    this.Rigidbody.YVelocity = newYVel;
                    this.StateMachine.State  = newState;

                    // TODO: Longjump feels weird.
                    if (isLongjump)
                    {
                        this.Rigidbody.XVelocity = this.Rigidbody.XVelocity /
                                                   PlayerConstants.UPRIGHT_MAX_FAST_XSPD *
                                                   PlayerConstants.LONGJUMP_MAX_XSPD;
                    }
                    else if (isBackflip)
                    {
                        // Instantly flip horizontal velocity.
                        var moveDirection = FinMath.Sign(this.Rigidbody.XVelocity);
                        this.Rigidbody.XVelocity =
                            -moveDirection * PlayerConstants.BACKFLIP_XSPD;
                    }
                }
                else if (this.StateMachine.State == PlayerState.WALL_SLIDING)
                {
                    this.StateMachine.State  = PlayerState.WALLJUMPING;
                    this.Rigidbody.YVelocity = PlayerConstants.JUMP_SPEED;
                    this.Rigidbody.XVelocity =
                        (this.StateMachine.WallSlidingOnLeft ? 1 : -1) *
                        PlayerConstants.WALL_SLIDING_XSPD;
                }
            }
            else if (this.scheduledJumpState_ == ScheduledState.STOPPING)
            {
                if (this.StateMachine.CanStallJumpingMomentum &&
                    this.Rigidbody.YVelocity < 0)
                {
                    this.StateMachine.State   = PlayerState.FALLING;
                    this.Rigidbody.YVelocity /= 2;
                }
            }
        }
Пример #2
0
        public void TestSign()
        {
            Assert.AreEqual(-1, FinMath.Sign(-1));
            Assert.AreEqual(1, FinMath.Sign(1));
            Assert.AreEqual(0, FinMath.Sign(0));

            Assert.AreEqual(-1, FinMath.Sign(-1d));
            Assert.AreEqual(1, FinMath.Sign(1d));
            Assert.AreEqual(0, FinMath.Sign(0d));
        }
Пример #3
0
        private void ProcessScheduledHorizontalMovement_()
        {
            var heldXAxis     = this.HeldXAxis;
            var isRunning     = this.IsRunning;
            var isTryingToRun = FloatMath.Abs(heldXAxis) > .5f;

            var heldXAxisSign = FinMath.Sign(heldXAxis);

            float?targetXVelocity = null;
            float xAcceleration   = 0;

            if (this.StateMachine.CanMoveUprightOnGround)
            {
                var maxGroundXVelocity =
                    isRunning
                ? PlayerConstants.UPRIGHT_MAX_FAST_XSPD
                : PlayerConstants.UPRIGHT_MAX_SLOW_XSPD;
                var groundAcceleration =
                    isTryingToRun
                ? PlayerConstants.GROUND_UPRIGHT_FAST_XACC
                : PlayerConstants.GROUND_UPRIGHT_SLOW_XACC;
                var reactionFraction =
                    heldXAxisSign == -FinMath.Sign(this.Rigidbody.XVelocity)
                ? PlayerConstants.GROUND_REACTION_FRAC
                : 1;

                targetXVelocity = maxGroundXVelocity * heldXAxis;
                xAcceleration   = groundAcceleration * reactionFraction * heldXAxisSign;

                // If holding a direction on the ground, we're either turning, running, or walking.
                if (heldXAxisSign != 0)
                {
                    this.StateMachine.State = reactionFraction != 1
                                        ? PlayerState.TURNING
                                        : isTryingToRun
                                            ? PlayerState.RUNNING
                                            : PlayerState.WALKING;
                }
                // If not holding a direction on the ground but velocity is not zero, we're stopping.
                else if (FinMath.Abs(this.Rigidbody.XVelocity) > .001)
                {
                    this.StateMachine.State = PlayerState.STOPPING;
                }
            }
            else if (this.StateMachine.CanMoveDuckedOnGround)
            {
                var maxGroundXVelocity =
                    isRunning
                ? PlayerConstants.DUCKED_MAX_FAST_XSPD
                : PlayerConstants.DUCKED_MAX_SLOW_XSPD;
                var groundAcceleration =
                    isTryingToRun
                ? PlayerConstants.GROUND_DUCKED_FAST_XACC
                : PlayerConstants.GROUND_DUCKED_SLOW_XACC;

                targetXVelocity = maxGroundXVelocity * heldXAxis;
                xAcceleration   = groundAcceleration * heldXAxisSign;

                // If holding a direction on the ground, we're either turning, running, or walking.
                if (heldXAxisSign != 0)
                {
                    this.StateMachine.State = PlayerState.DUCKWALKING;
                }
            }
            else if (this.StateMachine.CanMoveInAir)
            {
                var maxAirXVelocity =
                    isRunning
                ? PlayerConstants.UPRIGHT_MAX_FAST_XSPD
                : PlayerConstants.UPRIGHT_MAX_SLOW_XSPD;
                var airAcceleration =
                    isTryingToRun
                ? PlayerConstants.AIR_FAST_XACC
                : PlayerConstants.AIR_SLOW_XACC;

                targetXVelocity = maxAirXVelocity * heldXAxis;
                xAcceleration   = airAcceleration * heldXAxisSign;
            }

            this.Rigidbody.TargetXVelocity = targetXVelocity;
            this.Rigidbody.XAcceleration   = xAcceleration;
        }