|
@ -10,17 +10,20 @@ import { |
|
|
} from './constants' |
|
|
} from './constants' |
|
|
import { randomUUID } from 'crypto' |
|
|
import { randomUUID } from 'crypto' |
|
|
import { Spectator } from './Spectator' |
|
|
import { Spectator } from './Spectator' |
|
|
|
|
|
import { type Map } from './Map' |
|
|
|
|
|
|
|
|
const GAME_TICKS = 30 |
|
|
const GAME_TICKS = 30 |
|
|
|
|
|
|
|
|
function gameLoop (game: Game): void { |
|
|
function gameLoop (game: Game): void { |
|
|
const canvasRect = new Rect( |
|
|
const canvasRect: Rect = new Rect( |
|
|
new Point(gameInfoConstants.mapSize.x / 2, gameInfoConstants.mapSize.y / 2), |
|
|
new Point(game.map.size.x / 2, game.map.size.y / 2), |
|
|
new Point(gameInfoConstants.mapSize.x, gameInfoConstants.mapSize.y) |
|
|
new Point(game.map.size.x, game.map.size.y) |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
game.ball.update( |
|
|
game.ball.update( |
|
|
canvasRect, |
|
|
canvasRect, |
|
|
game.players.map((p) => p.paddle) |
|
|
game.players.map((p) => p.paddle), |
|
|
|
|
|
game.map |
|
|
) |
|
|
) |
|
|
const indexPlayerScored: number = game.ball.getIndexPlayerScored() |
|
|
const indexPlayerScored: number = game.ball.getIndexPlayerScored() |
|
|
if (indexPlayerScored !== -1) { |
|
|
if (indexPlayerScored !== -1) { |
|
@ -46,21 +49,23 @@ function gameLoop (game: Game): void { |
|
|
export class Game { |
|
|
export class Game { |
|
|
id: string |
|
|
id: string |
|
|
timer: NodeJS.Timer | null |
|
|
timer: NodeJS.Timer | null |
|
|
|
|
|
map: Map |
|
|
ball: Ball |
|
|
ball: Ball |
|
|
players: Player[] = [] |
|
|
players: Player[] = [] |
|
|
spectators: Spectator[] = [] |
|
|
spectators: Spectator[] = [] |
|
|
playing: boolean |
|
|
playing: boolean |
|
|
|
|
|
|
|
|
constructor (sockets: WebSocket[], uuids: string[], names: string[]) { |
|
|
constructor ( |
|
|
|
|
|
sockets: WebSocket[], |
|
|
|
|
|
uuids: string[], |
|
|
|
|
|
names: string[], |
|
|
|
|
|
map: Map |
|
|
|
|
|
) { |
|
|
this.id = randomUUID() |
|
|
this.id = randomUUID() |
|
|
this.timer = null |
|
|
this.timer = null |
|
|
this.playing = false |
|
|
this.playing = false |
|
|
this.ball = new Ball( |
|
|
this.map = map |
|
|
new Point( |
|
|
this.ball = new Ball(new Point(this.map.size.x / 2, this.map.size.y / 2)) |
|
|
gameInfoConstants.mapSize.x / 2, |
|
|
|
|
|
gameInfoConstants.mapSize.y / 2 |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
for (let i = 0; i < uuids.length; i++) { |
|
|
for (let i = 0; i < uuids.length; i++) { |
|
|
this.addPlayer(sockets[i], uuids[i], names[i]) |
|
|
this.addPlayer(sockets[i], uuids[i], names[i]) |
|
|
} |
|
|
} |
|
@ -70,8 +75,10 @@ export class Game { |
|
|
const yourPaddleIndex = this.players.findIndex((p) => p.name === name) |
|
|
const yourPaddleIndex = this.players.findIndex((p) => p.name === name) |
|
|
return { |
|
|
return { |
|
|
...gameInfoConstants, |
|
|
...gameInfoConstants, |
|
|
|
|
|
mapSize: this.map.size, |
|
|
yourPaddleIndex, |
|
|
yourPaddleIndex, |
|
|
gameId: this.id |
|
|
gameId: this.id, |
|
|
|
|
|
walls: this.map.walls |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -83,16 +90,16 @@ export class Game { |
|
|
private addPlayer (socket: WebSocket, uuid: string, name: string): void { |
|
|
private addPlayer (socket: WebSocket, uuid: string, name: string): void { |
|
|
let paddleCoords = new Point( |
|
|
let paddleCoords = new Point( |
|
|
gameInfoConstants.playerXOffset, |
|
|
gameInfoConstants.playerXOffset, |
|
|
gameInfoConstants.mapSize.y / 2 |
|
|
this.map.size.y / 2 |
|
|
) |
|
|
) |
|
|
if (this.players.length === 1) { |
|
|
if (this.players.length === 1) { |
|
|
paddleCoords = new Point( |
|
|
paddleCoords = new Point( |
|
|
gameInfoConstants.mapSize.x - gameInfoConstants.playerXOffset, |
|
|
this.map.size.x - gameInfoConstants.playerXOffset, |
|
|
gameInfoConstants.mapSize.y / 2 |
|
|
this.map.size.y / 2 |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
this.players.push( |
|
|
this.players.push( |
|
|
new Player(socket, uuid, name, paddleCoords, gameInfoConstants.mapSize) |
|
|
new Player(socket, uuid, name, paddleCoords, this.map.size) |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -119,12 +126,7 @@ export class Game { |
|
|
|
|
|
|
|
|
private start (): boolean { |
|
|
private start (): boolean { |
|
|
if (this.timer === null && this.players.length === 2) { |
|
|
if (this.timer === null && this.players.length === 2) { |
|
|
this.ball = new Ball( |
|
|
this.ball = new Ball(new Point(this.map.size.x / 2, this.map.size.y / 2)) |
|
|
new Point( |
|
|
|
|
|
gameInfoConstants.mapSize.x / 2, |
|
|
|
|
|
gameInfoConstants.mapSize.y / 2 |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
this.players.forEach((p) => { |
|
|
this.players.forEach((p) => { |
|
|
p.newGame() |
|
|
p.newGame() |
|
|
}) |
|
|
}) |
|
|