public void Execute(Entity entity, int index,
                                DynamicBuffer <VelocityCurveBuffer> velocityCurveBuffer,
                                ref VelocityCurve velocityCurve,
                                ref SideScrollingCharacterController sideScrollingCharacterController,
                                [ReadOnly] ref SolidAgent solidAgent)
            {
                //Z axis is always 0
                velocityCurve.Z = VelocityCurveAxis.Zero();

                //Vertical Movement
                ComputeVerticalMovement(ref velocityCurve,
                                        ref sideScrollingCharacterController,
                                        ref solidAgent);

                //Horizontal Movement
                ComputeHorizontalMovement(ref velocityCurve,
                                          ref sideScrollingCharacterController,
                                          ref solidAgent);

                //Add this velocity curve to the character's velocity curve buffer
                velocityCurveBuffer.Add(new VelocityCurveBuffer
                {
                    VelocityCurveEntity = entity
                });
            }
            private void ComputeHorizontalMovement(ref VelocityCurve velocityCurve,
                                                   ref SideScrollingCharacterController sideScrollingCharacterController,
                                                   [ReadOnly] ref SolidAgent solidAgent)
            {
                //Check for hitting a wall you're moving towards
                if (((velocityCurve.X.CurrentVelocity < 0.0f) &&
                     (solidAgent.IsLeftWallCollided))

                    ||

                    ((velocityCurve.X.CurrentVelocity > 0.0f) &&
                     (solidAgent.IsRightWallCollided)))
                {
                    //We hit a wall.
                    //Reduce our current X velocity to zero
                    velocityCurve.X.CurrentVelocity = 0.0f;
                }

                //Move based on the movement input
                if (MovementInput.x == 0.0f)
                {
                    //The player isn't holding any direction.
                    //In the air, do nothing.
                    //On the ground, skid to a halt
                    if (solidAgent.IsGroundCollided)
                    {
                        velocityCurve.X = VelocityCurveAxis.Quadratic(velocityCurve.X.CurrentVelocity,
                                                                      velocityCurve.X.CurrentVelocity < 0.0f,
                                                                      sideScrollingCharacterController.WalkingAbsoluteDeceleration,
                                                                      0.0f);
                    }
                }
                else
                {
                    //Moving left and right
                    //Check if we're moving with the momentum or against it
                    if (((MovementInput.x > 0.0f) &&
                         (velocityCurve.X.CurrentVelocity < 0.0f))

                        ||

                        ((MovementInput.x < 0.0f) &&
                         (velocityCurve.X.CurrentVelocity > 0.0f)))
                    {
                        //The player is skidding back towards zero velocity
                        velocityCurve.X = VelocityCurveAxis.Quadratic(velocityCurve.X.CurrentVelocity,
                                                                      MovementInput.x > 0.0f,
                                                                      sideScrollingCharacterController.SkiddingAbsoluteDeceleration,
                                                                      0.0f);
                    }
                    else
                    {
                        //The player is accelerating towards maximum velocity
                        velocityCurve.X = VelocityCurveAxis.Quadratic(velocityCurve.X.CurrentVelocity,
                                                                      MovementInput.x > 0.0f,
                                                                      sideScrollingCharacterController.WalkingAbsoluteAcceleration,
                                                                      sideScrollingCharacterController.WalkingAbsoluteMaximumVelocity);
                    }
                }
            }
            public void Execute(Entity entity, int index,
                                DynamicBuffer <VelocityCurveBuffer> velocityCurveBuffer,
                                ref VelocityCurve velocityCurve, ref MovingPlatformController movingPlatformController, ref Translation translation)
            {
                //The platform never moves on the Z axis
                velocityCurve.Z = VelocityCurveAxis.Zero();

                //Check if we've reached our current destination
                float3 destination           = movingPlatformController.IsMovingTowardsA ? movingPlatformController.PositionA : movingPlatformController.PositionB;
                float  distanceToDestination = math.abs(math.distance(translation.Value, destination));

                if (distanceToDestination == 0.0f)
                {
                    //Reverse the destination
                    movingPlatformController.IsMovingTowardsA = !movingPlatformController.IsMovingTowardsA;

                    destination           = movingPlatformController.IsMovingTowardsA ? movingPlatformController.PositionA : movingPlatformController.PositionB;
                    distanceToDestination = math.abs(math.distance(translation.Value, destination));
                }



                //Move towards the destination, being carefull not to move past it
                float distanceToMoveThisTimeStep        = DeltaTime * movingPlatformController.AbsoluteVelocity;
                float distanceToMoveThisTimeStepSquared = distanceToMoveThisTimeStep * distanceToMoveThisTimeStep;

                float3 distanceAvailableToMove        = destination - translation.Value;
                float  distanceAvailableToMoveSquared = math.lengthsq(distanceAvailableToMove);

                float3 directionToMove = math.normalize(distanceAvailableToMove);

                float3 linearVelocity = new float3();

                if (distanceAvailableToMoveSquared < distanceToMoveThisTimeStepSquared)
                {
                    linearVelocity = distanceAvailableToMove / DeltaTime;
                }
                else
                {
                    linearVelocity = directionToMove * movingPlatformController.AbsoluteVelocity;
                }


                //Set the velocity curve
                velocityCurve.X = VelocityCurveAxis.Linear(linearVelocity.x > 0.0f,
                                                           math.abs(linearVelocity.x));

                velocityCurve.Y = VelocityCurveAxis.Linear(linearVelocity.y > 0.0f,
                                                           math.abs(linearVelocity.y));


                //Add this velocity curve to the moving platform's velocity curve buffer
                velocityCurveBuffer.Add(new VelocityCurveBuffer
                {
                    VelocityCurveEntity = entity
                });
            }
        public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
        {
            var componentData = new JeffreyDufseth.VelocityCurveManagement.VelocityCurve
            {
                X = VelocityCurveAxis.Zero(),
                Y = VelocityCurveAxis.Zero(),
                Z = VelocityCurveAxis.Zero()
            };

            dstManager.AddComponentData(entity, componentData);
        }
            public void Execute(ref VelocityCurve velocityCurve, ref WindController windController)
            {
                //The wind never blows on the Z axis
                velocityCurve.Z = VelocityCurveAxis.Zero();

                //Turn the wind on and off
                windController.ElapsedTime += DeltaTime;

                if (windController.IsBlowing &&
                    (windController.ElapsedTime > windController.OnTime))
                {
                    //Turn the wind off.
                    windController.IsBlowing   = false;
                    windController.ElapsedTime = 0.0f;

                    velocityCurve.X = VelocityCurveAxis.Quadratic(velocityCurve.X.CurrentVelocity,
                                                                  windController.WindDirection.x < 0.0f,
                                                                  windController.WindAbsoluteDeceleration * math.abs(windController.WindDirection.x),
                                                                  0.0f);

                    velocityCurve.Y = VelocityCurveAxis.Quadratic(velocityCurve.Y.CurrentVelocity,
                                                                  windController.WindDirection.y < 0.0f,
                                                                  windController.WindAbsoluteDeceleration * math.abs(windController.WindDirection.y),
                                                                  0.0f);
                }
                else if (!windController.IsBlowing &&
                         (windController.ElapsedTime > windController.OffTime))
                {
                    //Turn the wind on.
                    windController.IsBlowing   = true;
                    windController.ElapsedTime = 0.0f;

                    velocityCurve.X = VelocityCurveAxis.Quadratic(velocityCurve.X.CurrentVelocity,
                                                                  windController.WindDirection.x > 0.0f,
                                                                  windController.WindAbsoluteDeceleration * math.abs(windController.WindDirection.x),
                                                                  windController.WindAbsoluteMaximumVelocity * math.abs(windController.WindDirection.x));

                    velocityCurve.Y = VelocityCurveAxis.Quadratic(velocityCurve.Y.CurrentVelocity,
                                                                  windController.WindDirection.y > 0.0f,
                                                                  windController.WindAbsoluteDeceleration * math.abs(windController.WindDirection.y),
                                                                  windController.WindAbsoluteMaximumVelocity * math.abs(windController.WindDirection.y));
                }
            }
