From ecf4c24338869c733086e0f7447758cb68efcc09 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Mon, 31 Jan 2022 09:15:16 +0100 Subject: [PATCH] Handle OOB + fix gyro controls --- game.js | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/game.js b/game.js index 3c12a36..4b15674 100644 --- a/game.js +++ b/game.js @@ -85,10 +85,10 @@ const spawnInterval = 40; let lastSpawned = 0; var gn; -var headSet = false; -var betaReference = 0.0; -var leftRightMove = 0.0; -var upDownMove = 0.0; +let headSet = false; +let gammaReference = 0.0; +let leftRightMove = 0.0; +let upDownMove = 0.0; var speed = 5.0; let score = 0; @@ -172,12 +172,12 @@ function start_gn() { function gnCallBack(data) { if (!headSet) { - betaReference = data.do.beta; + gammaReference = data.do.gamma; } else { - let leftRight = data.do.alpha > 180.0 ? "right" : "left"; - leftRightMove = data.do.alpha > 180.0 ? 360.0 - data.do.alpha : -data.do.alpha; - let upDown = data.do.beta > betaReference ? "up" : "down"; - upDownMove = data.do.beta - betaReference; + // leftRight = data.do.beta > 0 ? "right" : "left"; + leftRightMove = data.do.beta; + // upDown = data.do.gamma > gammaReference ? "up" : "down"; + upDownMove = data.do.gamma - gammaReference; } } @@ -187,7 +187,6 @@ function set_head_gn() { } document.getElementById("set_head").addEventListener("click", () => { - document.getElementById("log").innerHTML += "clicked button
"; set_head_gn(); }); @@ -318,6 +317,11 @@ function animate() { if (running) { + if (Math.abs(camera.position.x) > diameterOfTunnel || Math.abs(camera.position.y) > diameterOfTunnel) { + // out of bounds + gameOver(); + } + for (let i = 0; i < cubes.length; i++) { if (cubes[i].material !== materialCube) { continue; @@ -339,11 +343,18 @@ function animate() { break; } } + } + + if (running) { camera.position.z -= 0.5 * speed; speed += 0.01; - camera.position.x += 0.1 * leftRightMove; - camera.position.y += 0.1 * upDownMove; + if (!Number.isNaN(leftRightMove)) { + camera.position.x += 0.1 * leftRightMove; + } + if (!Number.isNaN(upDownMove)) { + camera.position.y += 0.1 * upDownMove; + } while (camera.position.z < lastSpawned - spawnInterval) { lastSpawned -= spawnInterval; @@ -369,7 +380,6 @@ function animate() { scene.add(meshCube); cubes.push(meshCube); } - } renderer.render(scene, camera);