3D Game Programming

(C. Jardin) #1
mesh.addEventListener('collision', function() {
var list_index = game_items.indexOf(mesh);
game_items.splice(list_index, 1);
scene.remove(mesh);
scoreboard.addPoints(200);
scoreboard.message('Yum!');
setTimeout(function() {scoreboard.clearMessage();}, 2.5* 1000);
});

ground.updateMatrixWorld();
var p = new THREE.Vector3(location.x, location.y, -20);
ground.localToWorld(p);
mesh.position.copy(p);
scene.add(mesh);
return mesh;
}

// Animate motion in the game
function animate() {
requestAnimationFrame(animate);
if (paused) return;

updateCamera();
renderer.render(scene, camera);
}
animate();

// Run physics
function gameStep() {
// Update physics 60 times a second so that motion is smooth
setTimeout(gameStep, 1000/60);

if (paused) return;

updateScore();
checkForGameOver();
scene.simulate();
}
gameStep();

var next_x;
function updateScore() {
if (!next_x) next_x = raft.position.x + 25;
if (raft.position.x > next_x) {
scoreboard.addPoints(10);
next_x = next_x + 25;
}
}

report erratum • discuss

Code: River Rafting • 271


Prepared exclusively for Michael Powell

Free download pdf