server handleInput rework done
This commit is contained in:
@@ -10,7 +10,7 @@ class Client {
|
||||
isAlive: boolean = true;
|
||||
gameSession: GameSession;
|
||||
|
||||
inputArr: ev.EventInput[] = [];
|
||||
inputBuffer: ev.EventInput;
|
||||
lastInputId: number = 0;
|
||||
|
||||
constructor(socket: WebSocket, id: string) {
|
||||
|
||||
@@ -52,19 +52,18 @@ class GameSession {
|
||||
}
|
||||
private _handleInput(delta: number, client: ClientPlayer) {
|
||||
const gc = this.components;
|
||||
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;
|
||||
const input = client.inputBuffer.input;
|
||||
|
||||
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.inputBuffer.inputId;
|
||||
client.inputBuffer = null;
|
||||
}
|
||||
private _gameLoop(s: GameSession) {
|
||||
/* 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 = c.fixedDeltaTime;
|
||||
|
||||
console.log(s.delta_time);
|
||||
|
||||
s.playersMap.forEach( (client) => {
|
||||
if (client.inputArr.length != 0) {
|
||||
if (client.inputBuffer) {
|
||||
s._handleInput(s.delta_time, client);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -151,8 +151,7 @@ export function clientInputListener(this: WebSocket, data: string)
|
||||
const input: ev.EventInput = JSON.parse(data);
|
||||
if (input.type === en.EventTypes.clientInput) {
|
||||
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);
|
||||
client.inputBuffer = input;
|
||||
}
|
||||
else {
|
||||
console.log("Invalid clientInput");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
|
||||
import { Vector, VectorInteger } from "./Vector.js";
|
||||
import { Component, Moving } from "./interface.js";
|
||||
import * as c from "../constants.js"
|
||||
|
||||
class Rectangle implements Component {
|
||||
pos: VectorInteger;
|
||||
@@ -120,9 +121,11 @@ class Ball extends MovingRectangle {
|
||||
|
||||
let y = impact / (racketHalf - horizontalMargin) * angleFactor;
|
||||
|
||||
// Normalize Vector (for consistency in speed independent of direction)
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,13 +7,15 @@ export const w = CanvasWidth;
|
||||
export const h = CanvasWidth / CanvasRatio;
|
||||
export const w_mid = Math.floor(w/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 ballSize = pw;
|
||||
export const wallSize = Math.floor(w/100);
|
||||
export const racketSpeed = Math.floor(w/1.5); // pixel per second
|
||||
export const ballSpeed = Math.floor(w/2); // pixel per second
|
||||
export const ballSpeedIncrease = ballSpeed/20; // pixel per second
|
||||
export const wallSize = Math.floor(w*0.01);
|
||||
export const racketSpeed = Math.floor(w*0.66); // pixel per second
|
||||
export const ballSpeed = Math.floor(w*0.66); // 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 newRoundDelay = 1500; // millisecond
|
||||
|
||||
Reference in New Issue
Block a user