authoritative server OK

+ TODO actual matchmaking
This commit is contained in:
LuckyLaszlo
2022-11-21 19:46:25 +01:00
parent 48665cfe30
commit add08c216f
20 changed files with 485 additions and 240 deletions

View File

@@ -1,9 +1,6 @@
import {pong, gc, clientInfo} from "./global.js"
import * as d from "./draw.js";
import {random} from "./utils.js";
import {handleInput} from "./handleInput.js";
let ballInPlay = false;
let actual_time: number = Date.now();
let last_time: number;
let delta_time: number;
@@ -21,47 +18,10 @@ function gameLoop()
handleInput(delta_time);
if (ballInPlay)
{
gc.ball.moveAndBounce(delta_time, [gc.wallTop, gc.wallBottom, gc.playerLeft, gc.playerRight]);
if (gc.ball.pos.x > pong.canvas.width) {
ballInPlay = false;
gc.score1.clear();
++gc.score1.value;
setTimeout(newRound, 1500);
}
else if (gc.ball.pos.x < 0 - gc.ball.width) {
ballInPlay = false;
gc.score2.clear();
++gc.score2.value;
setTimeout(newRound, 1500);
}
}
// prediction
// gc.ball.moveAndBounce(delta_time, [gc.wallTop, gc.wallBottom, gc.playerLeft, gc.playerRight]);
d.draw();
}
function newRound()
{
// https://fr.wikipedia.org/wiki/Tennis_de_table#Nombre_de_manches
if (gc.score1.value >= 11
|| gc.score2.value >= 11)
{
if (Math.abs(gc.score1.value - gc.score2.value) >= 2)
{
if (gc.score1.value > gc.score2.value) {
alert("Player 1 WIN");
}
else {
alert("Player 2 WIN");
}
return;
}
}
gc.ball.pos.x = Math.floor(pong.canvas.width/2);
gc.ball.pos.y = Math.floor((pong.canvas.height * 0.1) + random() * (pong.canvas.height * 0.8));
gc.ball.speed = gc.ball.baseSpeed;
ballInPlay = true;
}
export {gameLoop, newRound}
export {gameLoop}