
import * as c from "./constants.js";
import * as en from "../shared_js/enums.js"
import { gc, matchOptions, clientInfo } from "./global.js";
import { wallsMovements } from "../shared_js/wallsMovement.js";

let actual_time: number = Date.now();
let last_time: number;
let delta_time: number;

function gameLoop()
{
	/* last_time = actual_time;
	actual_time = Date.now();
	delta_time = (actual_time - last_time) / 1000; */

	delta_time = c.fixedDeltaTime;
	// console.log(`delta_gameLoop: ${delta_time}`);

	// 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.ballsArr.forEach((ball) => {
		ball.moveAndBounce(delta_time, [gc.wallTop, gc.wallBottom, gc.playerLeft, gc.playerRight]);
	});

	if (matchOptions & en.MatchOptions.movingWalls) {
		wallsMovements(delta_time, gc);
	}
}

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}
