3D Game Programming

(C. Jardin) #1
avatar.position.set(-50, 50, 0);
scene.add(avatar);

avatar.setAngularFactor(new THREE.Vector3( 0, 0, 0 )); // no rotation
avatar.setLinearFactor(new THREE.Vector3( 1, 1, 0 )); // only move on X/Y axes
avatar.setLinearVelocity(new THREE.Vector3(0, 150, 0));

avatar.addEventListener('collision', function(object) {
if (object.is_fruit) {
scoreboard.addPoints(10);
avatar.setLinearVelocity(new THREE.Vector3(0, 50, 0));
scene.remove(object);
}
if (object == ground) {
game_over = true;
scoreboard.message("Game Over!");
}
});
return avatar;
}

function addScoreboard() {
var scoreboard = new Scoreboard();
scoreboard.score(0);
scoreboard.help('Use arrow keys to move and the space bar to jump');
return scoreboard;
}

var game_over = false;
function animate() {
if (game_over) return;

requestAnimationFrame(animate);
scene.simulate(); // run physics
renderer.render(scene, camera);
}

function gameStep() {
if (game_over) return;

launchFruit();
setTimeout(gameStep, 3*1000);
}

function launchFruit() {
var fruit = new Physijs.ConvexMesh(
new THREE.CylinderGeometry(20, 20, 1, 24),
new THREE.MeshBasicMaterial({visible: false})
);
var material = new THREE.MeshBasicMaterial({

report erratum • discuss

Code: The Purple Fruit Monster Game • 247


Prepared exclusively for Michael Powell

Free download pdf