From 09d6d5679cac00087ef1c3ca1f74530d7118b8b4 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 24 Nov 2022 18:41:17 +0100 Subject: [PATCH] have some time logic in the refresh --- js/Env.js | 12 ++++++++++++ js/main.js | 11 +++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/js/Env.js b/js/Env.js index dd90e76..6943119 100644 --- a/js/Env.js +++ b/js/Env.js @@ -42,6 +42,9 @@ export class Env { light.position.set(2, 1, 4); light.castShadow = true; this.scene.add(light); + + // Clock + this.clock = new THREE.Clock(); } /** @@ -100,4 +103,13 @@ export class Env { * @returns */ render = () => this.renderer.render(this.scene, this.camera); + + /** + * Update the game logic + */ + update = () => { + console.log(s); + this.animate(); + this.render(); + }; } diff --git a/js/main.js b/js/main.js index 75d0f26..eb646bc 100644 --- a/js/main.js +++ b/js/main.js @@ -22,10 +22,17 @@ const main = () => { addEventListener("keypress", joueur.controlUser); + /** + * Run the game + */ const animate = () => { + const delta = env.clock.getDelta(); + const ticks = Math.round(delta / (1 / 120)); + for (let i = 0; i < ticks; i++) { + env.update(); + } + requestAnimationFrame(animate); - env.animate(); - env.render(); }; animate();