3D Game Programming

(C. Jardin) #1
functiongameStep() {
// Update physics 60 times a second so that motion is smooth
setTimeout(gameStep, 1000/60);

if(paused)return;
checkForGameOver();
scene.simulate();
}

Note that we’ve changed the order of the function calls in gameStep() to work
with pausing. The scene.simulate() and checkForGameOver() calls both come after
the if (paused) statement—there’s no sense in simulating physics or checking
if the game is over when the game is paused.

The checkForGameOver() function is new. It can go right after the gameStep()
function and should look like this:

functioncheckForGameOver() {
if(raft.position.x < 250)return;
paused = true;
scoreboard.stopTimer();
scoreboard.message("You made it!");
}

If the raft’s X position has not reached the finish line, or when X is 250 , then
this function does nothing—it returns immediately and nothing else happens.
If the raft has reached the finish line, then we set paused to true so that all the
other functions can stop working. We also stop the scoreboard timer and add
a message to display.

The game should pause at the end of the river and display the time it took
the player to complete the race, and a message of “You made it!” You might
even be able to make it pretty fast:

Scoring Points by Distance


In some games, a player receives points simply for making it further away
from the starting point. Keeping score is something that belongs in the
gameStep() method because it’s game logic, rather than animation, which would
belong in the animate() function.

Chapter 20. Project: River Rafting • 200


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf