3D Game Programming

(C. Jardin) #1
vardirection = 0;
if(is_moving_forward) direction = Math.PI;
if(is_moving_back) direction = 0;
if(is_moving_right) direction = Math.PI/2;
if(is_moving_left) direction = -Math.PI/2;

spinAvatar(direction);
}

functionspinAvatar(direction) {
newTWEEN
.Tween({y: avatar.rotation.y})
.to({y: direction}, 100)
.onUpdate(function() {
avatar.rotation.y = this.y;
})
.start();
}

varis_cartwheeling = false;
varis_flipping = false;
functionacrobatics() {
if(is_cartwheeling) {
avatar.rotation.z = avatar.rotation.z + 0.05;
}
if(is_flipping) {
avatar.rotation.x = avatar.rotation.x + 0.05;
}
}

varis_moving_left, is_moving_right, is_moving_forward, is_moving_back;
functionisWalking() {
if(is_moving_right)returntrue;
if(is_moving_left)returntrue;
if(is_moving_forward)returntrue;
if(is_moving_back)returntrue;
returnfalse;
}

document.addEventListener('keydown',function(event) {
varcode = event.keyCode;
if(code == 32) jump(); // space

if(code == 37) { // left
marker.position.x = marker.position.x-5;
is_moving_left = true;
}
if(code == 38) { // up
marker.position.z = marker.position.z-5;
is_moving_forward = true;

report erratum • discuss

Code: Fruit Hunt • 237


Prepared exclusively for Michael Powell

Free download pdf