Add player container

This commit is contained in:
schmelczerandras 2020-10-24 10:01:54 +02:00
parent 1cd8f5fbe6
commit b55e927a34
16 changed files with 159 additions and 133 deletions

View file

@ -1,5 +1,4 @@
import { PhysicalContainer } from './physics/containers/physical-container';
import { Player } from './players/player';
import ioserver from 'socket.io';
import {
TransportEvents,
@ -11,18 +10,22 @@ import {
import { createWorld } from './map/create-world';
import { DeltaTimeCalculator } from './helper/delta-time-calculator';
import { Options } from './options';
import { PlayerContainer } from './players/player-container';
const playerCountSubscribedRoom = 'playerCountUpdates';
export class GameServer {
private objects = new PhysicalContainer();
private players: Array<Player> = [];
private players: PlayerContainer;
private deltaTimes: Array<number> = [];
private deltaTimeCalculator = new DeltaTimeCalculator();
private serverName: string;
private playerLimit: number;
constructor(private readonly io: ioserver.Server, options: Options) {
this.players = new PlayerContainer(this.objects);
this.serverName = options.name;
this.playerLimit = options.playerLimit;
@ -31,8 +34,7 @@ export class GameServer {
io.on('connection', (socket: SocketIO.Socket) => {
socket.on(TransportEvents.PlayerJoining, (playerInfo: PlayerInformation) => {
const player = new Player(playerInfo, this.players, this.objects, socket);
this.players.push(player);
const player = this.players.createPlayer(playerInfo, socket);
socket.on(TransportEvents.PlayerToServer, (json: string) => {
const command = deserialize(json);
player.sendCommand(command);
@ -42,7 +44,7 @@ export class GameServer {
socket.on('disconnect', () => {
player.destroy();
this.players = this.players.filter((p) => p !== player);
this.players.deletePlayer(player);
this.sendPlayerCountUpdate();
});
});
@ -56,7 +58,7 @@ export class GameServer {
public sendPlayerCountUpdate() {
this.io
.to(playerCountSubscribedRoom)
.emit(TransportEvents.PlayerCountUpdate, this.players.length);
.emit(TransportEvents.PlayerCountUpdate, this.players.count);
}
public start() {
@ -64,8 +66,7 @@ export class GameServer {
}
private handlePhysics() {
const delta = this.deltaTimeCalculator.getNextDeltaTimeInMilliseconds();
this.deltaTimes.push(delta);
const delta = this.deltaTimeCalculator.getNextDeltaTimeInSeconds();
const framesBetweenDeltaTimeCalculation = 1000;
if (this.deltaTimes.length > framesBetweenDeltaTimeCalculation) {
@ -79,15 +80,15 @@ export class GameServer {
`Memory used: ${(process.memoryUsage().rss / 1024 / 1024).toFixed(2)} MB`,
);
this.deltaTimes = [];
console.log(this.players.map((p) => p.latency));
}
this.objects.stepObjects(delta / 1000);
this.players.forEach((p) => p.step(delta / 1000));
this.objects.stepObjects(delta);
this.players.step(delta);
const physicsDelta = this.deltaTimeCalculator.getDeltaTimeInMilliseconds();
const physicsDelta = this.deltaTimeCalculator.getDeltaTimeInSeconds() * 1000;
this.deltaTimes.push(physicsDelta);
const sleepTime = settings.targetPhysicsDeltaTimeInMilliseconds - physicsDelta;
if (sleepTime >= settings.minPhysicsSleepTime) {
setTimeout(this.handlePhysics.bind(this), sleepTime);
} else {
@ -98,7 +99,7 @@ export class GameServer {
public get serverInfo(): ServerInformation {
return {
serverName: this.serverName,
playerCount: this.players.length,
playerCount: this.players.count,
playerLimit: this.playerLimit,
};
}