3D Game Programming

(C. Jardin) #1
points and 200 more are awarded. If the player finishes in less than 20 sec-
onds, then the 100 and the 200 points are awarded, along with an additional
500 points, for a possible total of 800 extra points to be won. Can you do it?

Power-Up Points


Many games reward a player for capturing bonus items. If we want to do that
in our raft game, we have to do two things: add those items to the river and
add to the player’s score when the raft bumps into those objects.

We’ll want to add items to the game whenever it gets reset, so much of our
work needs to take place in and after the startGame() function. But first, we
need to declare a variable to hold the list of items in the game. Add it before
the paused variable and the first call to startGame() in the code outline:

vargame_items = [];
varpaused;
startGame(raft, river, scoreboard);

Then, in the definition of startGame(), add a call to resetItems():


functionstartGame(raft, river, scoreboard) {
varstart = river.river_points[100];
raft.__dirtyPosition = true;
raft.position.set(start.y, start.z + 100, 0);
raft.setLinearVelocity(newTHREE.Vector3());
scoreboard.resetTimer();
scoreboard.score(0);
scoreboard.clearMessage();
updateCamera();
camera.lookAt(newTHREE.Vector3(start.y, 0, 0));
resetItems(river, scoreboard);
paused = false;
}

Since the call to resetItems() comes after the camera changes, the definition of
the resetItems() function should come after camera functions like updateCamera().
The definition of this function is simple enough. It calls two other func-
tions—one to remove all existing items from the screen and the other to add
new items to the screen:

functionresetItems(ground, scoreboard) {
removeItems();
addItems(ground, scoreboard);
}

Removing items from the game is a simple matter of removing each one from
the scene. Once each item has been removed from the scene, we can set the
list of game items to an empty list:

Chapter 20. Project: River Rafting • 202


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf