From 429387ce830594488167e2d14ac37c625aed3276 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Mon, 28 Nov 2022 23:55:24 +0100 Subject: [PATCH] Wip reworking server handleInput + fixedDeltaTime for server --- src/client/handleInput.ts | 11 ++++++--- src/server/class/Client.ts | 6 ++++- src/server/class/GameSession.ts | 40 ++++++++++++++++++++------------- src/server/constants.ts | 6 ++++- src/server/wsServer.ts | 5 +++-- 5 files changed, 46 insertions(+), 22 deletions(-) diff --git a/src/client/handleInput.ts b/src/client/handleInput.ts index db7e14ee..78b52ad2 100644 --- a/src/client/handleInput.ts +++ b/src/client/handleInput.ts @@ -58,16 +58,21 @@ function playerMove(delta: number, keys: string[]) } } +function testInputDelay(input: ev.EventInput) { + socket.send(JSON.stringify(input)); +} +// setTimeout(testInputDelay, 100, input); + + function playerMovePrediction(delta: number, input: en.InputEnum) { // client prediction const racket = clientInfo.racket; - racket.dir.y = 0; if (input === en.InputEnum.up) { - racket.dir.y += -1; + racket.dir.y = -1; } else if (input === en.InputEnum.down) { - racket.dir.y += 1; + racket.dir.y = 1; } racket.moveAndCollide(delta, [gc.wallTop, gc.wallBottom]); } diff --git a/src/server/class/Client.ts b/src/server/class/Client.ts index 00e311a7..c0f6b2c8 100644 --- a/src/server/class/Client.ts +++ b/src/server/class/Client.ts @@ -2,13 +2,17 @@ import { WebSocket } from "../wsServer.js"; import { Racket } from "../../shared_js/class/Rectangle.js"; import { GameSession } from "./GameSession.js"; +import * as ev from "../../shared_js/class/Event.js" class Client { socket: WebSocket; id: string; // Pas indispensable si "socket" a une copie de "id" - lastInputId: number = 0; isAlive: boolean = true; gameSession: GameSession; + + inputArr: ev.EventInput[] = []; + lastInputId: number = 0; + constructor(socket: WebSocket, id: string) { this.socket = socket; this.id = id; diff --git a/src/server/class/GameSession.ts b/src/server/class/GameSession.ts index c6b00095..eaa6a1fb 100644 --- a/src/server/class/GameSession.ts +++ b/src/server/class/GameSession.ts @@ -50,25 +50,35 @@ class GameSession { clearInterval(s.gameLoopInterval); clearInterval(s.clientsUpdateInterval); } - handleInput(client: ClientPlayer, inputEvent: ev.EventInput) { + private _handleInput(delta: number, client: ClientPlayer) { const gc = this.components; - const input = inputEvent.input; - client.lastInputId = inputEvent.inputId; - - client.racket.dir.y = 0; - if (input === en.InputEnum.up) { - client.racket.dir.y += -1; - } - else if (input === en.InputEnum.down) { - client.racket.dir.y += 1; - } - client.racket.moveAndCollide(this.delta_time, [gc.wallTop, gc.wallBottom]); - /* how to handle Delta time correctly in handleInput ? */ + client.inputArr.forEach( (value) => { + const input = value.input; + client.racket.dir.y = 0; + if (input === en.InputEnum.up) { + client.racket.dir.y = -1; + } + else if (input === en.InputEnum.down) { + client.racket.dir.y = 1; + } + client.racket.moveAndCollide(delta, [gc.wallTop, gc.wallBottom]); + }); + client.lastInputId = client.inputArr[client.inputArr.length - 1].inputId; + client.inputArr.length = 0; } private _gameLoop(s: GameSession) { - s.last_time = s.actual_time; + /* s.last_time = s.actual_time; s.actual_time = Date.now(); - s.delta_time = (s.actual_time - s.last_time) / 1000; + s.delta_time = (s.actual_time - s.last_time) / 1000; */ + s.delta_time = c.fixedDeltaTime; + + console.log(s.delta_time); + + s.playersMap.forEach( (client) => { + if (client.inputArr.length != 0) { + s._handleInput(s.delta_time, client); + } + }); const gc = s.components; if (gc.ballInPlay) diff --git a/src/server/constants.ts b/src/server/constants.ts index 031423de..761622fb 100644 --- a/src/server/constants.ts +++ b/src/server/constants.ts @@ -1,5 +1,9 @@ export * from "../shared_js/constants.js" +// 15ms == 1000/66.666 export const gameLoopIntervalMS = 15; // millisecond -export const clientsUpdateIntervalMS = 42; // millisecond +export const fixedDeltaTime = gameLoopIntervalMS/1000; // second + +// 33.333ms == 1000/30 +export const clientsUpdateIntervalMS = 1000/30; // millisecond diff --git a/src/server/wsServer.ts b/src/server/wsServer.ts index 124a5d7e..b767ce7d 100644 --- a/src/server/wsServer.ts +++ b/src/server/wsServer.ts @@ -150,8 +150,9 @@ export function clientInputListener(this: WebSocket, data: string) // const input: ev.ClientEvent = JSON.parse(data); const input: ev.EventInput = JSON.parse(data); if (input.type === en.EventTypes.clientInput) { - const client = clientsMap.get(this.id); - client.gameSession.handleInput(client as ClientPlayer, input); + const client = clientsMap.get(this.id) as ClientPlayer; + client.inputArr.push(input); // TODO: reject input if too fast + // client.gameSession.handleInput(client as ClientPlayer, input); } else { console.log("Invalid clientInput");