server handleInput rework done

This commit is contained in:
LuckyLaszlo
2022-11-29 00:47:02 +01:00
parent 429387ce83
commit 6ac3592bc0
5 changed files with 27 additions and 26 deletions

View File

@@ -10,7 +10,7 @@ class Client {
isAlive: boolean = true; isAlive: boolean = true;
gameSession: GameSession; gameSession: GameSession;
inputArr: ev.EventInput[] = []; inputBuffer: ev.EventInput;
lastInputId: number = 0; lastInputId: number = 0;
constructor(socket: WebSocket, id: string) { constructor(socket: WebSocket, id: string) {

View File

@@ -52,19 +52,18 @@ class GameSession {
} }
private _handleInput(delta: number, client: ClientPlayer) { private _handleInput(delta: number, client: ClientPlayer) {
const gc = this.components; const gc = this.components;
client.inputArr.forEach( (value) => { const input = client.inputBuffer.input;
const input = value.input;
client.racket.dir.y = 0; if (input === en.InputEnum.up) {
if (input === en.InputEnum.up) { client.racket.dir.y = -1;
client.racket.dir.y = -1; }
} else if (input === en.InputEnum.down) {
else if (input === en.InputEnum.down) { client.racket.dir.y = 1;
client.racket.dir.y = 1; }
} client.racket.moveAndCollide(delta, [gc.wallTop, gc.wallBottom]);
client.racket.moveAndCollide(delta, [gc.wallTop, gc.wallBottom]);
}); client.lastInputId = client.inputBuffer.inputId;
client.lastInputId = client.inputArr[client.inputArr.length - 1].inputId; client.inputBuffer = null;
client.inputArr.length = 0;
} }
private _gameLoop(s: GameSession) { private _gameLoop(s: GameSession) {
/* s.last_time = s.actual_time; /* s.last_time = s.actual_time;
@@ -72,10 +71,8 @@ class GameSession {
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; s.delta_time = c.fixedDeltaTime;
console.log(s.delta_time);
s.playersMap.forEach( (client) => { s.playersMap.forEach( (client) => {
if (client.inputArr.length != 0) { if (client.inputBuffer) {
s._handleInput(s.delta_time, client); s._handleInput(s.delta_time, client);
} }
}); });

View File

@@ -151,8 +151,7 @@ export function clientInputListener(this: WebSocket, data: string)
const input: ev.EventInput = JSON.parse(data); const input: ev.EventInput = JSON.parse(data);
if (input.type === en.EventTypes.clientInput) { if (input.type === en.EventTypes.clientInput) {
const client = clientsMap.get(this.id) as ClientPlayer; const client = clientsMap.get(this.id) as ClientPlayer;
client.inputArr.push(input); // TODO: reject input if too fast client.inputBuffer = input;
// client.gameSession.handleInput(client as ClientPlayer, input);
} }
else { else {
console.log("Invalid clientInput"); console.log("Invalid clientInput");

View File

@@ -1,6 +1,7 @@
import { Vector, VectorInteger } from "./Vector.js"; import { Vector, VectorInteger } from "./Vector.js";
import { Component, Moving } from "./interface.js"; import { Component, Moving } from "./interface.js";
import * as c from "../constants.js"
class Rectangle implements Component { class Rectangle implements Component {
pos: VectorInteger; pos: VectorInteger;
@@ -120,9 +121,11 @@ class Ball extends MovingRectangle {
let y = impact / (racketHalf - horizontalMargin) * angleFactor; let y = impact / (racketHalf - horizontalMargin) * angleFactor;
// Normalize Vector (for consistency in speed independent of direction)
this.dir.assign(x, y); this.dir.assign(x, y);
this.dir = this.dir.normalized(); // Normalize Vector (for consistency in speed independent of direction)
if (c.normalizedSpeed) {
this.dir = this.dir.normalized();
}
// console.log(`x: ${this.dir.x}, y: ${this.dir.y}`); // console.log(`x: ${this.dir.x}, y: ${this.dir.y}`);
} }
} }

View File

@@ -7,13 +7,15 @@ export const w = CanvasWidth;
export const h = CanvasWidth / CanvasRatio; export const h = CanvasWidth / CanvasRatio;
export const w_mid = Math.floor(w/2); export const w_mid = Math.floor(w/2);
export const h_mid = Math.floor(h/2); export const h_mid = Math.floor(h/2);
export const pw = Math.floor(w/60); export const pw = Math.floor(w*0.017);
export const ph = pw*6; export const ph = pw*6;
export const ballSize = pw; export const ballSize = pw;
export const wallSize = Math.floor(w/100); export const wallSize = Math.floor(w*0.01);
export const racketSpeed = Math.floor(w/1.5); // pixel per second export const racketSpeed = Math.floor(w*0.66); // pixel per second
export const ballSpeed = Math.floor(w/2); // pixel per second export const ballSpeed = Math.floor(w*0.66); // pixel per second
export const ballSpeedIncrease = ballSpeed/20; // pixel per second export const ballSpeedIncrease = Math.floor(ballSpeed*0.05); // pixel per second
export const normalizedSpeed = false; // for consistency in speed independent of direction
export const matchStartDelay = 3000; // millisecond export const matchStartDelay = 3000; // millisecond
export const newRoundDelay = 1500; // millisecond export const newRoundDelay = 1500; // millisecond