Skip to content

Commit

Permalink
renamed player to paddle
Browse files Browse the repository at this point in the history
  • Loading branch information
subfuzion committed Aug 11, 2022
1 parent 09576f0 commit f449690
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
33 changes: 16 additions & 17 deletions src/client/pong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class PongApp extends P5App {
// game objects
table: Table;
ball: Ball;
player1: Paddle;
player2: Paddle;
paddle1: Paddle;
paddle2: Paddle;

// Maps message type to a [message class, message handler].
mapper = new Map<string, [{ new(data: object): Message; }, (m: any) => void]>;
Expand All @@ -57,28 +57,28 @@ export class PongApp extends P5App {

const ball = new Ball(250, 100);

// TODO: this is a temporary hack for player #1; player will use actual
// TODO: this is a temporary hack for player1; player1 will use actual
// cursor keys (currently assigned to player2).
const player1 = new Paddle(30, 250);
player1.upKey = 65; // up: 'a'
player1.downKey = 90; // down: 'z'
const paddle1 = new Paddle(30, 250);
paddle1.upKey = 65; // up: 'a'
paddle1.downKey = 90; // down: 'z'

// TODO: player2 is a hack right now until player matching works (and maybe
// single player mode).
const player2 = new Paddle(table.width - 50, 250);
player2.upKey = p5.UP_ARROW;
player2.downKey = p5.DOWN_ARROW;
const paddle2 = new Paddle(table.width - 50, 250);
paddle2.upKey = p5.UP_ARROW;
paddle2.downKey = p5.DOWN_ARROW;

table.add(ball, player1, player2);
table.add(ball, paddle1, paddle2);

// TODO: need actual player id assigned from server.
player1.onchange(y => { this.client!.send({id: 0, y: y}); });
player2.onchange(y => { this.client!.send({id: 1, y: y}); });
paddle1.onchange(y => { this.client!.send({id: 0, y: y}); });
paddle2.onchange(y => { this.client!.send({id: 1, y: y}); });

this.table = table;
this.ball = ball;
this.player1 = player1;
this.player2 = player2;
this.paddle1 = paddle1;
this.paddle2 = paddle2;

// Message handling.
//
Expand All @@ -93,7 +93,6 @@ export class PongApp extends P5App {
this.mapper.set(
"WebSocketError",
[WebSocketError, this.onWebSocketError.bind(this)]);

}

override setup() {
Expand Down Expand Up @@ -130,8 +129,8 @@ export class PongApp extends P5App {
this.ball.y = m.y;
this.ball.vx = m.vx;
this.ball.vy = m.vy;
this.player1.y = m.player1y;
this.player2.y = m.player2y;
this.paddle1.y = m.paddle1y;
this.paddle2.y = m.paddle2y;
}

private onStatsUpdate(m: StatsUpdate): void {
Expand Down
7 changes: 4 additions & 3 deletions src/common/pong/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export class Message {
* @param data An object with properties to merge into this instance.
* Be careful: this is a shotgun, don't blow your foot off.
* There is no validation that data properties belong in
* the instance.
* the instance. Be especially careful when renaming fields
* to change all usages.
* @protected
*/
protected merge(data: object) {
Expand Down Expand Up @@ -105,8 +106,8 @@ export class Update extends ServerMessage {
y = 0;
vx = 0;
vy = 0;
player1y = 0;
player2y = 0;
paddle1y = 0;
paddle2y = 0;

constructor(data: object) {
super(data);
Expand Down
48 changes: 24 additions & 24 deletions src/server/pong/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class PongEngine {
screenHeight: number;
table: Table;
ball: Ball;
player1: Paddle;
player2: Paddle;
paddle1: Paddle;
paddle2: Paddle;

cb: ((e: Message) => void) | null = null;

Expand All @@ -37,15 +37,15 @@ export class PongEngine {
this.ball.vx = 12;
this.ball.vy = 2;

this.player1 = new Paddle(30, 250);
this.player1.y = (this.table.height - this.player1.height) / 2;
this.player1.vy = 10;
this.paddle1 = new Paddle(30, 250);
this.paddle1.y = (this.table.height - this.paddle1.height) / 2;
this.paddle1.vy = 10;

this.player2 = new Paddle(this.table.width - 50, 250);
this.player2.y = (this.table.height - this.player2.height) / 2;
this.player2.vy = 10;
this.paddle2 = new Paddle(this.table.width - 50, 250);
this.paddle2.y = (this.table.height - this.paddle2.height) / 2;
this.paddle2.vy = 10;

this.table.add(this.ball, this.player1, this.player2);
this.table.add(this.ball, this.paddle1, this.paddle2);
}

onStateChange(cb: (e: Message) => void): void {
Expand All @@ -62,13 +62,13 @@ export class PongEngine {
movePaddle(id: number, y: number): void {
switch (id) {
case 0:
this.player1.y += this.player1.vy * y;
this.paddle1.y += this.paddle1.vy * y;
break;
case 1:
this.player2.y += this.player2.vy * y;
this.paddle2.y += this.paddle2.vy * y;
break;
default:
console.log(`error: invalid player id: ${id}`);
console.log(`error: invalid paddle id: ${id}`);
return;
}
}
Expand All @@ -85,8 +85,8 @@ export class PongEngine {
private update() {
const table = this.table;
const ball = this.ball;
const player1 = this.player1;
const player2 = this.player2;
const paddle1 = this.paddle1;
const paddle2 = this.paddle2;

// Ball bounces off the top and bottom sides of the table.
if (ball.y > table.height - 5 || ball.y < 5) {
Expand All @@ -95,19 +95,19 @@ export class PongEngine {

// If ball bounces off a paddle, reverse direction.
let reverse = false;
// if ball bounces off player 1 paddle
// if ball bounces off paddle1
if (
ball.x < player1.x + player1.width + 10 &&
ball.y > player1.y &&
ball.y < player1.y + player1.height
ball.x < paddle1.x + paddle1.width + 10 &&
ball.y > paddle1.y &&
ball.y < paddle1.y + paddle1.height
) {
reverse = true;
}
// if ball bounces off player 2 paddle
// if ball bounces off paddle2
if (
ball.x > player2.x - 10 &&
ball.y > player2.y &&
ball.y < player2.y + player2.height
ball.x > paddle2.x - 10 &&
ball.y > paddle2.y &&
ball.y < paddle2.y + paddle2.height
) {
reverse = true;
}
Expand All @@ -133,8 +133,8 @@ export class PongEngine {
y: ball.y,
vx: ball.vx,
vy: ball.vy,
player1y: this.player1.y,
player2y: this.player2.y,
paddle1y: this.paddle1.y,
paddle2y: this.paddle2.y,
});
this.fireStateChange(e);
}
Expand Down

0 comments on commit f449690

Please sign in to comment.