3D Game Programming

(C. Jardin) #1
requestAnimationFrame(animate);
TWEEN.update();
walk();
turn();
acrobatics();
renderer.render(scene, camera);
}
animate();

functionwalk() {
if(!isWalking())return;
varposition = Math.sin(clock.getElapsedTime()*5) * 50;
right_hand.position.z = position;
left_hand.position.z = -position;
right_foot.position.z = -position;
left_foot.position.z = position;
}

functionturn() {
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;

Appendix 1. Project Code • 228


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf