2022-11-24 20:16:48 +01:00
|
|
|
import { Env, TypeEntity } from "./Env.js";
|
2022-11-22 10:40:56 +01:00
|
|
|
import { Plane } from "./Plane.js";
|
2022-11-24 18:58:45 +01:00
|
|
|
import { Player } from "./Player.js";
|
2022-11-24 19:45:20 +01:00
|
|
|
import { Quality } from "./utils.js";
|
2022-11-08 10:40:45 +01:00
|
|
|
|
2022-11-07 09:10:20 +01:00
|
|
|
window.addEventListener("load", () => main());
|
|
|
|
|
|
|
|
const main = () => {
|
2022-11-24 19:45:20 +01:00
|
|
|
// THREE.js
|
2022-11-08 10:40:45 +01:00
|
|
|
const env = new Env();
|
|
|
|
document.body.appendChild(env.getDomElement());
|
2022-11-08 09:43:37 +01:00
|
|
|
|
2022-11-24 19:45:20 +01:00
|
|
|
// World
|
2022-11-22 10:40:56 +01:00
|
|
|
const plan = new Plane(
|
|
|
|
window.innerWidth / 50,
|
|
|
|
10,
|
|
|
|
THREE.Color.NAMES.white,
|
|
|
|
THREE.Color.NAMES.black
|
|
|
|
);
|
|
|
|
env.addToScene(plan);
|
2022-11-08 09:43:37 +01:00
|
|
|
|
2022-11-24 19:45:20 +01:00
|
|
|
// Player
|
2022-11-27 17:20:49 +01:00
|
|
|
const player = new Player(THREE.Color.NAMES.pink);
|
2022-11-24 20:16:48 +01:00
|
|
|
env.addToScene(player, TypeEntity.player);
|
2022-11-24 18:58:45 +01:00
|
|
|
addEventListener("keypress", player.controlUser);
|
2022-11-24 16:03:01 +01:00
|
|
|
|
2022-11-24 21:01:59 +01:00
|
|
|
// Generate random map
|
|
|
|
env.generateRandomMap(20);
|
|
|
|
|
2022-11-24 19:45:20 +01:00
|
|
|
// GUI
|
2022-11-24 19:46:18 +01:00
|
|
|
const gui = new dat.gui.GUI({ closeOnTop: true });
|
2022-11-24 19:45:20 +01:00
|
|
|
const menu = Quality.buildGUI(env.quality);
|
|
|
|
for (const quality in menu) {
|
|
|
|
gui.add(menu, quality)
|
|
|
|
.listen()
|
|
|
|
.onFinishChange(() => {
|
|
|
|
Quality.update(env.setQuality, menu, quality);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-24 21:33:38 +01:00
|
|
|
let score = document.getElementById("score");
|
|
|
|
score.style =
|
|
|
|
"position: absolute; \
|
|
|
|
top: 10px; \
|
|
|
|
width: 100%; \
|
|
|
|
text-align: center; \
|
|
|
|
display: block; \
|
|
|
|
color: white; \
|
|
|
|
font-size: xx-large; \
|
|
|
|
font-family: sans-serif;";
|
|
|
|
|
2022-11-24 18:41:17 +01:00
|
|
|
/**
|
|
|
|
* Run the game
|
|
|
|
*/
|
2022-11-08 09:43:37 +01:00
|
|
|
const animate = () => {
|
2022-11-24 18:41:17 +01:00
|
|
|
const delta = env.clock.getDelta();
|
|
|
|
const ticks = Math.round(delta / (1 / 120));
|
|
|
|
for (let i = 0; i < ticks; i++) {
|
2022-12-02 00:15:37 +01:00
|
|
|
if (env.update()) {
|
|
|
|
let end = document.createElement("p");
|
|
|
|
end.textContent = "Partie terminée !";
|
|
|
|
end.style =
|
|
|
|
"margin: 0; \
|
|
|
|
position: absolute; \
|
|
|
|
top: 50%; \
|
|
|
|
left: 40%; \
|
|
|
|
font-size: 400%; \
|
|
|
|
font-family: sans-serif; \
|
|
|
|
color: white; \
|
|
|
|
transform: translate(-50%, -50%) }";
|
|
|
|
document.body.appendChild(end);
|
|
|
|
|
|
|
|
let restart = document.createElement("button");
|
|
|
|
restart.textContent = "Cliquez ici pour redémarrer";
|
|
|
|
restart.style =
|
|
|
|
"margin: 0; \
|
|
|
|
position: absolute; \
|
|
|
|
top: 60%; \
|
|
|
|
left: 27%; \
|
|
|
|
border: none; \
|
|
|
|
background: none; \
|
|
|
|
color: white; \
|
|
|
|
font-size: 400%; \
|
|
|
|
font-family: sans-serif; \
|
|
|
|
transform: translate(-50%, -50%) }";
|
|
|
|
document.body.appendChild(restart);
|
|
|
|
|
|
|
|
restart.addEventListener("click", () => {
|
|
|
|
location.href = location.href;
|
|
|
|
});
|
|
|
|
|
|
|
|
gui.destroy();
|
|
|
|
return;
|
|
|
|
}
|
2022-11-24 18:41:17 +01:00
|
|
|
}
|
|
|
|
|
2022-11-24 21:33:38 +01:00
|
|
|
score.textContent = `Score : ${Math.floor(env.clock.elapsedTime)}`;
|
|
|
|
|
2022-11-08 09:43:37 +01:00
|
|
|
requestAnimationFrame(animate);
|
2022-11-08 10:40:45 +01:00
|
|
|
};
|
2022-11-08 09:43:37 +01:00
|
|
|
|
2022-11-24 19:45:20 +01:00
|
|
|
// Run it
|
2022-11-08 09:43:37 +01:00
|
|
|
animate();
|
2022-11-07 09:10:20 +01:00
|
|
|
};
|