Exemplo n.º 6
0
            private VelocityCurveAxis CalculateLinearVelocity(VelocityCurveAxis velocityCurveAxis, float timeStep)
            {
                float newVelocity = 0.0f;
                float direction   = velocityCurveAxis.IsPositive ? 1.0f : -1.0f;

                switch (velocityCurveAxis.Curve)
                {
                case VelocityCurveTypes.Zero:
                {
                    newVelocity = 0.0f;
                }
                break;

                case VelocityCurveTypes.Linear:
                {
                    newVelocity = velocityCurveAxis.AbsoluteVelocity * direction;
                }
                break;

                case VelocityCurveTypes.Quadratic:
                {
                    newVelocity = velocityCurveAxis.CurrentVelocity + (velocityCurveAxis.AbsoluteAcceleration * direction * timeStep);
                }
                break;

                case VelocityCurveTypes.LinearThenQuadratic:
                {
                    //Play linear for a bit, then switch over to
                    //quadratic, using any remaining time on it
                    if (velocityCurveAxis.DelayTimeRemaining > 0.0f)
                    {
                        if (velocityCurveAxis.DelayTimeRemaining >= timeStep)
                        {
                            velocityCurveAxis.DelayTimeRemaining = velocityCurveAxis.DelayTimeRemaining - timeStep;

                            //Stay linear
                            newVelocity = velocityCurveAxis.AbsoluteVelocity * direction;
                        }
                        else
                        {
                            //Quadradic with remaining time
                            float remainingTime = timeStep - velocityCurveAxis.DelayTimeRemaining;
                            velocityCurveAxis.DelayTimeRemaining = 0.0f;

                            newVelocity = velocityCurveAxis.CurrentVelocity + (velocityCurveAxis.AbsoluteAcceleration * direction * remainingTime);
                        }
                    }
                    else
                    {
                        //Full quadradic
                        newVelocity = velocityCurveAxis.CurrentVelocity + (velocityCurveAxis.AbsoluteAcceleration * direction * timeStep);
                    }
                }
                break;
                }


                //Apply limits
                if (velocityCurveAxis.IsPositive)
                {
                    newVelocity = math.min(newVelocity, velocityCurveAxis.MaximumAbsoluteVelocity * direction);
                }
                else
                {
                    newVelocity = math.max(newVelocity, velocityCurveAxis.MaximumAbsoluteVelocity * direction);
                }

                velocityCurveAxis.CurrentVelocity = newVelocity;

                return(velocityCurveAxis);
            }
            private void ComputeVerticalMovement(ref VelocityCurve velocityCurve,
                                                 ref SideScrollingCharacterController sideScrollingCharacterController,
                                                 [ReadOnly] ref SolidAgent solidAgent)
            {
                //Check if we're grounded
                if (solidAgent.IsGroundCollided)
                {
                    //We're grounded.
                    //Reset jumping and falling flags
                    sideScrollingCharacterController.IsJumping  = false;
                    sideScrollingCharacterController.IsFalling  = false;
                    sideScrollingCharacterController.IsJumpHeld = false;

                    //Check if we want to jump
                    if (IsJumpPressedThisFrame)
                    {
                        sideScrollingCharacterController.IsJumping  = true;
                        sideScrollingCharacterController.IsJumpHeld = true;

                        velocityCurve.Y = VelocityCurveAxis.Quadratic(sideScrollingCharacterController.JumpAbsoluteVelocity,
                                                                      false,
                                                                      sideScrollingCharacterController.JumpAbsoluteDeceleration,
                                                                      sideScrollingCharacterController.TerminalVelocity);
                    }
                    else
                    {
                        //While grounded, continue to push down on the ground an amount
                        //equal to a single step off acceleration
                        velocityCurve.Y = VelocityCurveAxis.Linear(false,
                                                                   sideScrollingCharacterController.FallingAbsoluteAcceleration * DeltaTime);
                    }
                }
                else
                {
                    //Check for hitting your head on the ceiling
                    if ((velocityCurve.Y.CurrentVelocity > 0.0f) &&
                        (solidAgent.IsCeilingCollided))
                    {
                        //Cancel the jump and reduce velocity to zero
                        velocityCurve.Y.CurrentVelocity             = 0.0f;
                        sideScrollingCharacterController.IsJumpHeld = false;
                    }

                    //Differentiate between jumping and falling
                    if (!IsJumpPressedThisFrame)
                    {
                        sideScrollingCharacterController.IsJumpHeld = false;
                    }

                    //Did the player let go of jump?
                    if (sideScrollingCharacterController.IsJumping && !sideScrollingCharacterController.IsJumpHeld)
                    {
                        sideScrollingCharacterController.IsJumping = false;
                    }

                    //Check if we should start falling
                    if (!sideScrollingCharacterController.IsJumping && !sideScrollingCharacterController.IsFalling)
                    {
                        //Start Falling
                        sideScrollingCharacterController.IsFalling = true;

                        velocityCurve.Y = VelocityCurveAxis.Quadratic(velocityCurve.Y.CurrentVelocity,
                                                                      false,
                                                                      sideScrollingCharacterController.FallingAbsoluteAcceleration,
                                                                      sideScrollingCharacterController.TerminalVelocity);
                    }
                }
            }