Up-in-the-Air – commitdiff

You can use Git to clone the repository via the web URL. Download snapshot (zip)
Add sound effects
authorJulian Fietkau <git@fietkau.software>
Wed, 25 Sep 2024 00:47:27 +0000 (02:47 +0200)
committerJulian Fietkau <git@fietkau.software>
Wed, 25 Sep 2024 00:47:27 +0000 (02:47 +0200)
audio/sound1.ogg [new file with mode: 0644]
audio/sound2.ogg [new file with mode: 0644]
audio/sound3.ogg [new file with mode: 0644]
audio/sound4.ogg [new file with mode: 0644]
audio/sound5.ogg [new file with mode: 0644]
main.js

diff --git a/audio/sound1.ogg b/audio/sound1.ogg
new file mode 100644 (file)
index 0000000..f9f7ec8
Binary files /dev/null and b/audio/sound1.ogg differ
diff --git a/audio/sound2.ogg b/audio/sound2.ogg
new file mode 100644 (file)
index 0000000..dc7982b
Binary files /dev/null and b/audio/sound2.ogg differ
diff --git a/audio/sound3.ogg b/audio/sound3.ogg
new file mode 100644 (file)
index 0000000..1d93155
Binary files /dev/null and b/audio/sound3.ogg differ
diff --git a/audio/sound4.ogg b/audio/sound4.ogg
new file mode 100644 (file)
index 0000000..6661b30
Binary files /dev/null and b/audio/sound4.ogg differ
diff --git a/audio/sound5.ogg b/audio/sound5.ogg
new file mode 100644 (file)
index 0000000..9cca774
Binary files /dev/null and b/audio/sound5.ogg differ
diff --git a/main.js b/main.js
index 3674f0f8c27c4a60d652e296e1977d8ee51bf489..a0795d7b659ae218cf977cd4798d2a34c981fe8d 100644 (file)
--- a/main.js
+++ b/main.js
@@ -8,6 +8,28 @@ function getRandomWord(game) {
   return game.assets.words[Math.floor(Math.random() * game.assets.words.length)];
 }
 
+function playRandomSound(game) {
+  if(!game.view || !game.view.audioListener) {
+    return;
+  }
+  if(!game.view.lastSoundsCache) {
+    game.view.lastSoundsCache = [];
+  }
+  let index;
+  // We remember the last two notes played and make sure not to repeat one of those.
+  do {
+    index = 1 + Math.floor(Math.random() * 5);
+  } while(game.view.lastSoundsCache.includes(index));
+  game.view.lastSoundsCache.push(index);
+  if(game.view.lastSoundsCache.length > 2) {
+    game.view.lastSoundsCache.splice(0, 1);
+  }
+  let sound = new THREE.Audio(game.view.audioListener);
+  sound.setBuffer(game.assets['audio']['sound' + index]);
+  sound.setVolume(game.settings['audio']['sounds']);
+  sound.play();
+}
+
 function easeInOut(val) {
   return -0.5 * Math.cos(val * Math.PI) + 0.5;
 }
@@ -25,6 +47,11 @@ function loadAllAssets(game, renderProgressCallback) {
   return new Promise((resolve, reject) => {
     let todoList = {
       'audio/music.ogg': 1636930,
+      'audio/sound1.ogg': 34002,
+      'audio/sound2.ogg': 34629,
+      'audio/sound3.ogg': 25399,
+      'audio/sound4.ogg': 16426,
+      'audio/sound5.ogg': 26122,
       'fonts/cookie.json': 37866,
       'textures/cloud0a.png': 568,
       'textures/cloud0b.png': 569,
@@ -523,6 +550,7 @@ function animate(game, renderer, scene) {
        new THREE.Vector3().subVectors(word.position, game.objects.feather.position).length() < collectingRadius) {
       word.collected = game.view.clock.getElapsedTime();
       game.objects.words.collectedCount += 1;
+      playRandomSound(game);
     }
     if(word.parent != game.view.scene) {
       // All that happens in here is the positional animation for the word, which
@@ -972,9 +1000,9 @@ applySettings(window['game']);
 game.ui.root.querySelectorAll('button.goto').forEach((btn) => {
   btn.addEventListener('click', (e) => {
     if(!game.view.music) {
-      const audioListener = new THREE.AudioListener();
-      game.view.camera.add(audioListener);
-      game.view.music = new THREE.Audio(audioListener);
+      game.view.audioListener = new THREE.AudioListener();
+      game.view.camera.add(game.view.audioListener);
+      game.view.music = new THREE.Audio(game.view.audioListener);
       game.view.music.setBuffer(game.assets.audio.music);
       game.view.music.setVolume(game.settings['audio']['music']);
     }
@@ -1017,6 +1045,8 @@ game.ui.root.querySelectorAll('.options .audio button').forEach((btn) => {
           game.view.music.stop();
         }, 6000);
       }
+    } else if(e.target.classList.contains('sounds')) {
+      playRandomSound(game);
     }
   });
 });