public void Serve(Racquet racquet, float serviceSpeed) { var xVarSpeed = (((float)_random.NextDouble() - .5f) * 2f) * (20f / 100f); xVarSpeed += 1f; var yVarSpeed = (((float)_random.NextDouble() - .5f) * 2f) * (20f / 100f); yVarSpeed += 1f; var xSpeed = serviceSpeed * xVarSpeed; var ySpeed = serviceSpeed * yVarSpeed; if (racquet.Location.X > 400) { xSpeed *= -1f; } if (racquet.Speed.Y == 0f) { ySpeed *= (_random.NextDouble() > 0.5) ? 1f : -1f; } else if (racquet.Speed.Y < 0f) { ySpeed *= -1; } Speed = new PointF(xSpeed, ySpeed); }
private bool PlayerCollision(Racquet racquet, ref PointF nextLocation, ref PointF nextSpeed) { var xRect = racquet.Location.X - (racquet.Size.Width / 2f); var yRect = racquet.Location.Y - (racquet.Size.Height / 2f); var rectangle = new RectangleF(new PointF(xRect, yRect), racquet.Size); return(PlayerHorizontalCollision(ref nextLocation, ref nextSpeed, rectangle) || PlayerVerticalCollision(ref nextLocation, ref nextSpeed, rectangle)); }
private void Spin(Racquet racquet, ref PointF nextSpeed) { var xVarSpeed = (((float)_random.NextDouble() - .5f) * 2f) * (10f / 100f); var yVarSpeed = (float)_random.NextDouble() * (20f / 100f); if (racquet.Speed.Y < 0) { yVarSpeed *= -1; } var xSpeed = nextSpeed.X * (1 + xVarSpeed); var ySpeed = nextSpeed.Y * (1 + yVarSpeed); nextSpeed = new PointF(xSpeed, ySpeed); }
public void PrepareToServe(Racquet racquet) { float x; if (racquet.Location.X > 400) { x = racquet.Location.X - Radius - (racquet.Size.Width / 2f); } else { x = racquet.Location.X + Radius + (racquet.Size.Width / 2f); } var y = racquet.Location.Y; Speed = new PointF(); Location = new PointF(x, y); }
public void Update(Racquet left, Racquet right, float deltaTime) { Accelerate(out var nextLocation, out var nextSpeed, deltaTime); TopDownCollision(ref nextLocation, ref nextSpeed); if (Speed.X < 0) { if (PlayerCollision(left, ref nextLocation, ref nextSpeed)) { Spin(left, ref nextSpeed); } } else { if (PlayerCollision(right, ref nextLocation, ref nextSpeed)) { Spin(right, ref nextSpeed); } } Location = nextLocation; Speed = nextSpeed; }