/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* multiOnlineGamePage.js :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: edbernar { if (e.key == 'g') { player.pointAnimation(map); map.animationGoal(ball.object.position.x, ball.object.position.y, ball.object.position.z, player.playerGoalAnimation); console.log('player.playerGoalAnimation', player.playerGoalAnimation); } if (e.key == 'h') { player.pointOpponentAnimation(map, opponent.object); map.animationGoal(ball.object.position.x, ball.object.position.y, ball.object.position.z, opponent.playerGoalAnimation); console.log('player.playerGoalAnimation', opponent.playerGoalAnimation); } if (e.key == 'c') debug = !debug; if (e.key == 'p') map.putVideoOnCanvas(0, null); if (e.key == 'o') { map.putVideoOnCanvas(3, 'goal'); } if (e.key == 'i') map.putVideoOnCanvas(3, 'outstanding'); if (e.key == 'u') map.putVideoOnCanvas(3, 3); if (e.key == 'y') map.putVideoOnCanvas(2, 3); if (e.key == 't') map.putVideoOnCanvas(1, 3); if (e.key == 'l') map.reCreate("player"); if (e.key == 'k') map.reCreate("opponent"); }) renderer.setAnimationLoop(loop) sendRequest('game', {action: 1}); let lastPosition = player.object.position.x; let lastUp = player.isUp; interval = setInterval(() => { if (player && player.object.position.x != lastPosition || player.isUp != lastUp) { lastPosition = player.object.position.x; lastUp = player.isUp; sendRequest('game', {action: 3, pos: player.object.position.x, up: player.isUp}); } }, 1000 / 20); intervalPing = setInterval(() => { if (!lastPingTime) { sendRequest('game', {action: 4}); lastPingTime = Date.now(); } }, 800); } static dispose() { window.removeEventListener('resize', windowUpdater); if (interval) clearInterval(interval); interval = null; if (intervalPing) clearInterval(intervalPing); intervalPing = null; if (renderer) renderer.dispose(); renderer = null; if (map) map.dispose(); map = null; if (ball) ball.dispose(); ball = null; if (player) player.dispose(); player = null; if (opponent) opponent.dispose(); opponent = null; if (scene) { scene.children.forEach(child => { if (child.geometry) child.geometry.dispose(); if (child.material) child.material.dispose(); if (child.texture) child.texture.dispose(); scene.remove(child); }); } scene = null; } static opponentDisconnect() { pageRenderer.changePage('lobbyPage'); } static ping() { const text = document.getElementById('ping'); const ping = Date.now() - lastPingTime; if (ping < 90) text.style.color = 'white'; else if (ping >= 90 && ping < 150) text.style.color = 'orange'; else text.style.color = 'red'; text.innerText = ping + ' ms'; lastPingTime = null; } static endGame(content) { const endGameDiv = document.getElementById('endGameDiv'); const scoreText = document.getElementById('endGameText'); const endGameScore = document.getElementById('endGameScore'); const simpleText = document.getElementById('endGameSimpleText'); let intervalEnd = null; let time = 4; endGameScore.innerText = `${map.score.player} - ${map.score.opponent}`; if (content.won) scoreText.innerText = "You win !" endGameDiv.style.display = 'flex'; intervalEnd = setInterval(() => { if (content.opponentLeft) simpleText.innerText = `Your opponent has given up...\nYou will be redirected to the lobby in ${time} seconds` else simpleText.innerText = `You will be redirected to the lobby in ${time} seconds` time--; if (time == -1) { clearInterval(intervalEnd); setTimeout(() => pageRenderer.changePage('lobbyPage'), 500); } }, 1000); } } function createBarPlayer(skin) { const geometry = new THREE.BoxGeometry(1, 0.1, 0.1); let material = null; if (skin.color) material = new THREE.MeshPhysicalMaterial({color: skin.color}); else material = new THREE.MeshPhysicalMaterial({map: new THREE.TextureLoader().load(skin.texture)}); const mesh = new THREE.Mesh(geometry, material); mesh.castShadow = true; return (mesh); } function windowUpdater() { renderer.setSize(window.innerWidth, window.innerHeight); player.camera.aspect = window.innerWidth / window.innerHeight; player.camera.updateProjectionMatrix(); }; function loop() { showFps(); player.update(); opponent.update(); ball.update(); map.update(ball); if (debug) { controls.update(); renderer.render(scene, cameraTmp); } else renderer.render(scene, player.camera); } let lastFpsArr = [10, 3, 5]; function showFps() { const fps = document.getElementById('fps') const now = Date.now(); if (now > lastFpsDisplayed + 800) { fps.innerText = Math.round(lastFpsArr.reduce((a, b) => a + b, 0) / lastFpsArr.length) + ' fps'; lastFpsDisplayed = now; lastFpsArr = []; } else lastFpsArr.push(Math.floor(1000 / (now - lastFpsTime))); lastFpsTime = now; } export { MultiOnlineGamePage, player, opponent, ball, map};