Up-in-the-Air – commitdiff

You can use Git to clone the repository via the web URL. Download snapshot (zip)
Gamepad support now working
authorJulian Fietkau <git@fietkau.software>
Thu, 26 Sep 2024 04:05:24 +0000 (06:05 +0200)
committerJulian Fietkau <git@fietkau.software>
Thu, 26 Sep 2024 04:05:24 +0000 (06:05 +0200)
index.html
main.js

index c3912382e69f8a614f5630aaaf9e04ddba2bd283..a62466a86bae715f0a8396e4103e0fa95d9573f3 100644 (file)
 <span class="touchpad">The game is controlled through touch movements on an on-screen touchpad. Movements correspond directly to the play area.</span>
 <span class="thumbstick">The game is controlled through touch movements on an on-screen thumbstick. Directional movements steer the cursor.</span>
 <span class="keyboard">The game is controlled through arrow or WASD key presses. Key rebinding and additional accessibility options are available.</span>
-<span class="gamepad">The game is controlled using the thumbstick or d-pad of an attached gamepad accessory.</span>
+<span class="gamepad">The game is controlled using the thumbstick of an attached gamepad accessory.</span>
 </p>
 </div>
 <div class="graphics">
diff --git a/main.js b/main.js
index c2e929d8b6809857ece1821f00ac3b7ea557378c..0df42844d2080f66e39b49c47704ebe0ed9e0718 100644 (file)
--- a/main.js
+++ b/main.js
@@ -458,6 +458,37 @@ function animate(game, renderer, scene) {
   let delta = Math.min(game.view.clock.getDeltaTime(), 1 / 12) * (game.settings['difficulty']['speed'] / 100);
   game.timeProgress = (game.timeProgress + delta);
 
+  if(game.settings['controls'] == 'gamepad') {
+    let speedScale = 7.0;
+    let deadZone = 0.2;
+    let speedX = [];
+    let speedY = [];
+    for(let gamepad of game.ui.gamepads) {
+      let sx = gamepad.axes[0];
+      if(Math.abs(sx) < deadZone) {
+        sx = 0.0;
+      }
+      speedX.push(sx);
+      let sy = gamepad.axes[1];
+      if(Math.abs(sy) < deadZone) {
+        sy = 0.0;
+      }
+      speedY.push(sy);
+    }
+    if(speedX.length > 0) {
+      speedX = speedX.reduce((s, a) => s + a, 0) / speedX.length;
+    } else {
+      speedX = 0;
+    }
+    if(speedY.length > 0) {
+      speedY = speedY.reduce((s, a) => s + a, 0) / speedY.length;
+    } else {
+      speedY = 0;
+    }
+    game.controls.speedX = speedScale * speedX;
+    game.controls.speedY = -1 * speedScale * speedY;
+  }
+
   const maxPinwheelSpeed = 8.0;
   const maxPinwheelDistance = 5.0;
   game.controls.speedX += delta * game.controls.accelX;
@@ -1175,6 +1206,7 @@ window['game'] = {
   state: 'loadingAssets',
   ui: {
     root: document.querySelector('.game-upintheair'),
+    gamepads: [],
   },
   settings: {},
 };
@@ -1373,6 +1405,14 @@ document.addEventListener('keydown', (e) => {
     }
   }
 });
+window.addEventListener('gamepadconnected', (e) => {
+  game.ui.gamepads.push(e.gamepad);
+});
+window.addEventListener('gamepaddisconnected', (e) => {
+  if(game.ui.gamepads.includes(e.gamepad)) {
+    game.ui.gamepads.splice(game.ui.gamepads.indexOf(e.gamepad), 1);
+  }
+});
 game.ui.root.querySelector('.ui-page.pause button.title').addEventListener('click', () => {
   reset(game);
 });