3D Game Programming

(C. Jardin) #1
We’re doing just about everything there is to do with Scoreboard.js here. Most
of these are simple directions for how the scoreboard should look: it should
show the timer, the countdown timer, and some help text.

The last thing we do with the scoreboard is use a function to describe what
happens when time expires—set the scoreboard message to Game Over! and
call the gameOver() function.

We’ve already called this gameOver() function—we called it when the avatar
collided with the goal. So we know that gameOver() needs to account for the
case in which there’s still some time remaining. That is, if the avatar reaches
the goal before time runs out, the player has won the game. In this case, we
set the scoreboard message to Win!:

varpause = false;
functiongameOver() {
if(scoreboard.getTimeRemaining() > 0) scoreboard.setMessage('Win!');
scoreboard.stopCountdown();
scoreboard.stopTimer();
pause = true;
}

We also tell the scoreboard to stop its timers. Finally, we set a pause variable.
We’ll use pause to tell the animation and physics functions to stop running.
In both functions, if paused == true, then we return before they have a chance
to call themselves again. Update the animate() function with the line that checks
pause:

functionanimate() {
if(pause)return;
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();

And do the same for the gameStep() function:


functiongameStep() {
if(pause)return;
scene.simulate();
// Update physics 60 times a second so that motion is smooth
setTimeout(gameStep, 1000/60);
}
gameStep();

If you have everything working correctly, and if you’re very, very good, you
should now be able to win the game. You might even be able to beat my high
score!

report erratum • discuss

Winning the Game • 175


Prepared exclusively for Michael Powell

Free download pdf