3D Game Programming

(C. Jardin) #1
map: THREE.ImageUtils.loadTexture('/images/fruit.png'),
transparent: true
});
var picture = new THREE.Mesh(new THREE.PlaneGeometry(40, 40), material);
picture.rotation.x = -Math.PI/2;
fruit.add(picture);

fruit.is_fruit = true;
fruit.setAngularFactor(new THREE.Vector3( 0, 0, 1 ));
fruit.setLinearFactor(new THREE.Vector3( 1, 1, 0 ));
fruit.position.set(300, 20, 0);
fruit.rotation.x = Math.PI/2;
scene.add(fruit);
fruit.setLinearVelocity(
new THREE.Vector3(-150, 0, 0)
);
}

document.addEventListener("keydown", function(event) {
var code = event.keyCode;

if (code == 37) left(); // left arrow
if (code == 39) right(); // right arrow
if (code == 38) up(); // up arrow
if (code == 32) up(); // space bar
if (code == 82) reset(); // R
});

function left() { move(-50, 0); }
function right() { move(50, 0); }
function up() { move(avatar.getLinearVelocity().x, 50); }

function move(x, y) {
avatar.setLinearVelocity(
new THREE.Vector3(x, y, 0)
);
}

function reset() {
avatar.__dirtyPosition = true;
avatar.position.set(-50, 50, 0);
avatar.setLinearVelocity(new THREE.Vector3(0, 150, 0));

for (var i in scene._objects) {
if (scene._objects[i].is_fruit) {
scene.remove(scene._objects[i]);
}
}

scoreboard.score(0);

Appendix 1. Project Code • 248


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf