Game Development Reference
In-Depth Information
* (
WORLD_WIDTH
- Platform.
PLATFORM_WIDTH
)
+ Platform.
PLATFORM_WIDTH
/ 2;
Platform platform =
new
Platform(type, x, y);
platforms.add(platform);
if
(rand.nextFloat() > 0.9f
&& type != Platform.
PLATFORM_TYPE_MOVING
) {
Spring spring =
new
Spring(platform.position.x,
platform.position.y + Platform.
PLATFORM_HEIGHT
/ 2
+ Spring.
SPRING_HEIGHT
/ 2);
springs.add(spring);
}
if
(y >
WORLD_HEIGHT
/ 3 && rand.nextFloat() > 0.8f) {
Squirrel squirrel =
new
Squirrel(platform.position.x
+ rand.nextFloat(), platform.position.y
+ Squirrel.
SQUIRREL_HEIGHT
+ rand.nextFloat() * 2);
squirrels.add(squirrel);
}
if
(rand.nextFloat() > 0.6f) {
Coin coin =
new
Coin(platform.position.x + rand.nextFloat(),
platform.position.y + Coin.
COIN_HEIGHT
+ rand.nextFloat() * 3);
coins.add(coin);
}
y += (maxJumpHeight - 0.5f);
y -= rand.nextFloat() * (maxJumpHeight / 3);
}
castle =
new
Castle(
WORLD_WIDTH
/ 2, y);
}
Let's outline the general idea of the algorithm in plain words:
1.
Start at the bottom of the world at y = 0.
2.
As long as we haven't reached the top of the world yet, do the following:
a. Create a platform, either moving or stationary, at the current y position with a
random x position.
b. Fetch a random number between 0 and 1 and, if it is greater than 0.9 and if the
platform is not moving, create a spring on top of the platform.
c. If we are above the first third of the level, fetch a random number and, if it is
above 0.8, create a squirrel offset randomly from the platform's position.
d. Fetch a random number and, if it is greater than 0.6, create a coin offset randomly
from the platform's position.