3D Game Programming

(C. Jardin) #1

}


if(code == 39) { // right
marker.position.x = marker.position.x+5;
is_moving_right = true;
}
if(code == 40) { // down
marker.position.z = marker.position.z+5;
is_moving_back = true;
}
if(code == 67) is_cartwheeling = !is_cartwheeling;// C
if(code == 70) is_flipping = !is_flipping; // F

if(detectCollisions()) {
if(is_moving_left) marker.position.x = marker.position.x+5;
if(is_moving_right) marker.position.x = marker.position.x-5;
if(is_moving_forward) marker.position.z = marker.position.z+5;
if(is_moving_back) marker.position.z = marker.position.z-5;
}
});

document.addEventListener('keyup',function(event) {
varcode = event.keyCode;

if(code == 37) is_moving_left = false;
if(code == 38) is_moving_forward = false;
if(code == 39) is_moving_right = false;
if(code == 40) is_moving_back = false;
});

functiondetectCollisions() {
varvector =newTHREE.Vector3(0, -1, 0);
varray =newTHREE.Ray(marker.position, vector);
varintersects = ray.intersectObjects(not_allowed);
if(intersects.length > 0)returntrue;
returnfalse;
}

functionjump() {
checkForTreasure();
animateJump();
}

functioncheckForTreasure() {
if(tree_with_treasure == undefined)return;

vartreasure_tree = trees[tree_with_treasure],
p1 = treasure_tree.parent.position,
p2 = marker.position;

vardistance = Math.sqrt(

Appendix 1. Project Code • 238


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf