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;
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<br>";
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);