opponent interpolation

This commit is contained in:
LuckyLaszlo
2022-11-28 16:38:02 +01:00
parent 5325c8b9ee
commit c656de5cad
3 changed files with 58 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
import { gc } from "./global.js";
import { gc, clientInfo } from "./global.js";
let actual_time: number = Date.now();
let last_time: number;
@@ -7,17 +7,31 @@ let delta_time: number;
function gameLoop()
{
/*
// I try to clear only what need to be update.
// Will revert to clear() all if not satisfactory.
pong.clear();
*/
last_time = actual_time;
actual_time = Date.now();
delta_time = (actual_time - last_time) / 1000;
// interpolation
// console.log(`dir.y: ${clientInfo.opponent.dir.y}, pos.y: ${clientInfo.opponent.pos.y}, opponentNextPos.y: ${clientInfo.opponentNextPos.y}`);
if (clientInfo.opponent.dir.y != 0 ) {
opponentInterpolation(delta_time);
}
// client prediction
gc.ball.moveAndBounce(delta_time, [gc.wallTop, gc.wallBottom, gc.playerLeft, gc.playerRight]);
}
function opponentInterpolation(delta: number)
{
// interpolation
clientInfo.opponent.moveAndCollide(delta, [gc.wallTop, gc.wallBottom]);
if ((clientInfo.opponent.dir.y > 0 && clientInfo.opponent.pos.y > clientInfo.opponentNextPos.y)
|| (clientInfo.opponent.dir.y < 0 && clientInfo.opponent.pos.y < clientInfo.opponentNextPos.y))
{
clientInfo.opponent.dir.y = 0;
clientInfo.opponent.pos.y = clientInfo.opponentNextPos.y;
}
}
export {gameLoop}