"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Game = void 0; const uuid_1 = require("uuid"); const Player_1 = require("./models/Player"); const GameSettings_1 = require("./models/GameSettings"); class Game { constructor() { this.gameSettings = new GameSettings_1.GameSettings(); this.players = new Map(); this.sockets = new Map(); this.computeUpdates = false; this.updateInterval = setInterval(() => this.step(), 10000000); this.lastUpdateTime = Date.now(); this.doUpdate = true; clearInterval(this.updateInterval); } login(connection, name) { console.log('[GAME] Player ' + name + ' connected | ' + this.players.size); const id = uuid_1.v4(); connection.id = id; connection.name = name; this.sockets.set(id, connection); this.players.set(id, new Player_1.Player(id, name)); connection.send(JSON.stringify({ type: 'login', player: this.players.get(id) })); connection.send(JSON.stringify({ type: 'gameSettings', gameSettings: this.gameSettings })); if (!this.computeUpdates) { console.log('[GAME] Starting updates'); this.updateInterval = setInterval(() => this.step(), 1000 / 60); this.computeUpdates = true; } } logout(connection) { console.log('[GAME] Player ' + connection.name + ' disconnected | ' + this.players.size); this.sockets.delete(connection.id); this.players.delete(connection.id); if (this.players.size === 0) { console.log('[GAME] Stoping updates'); clearInterval(this.updateInterval); this.computeUpdates = false; } } respawn(connection) { console.log('[GAME] Player ' + connection.name + ' respawned'); this.players.get(connection.id)?.reset(); connection.send(JSON.stringify({ type: 'gamePlayerSpawn', player: this.players.get(connection.id) })); } update(connection, player) { const playerToUpdate = this.players.get(connection.id); if (playerToUpdate) playerToUpdate.angle = player.angle; } kill(player) { console.log('[GAME] Player ' + player.name + ' died'); player.kill(); this.sockets.get(player.id)?.send(JSON.stringify({ type: 'gamePlayerDead', player: player })); } step() { const currentTime = Date.now(); const durationSinceLastUpdate = (currentTime - this.lastUpdateTime) / 1000; const tickToSimulate = (durationSinceLastUpdate * 60) / 1000; this.lastUpdateTime = currentTime; // console.log('UPDATE ' + currentTime + ' doUpdate ' + doUpdate) this.players.forEach((player, id) => { if (player.isOutOfBorders()) this.kill(player); this.players.forEach((player2, id2) => { if (player.state === 'DEAD') return; for (let i = 0; i < player2.walls.length - 2; i++) { // Prevent self destroy on last wall if (player === player2 && i >= player2.walls.length - 1) break; const wallA = player2.walls[i]; const wallB = player2.walls[i + 1]; if (player.isCloseToWall(wallA, wallB)) { if (player.isCrossingLine(wallA, wallB)) { if (player !== player2) player2.score += 300; this.kill(player); return; } } } }); }); this.players.forEach((player, id) => { player.step(tickToSimulate); }); if (this.doUpdate) this.broadcastUpdate(); this.doUpdate = !this.doUpdate; } broadcastUpdate() { const update = { type: 'gameUpdate', players: [...this.players.values()], time: this.lastUpdateTime }; this.sockets.forEach((connection, id) => { connection.send(JSON.stringify(update)); }); } } exports.Game = Game; //# sourceMappingURL=Game.js.map