3D Game Programming

(C. Jardin) #1
If you want to increase the score as the raft makes its way along the river,
we can add a call to the updateScore() function in gameStep():

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

if(paused)return;

updateScore();
checkForGameOver();
scene.simulate();
}

Due to the way we have the river scene rotated, the raft’s X position changes
as it moves down the river. To increase the score as the raft reaches the next
25 units of distance, we can use the following (the code goes below the
gameStep() function):

varnext_x;
functionupdateScore() {
if(!next_x) next_x = raft.position.x + 25;
if(raft.position.x > next_x) {
scoreboard.addPoints(10);
next_x = next_x + 25;
}
}

Each time the raft reaches the next X scoring point, this function adds ten
points to the score and recalculates the next X scoring area to be 25 units
further away.

Another distance-based scoring feature we can add is a time bonus for finish-
ing the rafting course within a certain amount of time. We do this by adding
the last three lines shown here to the checkForGameOver() function:

functioncheckForGameOver() {
if(raft.position.x < 250)return;

paused = true;
scoreboard.stopTimer();
scoreboard.message("You made it!");
if(scoreboard.getTime() < 30) scoreboard.addPoints(100);
if(scoreboard.getTime() < 25) scoreboard.addPoints(200);
if(scoreboard.getTime() < 20) scoreboard.addPoints(500);
}

If the player finishes in less than 30 seconds, an additional 100 points are
awarded. If the player finishes in less than 25 seconds, then both the 100

report erratum • discuss

Setting the Finish Line • 201


Prepared exclusively for Michael Powell

Free download pdf