Game Development Reference
In-Depth Information
@Override
public void
resume() {
}
@Override
public void
dispose() {
}
}
The rest is just empty stubs so that we fulfill the
GLGame
interface definition. On to our final class:
WorldRenderer
!
The WorldRender Class
Let's recall what we have to render in 3D:
ï?®
The ship, using the ship model and texture, and applying lighting.
ï?®
The invaders, using the invader model and texture, again with lighting.
ï?®
Any shots on the playfield, based on the shot model, this time without
texturing but with lighting.
ï?®
The shield blocks, based on the shield block model, again without texturing,
but with lighting and transparency (see Figure
12-3
).
ï?®
Explosions instead of the ship or invader model, in case the ship or invader
is exploding. The explosion is not lit, of course.
We know how to code the first four items on this list. But what about the explosions?
It turns out that we can abuse the
SpriteBatcher
for this. Based on the state time of the
exploding ship or invader, we can fetch a
TextureRegion
from the
Animation
instance holding
the explosion animation (see
Assets
class). The
SpriteBatcher
can render textured rectangles
only in the x-y plane, so we have to find a way to move such a rectangle to an arbitrary position
in space (where the exploding ship or invader is located). We can easily achieve this by using
glTranslatef()
on the model-view matrix before rendering the rectangle via the
SpriteBatcher
!
The rendering setup for the other objects is pretty straightforward. We have a directional light
coming from the top right, and we have an ambient light to light all the objects a little, no matter
their orientation. The camera is located a little above and behind the ship, and it will look at a
point a little ahead of the ship. We use our
LookAtCamera
for this. To let the camera follow the
ship, we just need to keep the x coordinate of its position and the look-at point in sync with the
ship's x coordinate.
For some extra eye candy, we'll rotate the invaders around the y axis, and we'll rotate the ship
around the z axis based on its current velocity, so that it appears to be leaning in the direction in
which it is moving.
Let's put this into code! Listing 12-12 shows the final class of Android Invaders.