示例#1
0
        public void TestAbs()
        {
            Assert.AreEqual(5, FinMath.Abs(5));
            Assert.AreEqual(5, FinMath.Abs(-5));

            Assert.AreEqual(5d, FinMath.Abs(5d));
            Assert.AreEqual(5d, FinMath.Abs(-5d));
        }
示例#2
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;
                }
            }
        }
示例#3
0
        private void ProcessInputs_(ProcessInputsEvent _)
        {
            this.motor_.ClearScheduled();

            var primaryAnalogStick = this.gamepad_[AnalogStickType.PRIMARY];
            var heldAxes           = primaryAnalogStick.RawAxes;
            //var runButton = this.gamepad_[FaceButtonType.SECONDARY];
            var isRunning = true;

            var heldX = FinMath.Abs(heldAxes.X) > GamepadConstants.DEADZONE
                      ? heldAxes.X
                      : 0;

            this.motor_.ScheduleMoveAttempt(heldX, isRunning);

            var heldY             = heldAxes.Y;
            var minHeldDuckAmount = -.75f;

            if (this.stateMachine_.CanDuck)
            {
                var maxHeldDuckAmount = -.25f;
                this.toDuckFraction_ = 1 -
                                       (FloatMath.Clamp(minHeldDuckAmount,
                                                        heldY,
                                                        maxHeldDuckAmount) -
                                        minHeldDuckAmount) /
                                       (maxHeldDuckAmount - minHeldDuckAmount);
            }
            else if (this.stateMachine_.IsDucked)
            {
                this.toDuckFraction_ = 1;
            }
            else
            {
                this.toDuckFraction_ = 0;
            }

            this.duckFraction_ += (this.toDuckFraction_ - this.duckFraction_) / 2;

            if (heldY <= minHeldDuckAmount)
            {
                this.motor_.ScheduleDuckStartAttempt();
            }
            else
            {
                this.motor_.ScheduleDuckStopAttempt();
            }

            var jumpButton = this.gamepad_[FaceButtonType.PRIMARY];

            if (jumpButton.IsPressed)
            {
                this.motor_.ScheduleJumpStartAttempt();
            }
            else if (jumpButton.IsReleased)
            {
                this.motor_.ScheduleJumpStopAttempt();
            }

            this.motor_.ProcessInputs();
        }
示例#4
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;
        }
示例#5
0
        private void TickPhysics_(TickPhysicsEvent _)
        {
            var heldX     = this.motor_.HeldXAxis;
            var isRunning = this.motor_.IsRunning;

            // TODO: Wrap these in a struct.
            this.rigidbody_.MaxXSpeed = float.MaxValue;
            var duckedMaxXSpd = isRunning
                              ? PlayerConstants.DUCKED_MAX_FAST_XSPD
                              : PlayerConstants.DUCKED_MAX_SLOW_XSPD;

            if (this.stateMachine_.IsOnGround)
            {
                if (this.stateMachine_.State == PlayerState.SLIDING)
                {
                    this.rigidbody_.Friction = PlayerConstants.GROUND_SLIDING_FRICTION;
                }
                else
                {
                    this.rigidbody_.Friction = PlayerConstants.GROUND_FRICTION;
                }
            }
            else
            {
                this.rigidbody_.Friction = PlayerConstants.AIR_FRICTION;
            }

            var isWide = false; //this.stateMachine_.State == PlayerState.SLIDING;
            var isTall = !this.stateMachine_.IsDucked;

            // TODO: Might be a problem, this should probably just be a visible thing.
            // TODO: Might be a problem, this should probably just be a visible thing.
            this.playerRigidbody_.Width =
                (.8f * PlayerConstants.HSIZE) * (1 - this.duckFraction_) +
                PlayerConstants.HSIZE * this.duckFraction_;
            this.playerRigidbody_.Height =
                PlayerConstants.VSIZE * (1 - this.duckFraction_) +
                PlayerConstants.HSIZE * this.duckFraction_;
            //this.playerRigidbody_.Width =
            // !isWide ? PlayerConstants.HSIZE : PlayerConstants.VSIZE;
            //
            //this.playerRigidbody_.Height =
            //    isTall ? PlayerConstants.VSIZE : PlayerConstants.HSIZE;

            this.rigidbody_.TickPhysics(3);

            var(xVelocity, yVelocity) = this.rigidbody_.Velocity;

            // When comes to a stop, start standing.
            if (xVelocity == 0)
            {
                if (this.stateMachine_.IsMovingUprightOnGround)
                {
                    this.stateMachine_.State = PlayerState.STANDING;
                }
                else if (this.stateMachine_.IsMovingDuckedOnGround)
                {
                    this.stateMachine_.State = PlayerState.DUCKING;
                }
            }

            if (this.stateMachine_.State == PlayerState.SLIDING &&
                FinMath.Abs(heldX) > GamepadConstants.DEADZONE &&
                FinMath.Abs(xVelocity) <= duckedMaxXSpd)
            {
                this.stateMachine_.State = PlayerState.DUCKWALKING;
            }

            // When transitions to downward y velocity in air after a jump, start
            // falling.
            if (this.stateMachine_.IsMovingUpwardInAirAndCanFall && yVelocity > 0)
            {
                this.stateMachine_.State = PlayerState.FALLING;
            }

            if (this.stateMachine_.State == PlayerState.INITIALLY_FALLING_OFF_LEDGE &&
                --this.initiallyFallingTimer_ <= -1)
            {
                this.stateMachine_.State = PlayerState.FALLING;
            }
        }