Game Development Reference
In-Depth Information
And that's all the classes that make up our game world! Compare that to what we had in Super
Jumper. The principles are nearly the same, and the code looks quite similar. Android Invaders
is, of course, a very simple game, so we can get away with simple solutions, such as using
bounding spheres for everything. For many simple 3D games, that's all you'll need. On to the last
two parts of our game: the
GameScreen
class and the
WorldRenderer
class!
The GameScreen Class
Once the game transitions to the
GameScreen
class, the player can immediately start playing
without having to state that he or she is ready. The only states we have are these:
ï?®
The running state, where we render the background, the world, and the
UI elements, as shown in Figure
12-4
ï?®
The paused state, where we render the background, the world, and the
paused menu, as shown in Figure
12-4
ï?®
The game-over state, where we render pretty much the same thing as for
the pause state.
We'll follow the same pattern we used in Super Jumper and have different
update()
and
present()
methods for each of the three states.
The most interesting part of this class is how we handle the user input to move the ship. We
want our player to be able to control the ship with either onscreen buttons or the accelerometer.
We can read the
Settings.touchEnabled
field to figure out what the user wants in regard to
this. Depending on which input method is active, we decide on whether to render the onscreen
buttons or not, and pass the proper accelerometer values to the
World.update()
method to
move the ship.
If the player chooses the onscreen buttons, we don't need to use the accelerometer values,
of course; instead, we just pass a constant artificial acceleration value to the
World.update()
method. It has to be in the range −10 (left) to 10 (right). After a little experimentation, we arrived
at a value of −5 for left movement and 5 for right movement via the onscreen buttons.
The other interesting part of this class is the way we combine the rendering of the 3D game
world and the 2D UI elements. Let's take a look at the code of the
GameScreen
class in
Listing 12-11.
Listing 12-11. GameScreen.java, the Game Screen
package
com.badlogic.androidgames.androidinvaders;
import
java.util.List;
import
javax.microedition.khronos.opengles.GL10;
import
com.badlogic.androidgames.androidinvaders.World.WorldListener;
import
com.badlogic.androidgames.framework.Game;
import
com.badlogic.androidgames.framework.Input.TouchEvent;
import
com.badlogic.androidgames.framework.gl.Camera2D;
import
com.badlogic.androidgames.framework.gl.FPSCounter;