Game Development Reference
In-Depth Information
Also, this range helps to create a more rain-like effect by distributing the carrots in a
column full of carrots, so that they will hit the ground at different points in time. The
random rotation values are chosen within a range of a full circle ( 0 to 360 degrees),
which simply means that any angle of rotation can occur. The random scale values
are chosen within the range of half below or above the carrot's original size, which
gives the overall effect a more natural touch as carrots are not of the same size in
real life either.
Similar to what we did for the rock game object, a Box2D body, fixture, and shape
must be created for each carrot game object. It should be noted once more that the
reference of a Box2D body is stored in the body variable of the carrot instance to
activate the Box2D update mechanism for the game object. Also, notice that the
fixture is set to have a density of 50 , which affects the object's calculated mass data,
and thus controls whether it is a light or a heavy object. Furthermore, restitution
is set to 0.5 , which means that the carrots will be half-elastic to allow some rebound
to happen until it comes to rest eventually. A friction value of 0.5 lets the carrots
skid down on each other. Using a value of 1 or higher for friction will make the
object look sticky when it gets in contact with other objects.
As a last tweak, each Box2D shape is shrunk to half the size ( carrotShapeScale =
0.5f ) of the carrot game object to eliminate small gaps between the adjacent carrots.
Finally, each new carrot with its corresponding Box2D body is added to the level's
list of carrots for updating and rendering.
Next, add the following lines of code to the WorldController class:
private void onCollisionBunnyWithGoal () {
goalReached = true;
timeLeftGameOverDelay = Constants.TIME_DELAY_GAME_FINISHED;
Vector2 centerPosBunnyHead =
new Vector2(level.bunnyHead.position);
centerPosBunnyHead.x + = level.bunnyHead.bounds.width;
spawnCarrots(centerPosBunnyHead, Constants.CARROTS_SPAWN_MAX,
Constants.CARROTS_SPAWN_RADIUS);
}
This new method will handle the event when the player passes the goal-level object.
The goalReached flag is set to true , which will be used to avoid unnecessary
collision tests and start the countdown that will switch back to the menu screen.
The countdown starts at the value that is taken from the new constant TIME_DELAY_
GAME_FINISHED . Then, the spawnCarrots() method is called with the player's
current position, the number of carrots, and the spawn radius.
 
Search WWH ::




Custom Search