Files
42_INT_14_transcendence/jeu/src/client/pong.js
2022-12-14 10:45:10 +01:00

64 lines
2.6 KiB
JavaScript

initDom();
function initDom() {
document.getElementById("play_pong_button").addEventListener("click", init);
}
import * as c from "./constants.js";
import * as en from "../shared_js/enums.js";
import { GameArea } from "./class/GameArea.js";
import { GameComponentsClient } from "./class/GameComponentsClient.js";
import { handleInput } from "./handleInput.js";
// import { sendLoop } from "./handleInput.js";
import { gameLoop } from "./gameLoop.js";
import { drawLoop } from "./draw.js";
import { countdown } from "./utils.js";
import { initWebSocket } from "./ws.js";
import { initAudio } from "./audio.js";
/* TODO: A way to delay the init of variables, but still use "const" not "let" ? */
import { pong, gc } from "./global.js";
import { initPong, initGc, initMatchOptions, initStartFunction } from "./global.js";
function init() {
console.log("multi_balls:" + document.getElementById("multi_balls").checked);
console.log("moving_walls:" + document.getElementById("moving_walls").checked);
console.log("sound_on:" + document.getElementById("sound_on").checked);
let soundMutedFlag = false;
if (document.getElementById("sound_off").checked) {
soundMutedFlag = true;
}
initAudio(soundMutedFlag);
let matchOptions = en.MatchOptions.noOption;
if (document.getElementById("multi_balls").checked) {
matchOptions |= en.MatchOptions.multiBalls;
}
if (document.getElementById("moving_walls").checked) {
matchOptions |= en.MatchOptions.movingWalls;
}
initMatchOptions(matchOptions);
document.getElementById("div_game_options").remove();
document.getElementById("div_game_instructions").remove();
initPong(new GameArea());
initGc(new GameComponentsClient(matchOptions, pong.ctx));
initStartFunction(start);
initWebSocket(matchOptions);
}
function start() {
gc.text1.pos.assign(c.w * 0.5, c.h * 0.75);
countdown(c.matchStartDelay / 1000, (count) => {
gc.text1.clear();
gc.text1.text = `${count}`;
gc.text1.update();
}, resume);
}
function resume() {
gc.text1.text = "";
window.addEventListener('keydown', function (e) {
pong.addKey(e.key);
});
window.addEventListener('keyup', function (e) {
pong.deleteKey(e.key);
});
pong.handleInputInterval = window.setInterval(handleInput, c.handleInputIntervalMS);
// pong.handleInputInterval = window.setInterval(sendLoop, c.sendLoopIntervalMS);
pong.gameLoopInterval = window.setInterval(gameLoop, c.gameLoopIntervalMS);
pong.drawLoopInterval = window.setInterval(drawLoop, c.drawLoopIntervalMS);
}