Browse Source

* Added time before launching pong ball

master
vvandenb 2 years ago
parent
commit
56bb28bbb6
  1. 9
      back/volume/src/pong/game/Ball.ts
  2. 7
      back/volume/src/pong/game/Game.ts

9
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
if (this.timeoutTime <= 0) {
this.move(canvasRect, paddles, map)
} else {
this.timeoutTime -= 1000 / GAME_TICKS
}
}
}

7
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<void> {
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`)
}
}

Loading…
Cancel
Save