feat: Ball Speed Up Over Time

This commit is contained in:
Syntriax 2024-01-29 12:34:27 +03:00
parent 0b2158cc7b
commit ad60412d5f
3 changed files with 15 additions and 2 deletions

View File

@ -9,6 +9,7 @@ public class MovementBallBehaviour : BehaviourOverride
{ {
public Vector2D StartDirection { get; private set; } = Vector2D.Zero; public Vector2D StartDirection { get; private set; } = Vector2D.Zero;
public float Speed { get; set; } = 500f; public float Speed { get; set; } = 500f;
public float SpeedUpMultiplier { get; set; } = .0125f;
private IRigidBody2D rigidBody = null!; private IRigidBody2D rigidBody = null!;
@ -32,6 +33,15 @@ public class MovementBallBehaviour : BehaviourOverride
rigidBody = foundRigidBody; rigidBody = foundRigidBody;
} }
protected override void OnUpdate()
{
if (rigidBody.Velocity.MagnitudeSquared <= 0.01f)
return;
Vector2D speedUp = rigidBody.Velocity.Normalized * (float)Time.Elapsed.TotalMilliseconds;
rigidBody.Velocity += speedUp * SpeedUpMultiplier;
}
private void OnCollisionDetected(ICollider2D collider2D, CollisionDetectionInformation information) private void OnCollisionDetected(ICollider2D collider2D, CollisionDetectionInformation information)
{ {
rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal); rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal);

View File

@ -39,7 +39,9 @@ public class PongScoreboard(int WinScore) : BehaviourOverride
private void CheckFinish() private void CheckFinish()
{ {
if (ScoreSum == WinScore) int halfwayScore = (int)(WinScore * .5f);
if (ScoreLeft > halfwayScore || ScoreRight > halfwayScore)
OnFinished?.Invoke(); OnFinished?.Invoke();
} }
} }

View File

@ -169,6 +169,7 @@ public class Game1 : Game
pongScoreboard.OnScored += () => pongScoreboard.OnScored += () =>
{ {
gameObjectBall.Transform.Position = Vector2D.Zero; gameObjectBall.Transform.Position = Vector2D.Zero;
rigidBodyBall.Velocity = rigidBodyBall.Velocity.Normalized * movementBallBehaviour.Speed;
textBehaviourLeft.Text = pongScoreboard.ScoreLeft.ToString(); textBehaviourLeft.Text = pongScoreboard.ScoreLeft.ToString();
textBehaviourRight.Text = pongScoreboard.ScoreRight.ToString(); textBehaviourRight.Text = pongScoreboard.ScoreRight.ToString();
}; };
@ -182,7 +183,7 @@ public class Game1 : Game
{ {
gameObjectBall.StateEnable.Enabled = true; gameObjectBall.StateEnable.Enabled = true;
rigidBodyBall.BehaviourController.GameObject.Transform.Position = Vector2D.Zero; rigidBodyBall.BehaviourController.GameObject.Transform.Position = Vector2D.Zero;
rigidBodyBall.Velocity = Vector2D.One * movementBallBehaviour.Speed; rigidBodyBall.Velocity = Vector2D.One.Normalized * movementBallBehaviour.Speed;
textBehaviourLeft.Text = pongScoreboard.ScoreLeft.ToString(); textBehaviourLeft.Text = pongScoreboard.ScoreLeft.ToString();
textBehaviourRight.Text = pongScoreboard.ScoreRight.ToString(); textBehaviourRight.Text = pongScoreboard.ScoreRight.ToString();
}; };