Wip reworking server handleInput
+ fixedDeltaTime for server
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user