2022-11-04 01:46:48 +01:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
|
|
|
|
namespace PongGame.Hubs;
|
2022-11-04 00:13:00 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Pong game state saving the positions of the paddles and the ball.
|
|
|
|
|
/// The Pong board has aspect ratio 1:2 with height 500 and width 1000.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public struct PongGameState {
|
2022-11-04 01:46:48 +01:00
|
|
|
|
public const float HEIGHT = 500.0f;
|
|
|
|
|
public const float WIDTH = 2 * HEIGHT;
|
2022-11-04 04:05:20 +01:00
|
|
|
|
public const float PADDLE1_OFFSET = WIDTH / 20;
|
|
|
|
|
public const float PADDLE2_OFFSET = WIDTH - PADDLE1_OFFSET;
|
2022-11-04 01:46:48 +01:00
|
|
|
|
|
2022-11-04 00:13:00 +01:00
|
|
|
|
public static readonly PongGameState Initial = new() {
|
2022-11-04 01:46:48 +01:00
|
|
|
|
BallState = PongBallState.Initial,
|
|
|
|
|
Paddle1 = PongPaddleState.Initial,
|
|
|
|
|
Paddle2 = PongPaddleState.Initial,
|
2022-11-04 13:00:54 +01:00
|
|
|
|
Status = GameStatus.WaitingForPlayers
|
2022-11-04 00:13:00 +01:00
|
|
|
|
};
|
2022-11-04 01:46:48 +01:00
|
|
|
|
|
|
|
|
|
public PongPaddleState Paddle1;
|
|
|
|
|
public PongPaddleState Paddle2;
|
|
|
|
|
public PongBallState BallState;
|
|
|
|
|
public GameStatus Status;
|
|
|
|
|
|
|
|
|
|
public static void Update(ref PongGameState state) {
|
|
|
|
|
if (state.Status is not GameStatus.InProgress) return;
|
|
|
|
|
|
|
|
|
|
PongPaddleState.Update(ref state.Paddle1);
|
|
|
|
|
PongPaddleState.Update(ref state.Paddle2);
|
|
|
|
|
PongBallState.Update(ref state.BallState);
|
2022-11-04 13:00:54 +01:00
|
|
|
|
Collide(in state.Paddle1, ref state.BallState, true);
|
|
|
|
|
Collide(in state.Paddle2, ref state.BallState, false);
|
|
|
|
|
|
|
|
|
|
switch (state.BallState.Pos.X) {
|
|
|
|
|
case > WIDTH:
|
|
|
|
|
AnnounceScore(ref state, ref state.Paddle1);
|
|
|
|
|
return;
|
|
|
|
|
case < 0:
|
|
|
|
|
AnnounceScore(ref state, ref state.Paddle2);
|
2022-11-04 04:05:20 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-11-04 13:00:54 +01:00
|
|
|
|
}
|
2022-11-04 01:46:48 +01:00
|
|
|
|
|
2022-11-04 13:00:54 +01:00
|
|
|
|
private static void AnnounceScore(ref PongGameState state, ref PongPaddleState winner) {
|
|
|
|
|
winner.Score++;
|
|
|
|
|
state.Paddle1.ResetPosition();
|
|
|
|
|
state.Paddle2.ResetPosition();
|
|
|
|
|
state.BallState.ResetPosition();
|
2022-11-04 01:46:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Collide(in PongPaddleState paddle, ref PongBallState ballState, bool left) {
|
|
|
|
|
var paddleX = left ? PADDLE1_OFFSET : PADDLE2_OFFSET;
|
|
|
|
|
var intersection = RectangleF.Intersect(paddle.GetCollider(paddleX), ballState.GetCollider());
|
|
|
|
|
if (intersection.IsEmpty) return;
|
|
|
|
|
|
|
|
|
|
// TODO: continuous collision
|
2022-11-04 04:05:20 +01:00
|
|
|
|
var ratio = (ballState.Pos.Y - paddle.Height + PongPaddleState.PADDLE_HALF_LENGTH) / PongPaddleState.PADDLE_LENGTH;
|
|
|
|
|
|
|
|
|
|
// TODO: lesser angles
|
2022-11-04 01:46:48 +01:00
|
|
|
|
var upAngle = left ? MathF.PI * 3 / 8 : MathF.PI * 5 / 8;
|
2022-11-04 04:05:20 +01:00
|
|
|
|
var downAngle = left ? -MathF.PI * 3 / 8 : MathF.PI * 11 / 8;
|
2022-11-04 01:46:48 +01:00
|
|
|
|
|
2022-11-04 04:05:20 +01:00
|
|
|
|
// TODO: reflect ball on surface instead of launching
|
2022-11-04 01:46:48 +01:00
|
|
|
|
ballState.BallAngle = ratio * downAngle + (1 - ratio) * upAngle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public struct PongBallState {
|
2022-11-04 04:05:20 +01:00
|
|
|
|
public const float BALL_SPEED = 2 * BALL_RADIUS;
|
|
|
|
|
public const float BALL_RADIUS = HEIGHT / 125;
|
2022-11-04 01:46:48 +01:00
|
|
|
|
|
|
|
|
|
public static readonly PongBallState Initial = new() {
|
|
|
|
|
BallAngle = 0.0f,
|
2022-11-04 04:05:20 +01:00
|
|
|
|
Pos = new() {
|
|
|
|
|
X = WIDTH / 2,
|
|
|
|
|
Y = HEIGHT / 2
|
|
|
|
|
}
|
2022-11-04 00:13:00 +01:00
|
|
|
|
};
|
2022-11-04 01:46:48 +01:00
|
|
|
|
|
2022-11-04 04:05:20 +01:00
|
|
|
|
public PointF Pos;
|
2022-11-04 01:46:48 +01:00
|
|
|
|
public float BallAngle;
|
|
|
|
|
|
|
|
|
|
public static void Update(ref PongBallState state) {
|
|
|
|
|
var (dy, dx) = MathF.SinCos(state.BallAngle);
|
2022-11-04 04:05:20 +01:00
|
|
|
|
state.Pos.X += BALL_SPEED * dx;
|
|
|
|
|
state.Pos.Y -= BALL_SPEED * dy;
|
|
|
|
|
|
2022-11-04 12:04:05 +01:00
|
|
|
|
// If reflected from lower or upper border calculate continuous reflection
|
|
|
|
|
// This prevents the ball from temporarily glitching into paddles
|
|
|
|
|
if (state.Pos.Y < BALL_RADIUS) {
|
|
|
|
|
state.Pos.Y = 2 * BALL_RADIUS - state.Pos.Y;
|
2022-11-04 04:05:20 +01:00
|
|
|
|
state.BallAngle = 2 * MathF.PI - state.BallAngle;
|
2022-11-04 12:04:05 +01:00
|
|
|
|
} else if (state.Pos.Y > HEIGHT - BALL_RADIUS) {
|
|
|
|
|
state.Pos.Y = 2 * (HEIGHT - BALL_RADIUS) - state.Pos.Y;
|
|
|
|
|
state.BallAngle = 2 * MathF.PI - state.BallAngle;
|
|
|
|
|
}
|
2022-11-04 01:46:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-04 04:05:20 +01:00
|
|
|
|
public RectangleF GetCollider() => new(Pos.X - BALL_RADIUS, Pos.Y - BALL_RADIUS, 2 * BALL_RADIUS, 2 * BALL_RADIUS);
|
2022-11-04 13:00:54 +01:00
|
|
|
|
|
|
|
|
|
public void ResetPosition() {
|
|
|
|
|
BallAngle = 0.0f;
|
|
|
|
|
Pos.X = WIDTH / 2;
|
|
|
|
|
Pos.Y = HEIGHT / 2;
|
|
|
|
|
}
|
2022-11-04 01:46:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public struct PongPaddleState {
|
|
|
|
|
public const float PADDLE_LENGTH = HEIGHT / 10;
|
|
|
|
|
public const float PADDLE_HALF_LENGTH = PADDLE_LENGTH / 2;
|
2022-11-04 04:05:20 +01:00
|
|
|
|
public const float PADDLE_WIDTH = PADDLE_LENGTH / 5;
|
2022-11-04 01:46:48 +01:00
|
|
|
|
public const float PADDLE_SPEED = 8;
|
2022-11-04 13:00:54 +01:00
|
|
|
|
private const float PADDLE_INITIAL_HEIGHT = HEIGHT / 2;
|
2022-11-04 01:46:48 +01:00
|
|
|
|
|
|
|
|
|
public static readonly PongPaddleState Initial = new() {
|
2022-11-04 13:00:54 +01:00
|
|
|
|
Score = 0,
|
2022-11-04 01:46:48 +01:00
|
|
|
|
Direction = PongPaddleDirection.Stop,
|
2022-11-04 13:00:54 +01:00
|
|
|
|
Height = PADDLE_INITIAL_HEIGHT
|
2022-11-04 01:46:48 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public float Height;
|
|
|
|
|
public PongPaddleDirection Direction;
|
2022-11-04 13:00:54 +01:00
|
|
|
|
public int Score;
|
2022-11-04 01:46:48 +01:00
|
|
|
|
|
|
|
|
|
public static void Update(ref PongPaddleState state) {
|
2022-11-04 13:00:54 +01:00
|
|
|
|
state.Height = Math.Clamp(state.Height + ((int)state.Direction) * PADDLE_SPEED, PADDLE_HALF_LENGTH, HEIGHT - PADDLE_HALF_LENGTH);
|
2022-11-04 01:46:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-04 04:05:20 +01:00
|
|
|
|
public RectangleF GetCollider(float x) => new(x - PADDLE_WIDTH / 2, Height - PADDLE_HALF_LENGTH, PADDLE_WIDTH, PADDLE_LENGTH);
|
2022-11-04 13:00:54 +01:00
|
|
|
|
|
|
|
|
|
public void ResetPosition() {
|
|
|
|
|
Direction = PongPaddleDirection.Stop;
|
|
|
|
|
Height = PADDLE_INITIAL_HEIGHT;
|
|
|
|
|
}
|
2022-11-04 00:13:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|