From 56bb28bbb6772e4dedd2ee4e1076eaa70cc9664d Mon Sep 17 00:00:00 2001 From: vvandenb Date: Mon, 13 Mar 2023 15:58:14 +0100 Subject: [PATCH] * Added time before launching pong ball --- back/volume/src/pong/game/Ball.ts | 11 +++++++++-- back/volume/src/pong/game/Game.ts | 7 ++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/back/volume/src/pong/game/Ball.ts b/back/volume/src/pong/game/Ball.ts index b5c7265..a81aae6 100644 --- a/back/volume/src/pong/game/Ball.ts +++ b/back/volume/src/pong/game/Ball.ts @@ -1,7 +1,7 @@ import { type Paddle } from './Paddle' import { type Point, Rect } from './utils' import { type MapDtoValidated } from '../dtos/MapDtoValidated' -import { DEFAULT_BALL_SIZE, DEFAULT_BALL_INITIAL_SPEED } from './constants' +import { DEFAULT_BALL_SIZE, DEFAULT_BALL_INITIAL_SPEED, GAME_TICKS } from './constants' export class Ball { rect: Rect @@ -9,6 +9,7 @@ export class Ball { speed: Point spawn: Point indexPlayerScored: number + timeoutTime: number constructor ( spawn: Point, @@ -20,6 +21,7 @@ export class Ball { this.initial_speed = speed this.spawn = spawn.clone() this.indexPlayerScored = -1 + this.timeoutTime = 0 } getIndexPlayerScored (): number { @@ -29,9 +31,14 @@ export class Ball { update (canvasRect: Rect, paddles: Paddle[], map: MapDtoValidated): void { if (!canvasRect.contains_x(this.rect)) { this.indexPlayerScored = this.playerScored() + this.timeoutTime = 2000 } else { this.indexPlayerScored = -1 - this.move(canvasRect, paddles, map) + if (this.timeoutTime <= 0) { + this.move(canvasRect, paddles, map) + } else { + this.timeoutTime -= 1000 / GAME_TICKS + } } } diff --git a/back/volume/src/pong/game/Game.ts b/back/volume/src/pong/game/Game.ts index 58c8448..cc4aec1 100644 --- a/back/volume/src/pong/game/Game.ts +++ b/back/volume/src/pong/game/Game.ts @@ -84,12 +84,12 @@ export class Game { this.players[playerIndex].ready = true console.log(`${this.players[playerIndex].name} is ready`) if (this.players.length === 2 && this.players.every((p) => p.ready)) { - this.start() + void this.start() } } } - private start (): void { + private async start (): Promise { if (this.timer === null && this.players.length === 2) { this.ball = new Ball(new Point(this.map.size.x / 2, this.map.size.y / 2)) this.players.forEach((p) => { @@ -98,8 +98,9 @@ export class Game { }) this.playing = true this.broadcastGame(formatWebsocketData(GAME_EVENTS.START_GAME)) + console.log(`Game ${this.id} starting in 3 seconds`) + await new Promise((resolve) => setTimeout(resolve, 3000)) this.timer = setInterval(this.gameLoop.bind(this), 1000 / GAME_TICKS) - console.log(`Game ${this.id} started`) } }