Handle OOB + fix gyro controls

This commit is contained in:
Arne Keller 2022-01-31 09:15:16 +01:00
parent d5bb16fd56
commit ecf4c24338

36
game.js
View File

@ -85,10 +85,10 @@ const spawnInterval = 40;
let lastSpawned = 0; let lastSpawned = 0;
var gn; var gn;
var headSet = false; let headSet = false;
var betaReference = 0.0; let gammaReference = 0.0;
var leftRightMove = 0.0; let leftRightMove = 0.0;
var upDownMove = 0.0; let upDownMove = 0.0;
var speed = 5.0; var speed = 5.0;
let score = 0; let score = 0;
@ -172,12 +172,12 @@ function start_gn() {
function gnCallBack(data) { function gnCallBack(data) {
if (!headSet) { if (!headSet) {
betaReference = data.do.beta; gammaReference = data.do.gamma;
} else { } else {
let leftRight = data.do.alpha > 180.0 ? "right" : "left"; // leftRight = data.do.beta > 0 ? "right" : "left";
leftRightMove = data.do.alpha > 180.0 ? 360.0 - data.do.alpha : -data.do.alpha; leftRightMove = data.do.beta;
let upDown = data.do.beta > betaReference ? "up" : "down"; // upDown = data.do.gamma > gammaReference ? "up" : "down";
upDownMove = data.do.beta - betaReference; upDownMove = data.do.gamma - gammaReference;
} }
} }
@ -187,7 +187,6 @@ function set_head_gn() {
} }
document.getElementById("set_head").addEventListener("click", () => { document.getElementById("set_head").addEventListener("click", () => {
document.getElementById("log").innerHTML += "clicked button<br>";
set_head_gn(); set_head_gn();
}); });
@ -318,6 +317,11 @@ function animate() {
if (running) { 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++) { for (let i = 0; i < cubes.length; i++) {
if (cubes[i].material !== materialCube) { if (cubes[i].material !== materialCube) {
continue; continue;
@ -339,11 +343,18 @@ function animate() {
break; break;
} }
} }
}
if (running) {
camera.position.z -= 0.5 * speed; camera.position.z -= 0.5 * speed;
speed += 0.01; speed += 0.01;
camera.position.x += 0.1 * leftRightMove; if (!Number.isNaN(leftRightMove)) {
camera.position.y += 0.1 * upDownMove; camera.position.x += 0.1 * leftRightMove;
}
if (!Number.isNaN(upDownMove)) {
camera.position.y += 0.1 * upDownMove;
}
while (camera.position.z < lastSpawned - spawnInterval) { while (camera.position.z < lastSpawned - spawnInterval) {
lastSpawned -= spawnInterval; lastSpawned -= spawnInterval;
@ -369,7 +380,6 @@ function animate() {
scene.add(meshCube); scene.add(meshCube);
cubes.push(meshCube); cubes.push(meshCube);
} }
} }
renderer.render(scene, camera); renderer.render(scene, camera);