Up-in-the-Air – commitdiff

You can use Git to clone the repository via the web URL. Download snapshot (zip)
Inner game loop is now allocation free
authorJulian Fietkau <git@fietkau.software>
Fri, 20 Sep 2024 20:39:16 +0000 (22:39 +0200)
committerJulian Fietkau <git@fietkau.software>
Fri, 20 Sep 2024 20:39:16 +0000 (22:39 +0200)
main.js

diff --git a/main.js b/main.js
index 56afa1b51bc88efd8b2017e841af2c52b6211b41..d2873c1cb66ae9733361ba5678f53d342da737fb 100644 (file)
--- a/main.js
+++ b/main.js
@@ -291,9 +291,13 @@ function init(game, canvas) {
     game.objects.clouds.push(cloud);
   }
 
-  let featherLocalPos = new THREE.Vector3();
-  let lightStartColor = new THREE.Color(0xffffff);
-  let lightEndColor = new THREE.Color(0xffaa66);
+  // All vectors used by the game loop (no allocations inside)
+  const featherLocalPos = new THREE.Vector3();
+  const featherBorderForce = new THREE.Vector3();
+  const pinwheelDistance = new THREE.Vector3();
+  const notCollectedPos = new THREE.Vector3();
+  const collectedPos = new THREE.Vector3();
+  const letterPos = new THREE.Vector3();
   function animate() {
     let delta = Math.min(game.view.clock.getDeltaTime(), 1 / 12);
 
@@ -308,22 +312,18 @@ function init(game, canvas) {
     }
 
     featherLocalPos.subVectors(game.objects.feather.position, game.view.camera.position).setZ(0);
-    if(featherLocalPos.y > 3) {
-      applyForceToFeather(game, new THREE.Vector3(0, (3 - featherLocalPos.y), 0));
-    } else if(featherLocalPos.y < -3) {
-      applyForceToFeather(game, new THREE.Vector3(0, -(3 + featherLocalPos.y), 0));
-    }
-    if(featherLocalPos.x > 3) {
-      applyForceToFeather(game, new THREE.Vector3((3 - featherLocalPos.x), 0, 0));
-    } else if(featherLocalPos.x < -3) {
-      applyForceToFeather(game, new THREE.Vector3(-(3 + featherLocalPos.x), 0, 0));
+    featherBorderForce.set(0, 0, 0);
+    for(let coord of [0, 1]) {
+      if(Math.abs(featherLocalPos.getComponent(coord)) > 3) {
+        featherBorderForce.setComponent(coord, 3 * Math.sign(featherLocalPos.getComponent(coord)) - featherLocalPos.getComponent(coord));
+      }
     }
+    applyForceToFeather(game, featherBorderForce);
     const tiltedGravity = game.gravity.clone();
-    const diff = new THREE.Vector3();
-    diff.subVectors(game.objects.feather.position, game.objects.pinwheel.position).setZ(0);
+    pinwheelDistance.subVectors(game.objects.feather.position, game.objects.pinwheel.position).setZ(0);
 
-    const pinwheelForce = 0.5 * Math.max(0, Math.pow(diff.length(), - 0.5) - 0.5);
-    applyForceToFeather(game, diff.normalize().multiplyScalar(pinwheelForce));
+    const pinwheelForce = 0.5 * Math.max(0, Math.pow(pinwheelDistance.length(), - 0.5) - 0.5);
+    applyForceToFeather(game, pinwheelDistance.normalize().multiplyScalar(pinwheelForce));
     if(pinwheelForce < 0.2) {
       if(game.objects.feather.swayDirection > 0 && game.objects.feather.speed.x > 1.5) {
         game.objects.feather.swayDirection *= -1;
@@ -371,9 +371,10 @@ function init(game, canvas) {
         game.objects.words.collectedCount += 1;
       }
       let x, y, z;
-      let notCollectedPos, collectedPos;
       let collectionAnimationDuration = 1.0;
       for(let j = 0; j < word.children.length; j++) {
+        notCollectedPos.set(0, 0, 0);
+        collectedPos.set(0, 0, 0);
         let letter = word.children[j];
         let wordProgress = j / word.children.length;
         let animationProgress = (((game.timeProgress + 5 * word.randomAnimOffset) % 5) / 5 + j / 37) % 1;
@@ -382,7 +383,7 @@ function init(game, canvas) {
           x = word.mapPos.x + wordAnimationRadius * Math.cos(animationProgress * 5 * Math.PI * 2);
           y = word.mapPos.y + wordAnimationRadius * Math.sin(animationProgress * 4 * Math.PI * 2);
           z = wordAnimationRadius * Math.sin(animationProgress * 6 * Math.PI * 2);
-          notCollectedPos = new THREE.Vector3(x, y, z);
+          notCollectedPos.set(x, y, z);
         }
         if(word.collected) {
           x = game.objects.feather.scale.x * 0.5 * Math.cos(animationProgress * 1 * Math.PI * 2);
@@ -393,18 +394,17 @@ function init(game, canvas) {
           x += game.objects.feather.position.x;
           y += game.objects.feather.position.y;
           z += game.objects.feather.position.z;
-          collectedPos = new THREE.Vector3(x, y, z);
+          collectedPos.set(x, y, z);
         }
-        let letterPos;
-        if(notCollectedPos && collectedPos) {
+        if(notCollectedPos.length() > 0 && collectedPos.length() > 0) {
           let collectingProgress = easeInOut(Math.max(0.0, Math.min(1.0, (game.view.clock.getElapsedTime() - word.collected) / collectionAnimationDuration)));
-          letterPos = new THREE.Vector3().lerpVectors(notCollectedPos, collectedPos, collectingProgress);
+          letterPos.lerpVectors(notCollectedPos, collectedPos, collectingProgress);
           let scale = lerp(1.0, collectedScale, collectingProgress);
           letter.scale.set(scale, scale, scale);
-        } else if(notCollectedPos) {
-          letterPos = notCollectedPos;
-        } else if(collectedPos) {
-          letterPos = collectedPos;
+        } else if(notCollectedPos.length() > 0) {
+          letterPos.set(notCollectedPos.x, notCollectedPos.y, notCollectedPos.z);
+        } else if(collectedPos.length() > 0) {
+          letterPos.set(collectedPos.x, collectedPos.y, collectedPos.z);
         }
         letter.position.set(letterPos.x, letterPos.y, letterPos.z);
         let rotation = (game.timeProgress * 3) % (2 * Math.PI);