3D Game Programming

(C. Jardin) #1
function checkForGameOver() {
if (raft.position.x < 250) return;

paused = true;
scoreboard.stopTimer();
scoreboard.message("You made it!");
if (scoreboard.getTime() < 30) scoreboard.addPoints(100);
if (scoreboard.getTime() < 25) scoreboard.addPoints(200);
if (scoreboard.getTime() < 20) scoreboard.addPoints(500);
}
var mass, velocity;
document.addEventListener("keydown", function(event) {
var code = event.keyCode;
if (code == 32) pushRaft(); // space
if (code == 38) pushRaft(); // up
if (code == 40) pushRaft(); // down
if (code == 37) rotateRaft(-1); // left
if (code == 39) rotateRaft(1); // right
if (code == 82) startGame(raft, river, scoreboard); // r
if (code == 80) { // p
paused = !paused;
if (paused) {
mass = raft.mass;
velocity = raft.getLinearVelocity();
raft.mass=0;
}
else {
raft.mass = mass;
raft.setLinearVelocity(velocity);
}
}
});

function pushRaft() {
var angle = raft.rotation.z;

raft.applyCentralForce(
new THREE.Vector3(
500 * Math.cos(angle),
0,
-500 * Math.sin(angle)
)
);
}
function rotateRaft(direction) {
raft.__dirtyRotation = true;
raft.rotation.z = raft.rotation.z + direction * Math.PI/10;
}
</script>

Appendix 1. Project Code • 272


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf