From f449690c079fd317cd68a76b095693d9677eb0bd Mon Sep 17 00:00:00 2001 From: Tony Pujals Date: Thu, 11 Aug 2022 00:58:45 -0700 Subject: [PATCH] renamed player to paddle --- src/client/pong.ts | 33 +++++++++++++------------ src/common/pong/messages.ts | 7 +++--- src/server/pong/engine.ts | 48 ++++++++++++++++++------------------- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/client/pong.ts b/src/client/pong.ts index 7fb1918..cd51378 100644 --- a/src/client/pong.ts +++ b/src/client/pong.ts @@ -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 void]>; @@ -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. // @@ -93,7 +93,6 @@ export class PongApp extends P5App { this.mapper.set( "WebSocketError", [WebSocketError, this.onWebSocketError.bind(this)]); - } override setup() { @@ -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 { diff --git a/src/common/pong/messages.ts b/src/common/pong/messages.ts index ce4c6d6..2487fb1 100644 --- a/src/common/pong/messages.ts +++ b/src/common/pong/messages.ts @@ -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) { @@ -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); diff --git a/src/server/pong/engine.ts b/src/server/pong/engine.ts index 6add5dd..ba86557 100644 --- a/src/server/pong/engine.ts +++ b/src/server/pong/engine.ts @@ -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; @@ -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 { @@ -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; } } @@ -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) { @@ -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; } @@ -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); }