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-24 18:58:45 +01:00
|
|
|
const player = new Player(THREE.Color.NAMES.red);
|
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 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++) {
|
|
|
|
env.update();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|