3D Game Programming

(C. Jardin) #1
varmaterial = Physijs.createMaterial(
newTHREE.MeshBasicMaterial({color:0x333333}), 0.2, 1.0
);

varobstacle =newPhysijs.ConvexMesh(shape, material, 0);
obstacle.position.set(x, y, 0);
returnobstacle;
}

This builds two different kinds of obstacles—platforms and stalactites.^1


Now we’re ready to build levels. Create a new Levels object and add the first
two levels:

varlevels =newLevels(scoreboard, scene);
levels.addLevel([]);
levels.addLevel([
buildObstacle('platform', 0, 0.5 * height/2 * Math.random())
]);

The first level has nothing in it and the second has a platform from the
buildObstacle() function.

The last thing we need to do is call that levelUp() method when the player
reaches a goal on the current level. To do that, find the code that adds the
collision event listener to the avatar. It should look something like this:

avatar.addEventListener('collision',function(object) {
if(object.isGoal) gameOver();
});

Delete this code.


Now back down below our levels code, add the following:


avatar.addEventListener('collision',function(object) {
if(!object.isGoal)return;
if(!levels.hasMoreLevels())returngameOver();
moveGoal();
levels.levelUp();
});

This changes the collision code slightly. We still do nothing if the avatar col-
lides with something that’s not a goal (like the walls, ramps, and obstacles).
We also add a check to see if there are any more levels. If there are no more
levels, then we return the results of the gameOver() function. If levels remain,
then we move the goal and level up.


  1. Stalactites are on the ceiling. Stalagmites are on the ground. Stalactites hang on tight
    to the ceiling. Stalagmites start on the ground and might reach the ceiling.


Chapter 19. Project: Multilevel Game • 182


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf