Game Development Reference
In-Depth Information
The World Class
The last class we have to discuss is the World class. It's a little longer, so we'll split it up.
Listing 9-15 shows the first part of the code.
Listing 9-15. Excerpt from World.java; Constants, Members, and Initialization
package com.badlogic.androidgames.jumper;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.badlogic.androidgames.framework.math.OverlapTester;
import com.badlogic.androidgames.framework.math.Vector2;
public class World {
public interface WorldListener {
public void jump();
public void highJump();
public void hit();
public void coin();
}
The first thing we define is an interface called WorldListener . What does it do? We need it to
solve a little MVC problem: when do we play sound effects? We could just add invocations of
Assets.playSound() to the respective simulation classes, but that's not very clean. Instead, we'll
let a user of the World class register a WorldListener , which will be called when Bob jumps from
a platform, jumps from a spring, gets hit by a squirrel, or collects a coin. We will later register a
listener that plays back the proper sound effects for each of those events, keeping the simulation
classes clean from any direct dependencies on rendering and audio playback.
public static final float WORLD _ WIDTH = 10;
public static final float WORLD _ HEIGHT = 15 * 20;
public static final int WORLD _ STATE _ RUNNING = 0;
public static final int WORLD _ STATE _ NEXT _ LEVEL = 1;
public static final int WORLD _ STATE _ GAME _ OVER = 2;
public static final Vector2 gravity = new Vector2(0, -12);
Next, we define a couple of constants. The WORLD_WIDTH and WORLD_HEIGHT specify the extents
of our world horizontally and vertically. Remember that our view frustum will show a region
of 10×15 meters of our world. Given the constants defined here, our world will span 20 view
frustums or screens vertically. Again, that's a value we came up with by tuning. We'll get back
to it when we discuss how we generate a level. The world can also be in one of three states:
running, waiting for the next level to start, or the game-over state—when Bob falls too far
(outside of the view frustum). We also define our gravity acceleration vector as a constant here.
public final Bob bob;
public final List<Platform> platforms;
public final List<Spring> springs;
public final List<Squirrel> squirrels;
 
Search WWH ::




Custom Search