Game Development Reference
In-Depth Information
e.
Increase y by the maximum normal jump height of Bob, decrease it a tiny bit
randomly—but only so far that it doesn't fall below the last y value—and go to the
beginning of step 2.
3.
Place the castle at the last y position, centered horizontally.
The big secret of this procedure is how we increase the y position for the next platform in step
2e. We have to make sure that each subsequent platform is reachable by Bob by jumping from
the current platform. Bob can only jump as high as gravity allows, given his initial jump velocity
of 11 m/s vertically. How can we calculate how high Bob will jump? We can do this with the
following formula:
(
)
( )
height velocity velocity / 2
=
×
×
gravity 11 11 / 2
= ×
13 ~ 4.6m
×
=
This means that we should have a distance of 4.6 meters vertically between each platform so
that Bob can still reach it. To make sure that all platforms are reachable, we use a value that's
a little bit less than the maximum jump height. This guarantees that Bob will always be able to
jump from one platform to the next. The horizontal placement of platforms is again random.
Given Bob's horizontal movement speed of 20 m/s, we can be more than sure that he will be
able to reach a platform not only vertically but also horizontally.
The other objects are created based on chance. The method Random.nextFloat() returns
a random number between 0 and 1 on each invocation, where each number has the same
probability of occurring. Squirrels are only generated when the random number we fetch from
Random is greater than 0.8. This means that we'll generate a squirrel with a probability of 20
percent (1 - 0.8). The same is true for all other randomly created objects. By tuning these values,
we can have more or fewer objects in our world.
Updating the World
Once we have generated our world, we can update all objects in it and check for collisions.
Listing 9-17 shows the update methods of the World class, with comments.
Listing 9-17. Excerpt from World.java; the Update Methods
public void update( float deltaTime, float accelX) {
updateBob(deltaTime, accelX);
updatePlatforms(deltaTime);
updateSquirrels(deltaTime);
updateCoins(deltaTime);
if (bob.state != Bob. BOB_STATE_HIT )
checkCollisions();
checkGameOver();
}
The method update() is the one called by our game screen later on. It receives the delta time
and acceleration on the x axis of the accelerometer as an argument. It is responsible for calling
the other update methods, as well as performing the collision checks and game-over check.
We have an update method for each object type in our world.
 
Search WWH ::




Custom Search