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; 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; 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); } } 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}