Game Development Reference
In-Depth Information
public void setWorldListener(WorldListener worldListener) {
this .listener = worldListener;
}
We also have a setter method to set a listener on the world. We'll use this to get informed about
events in the world and react accordingly, e.g. play a sound effect.
public void update( float deltaTime, float accelX) {
ship.update(deltaTime, accelX);
updateInvaders(deltaTime);
updateShots(deltaTime);
checkShotCollisions();
checkInvaderCollisions();
if (invaders.size() == 0) {
generateInvaders();
waves++;
speedMultiplier += 0.5f;
}
}
The update() method is surprisingly simple. It uses the current delta time, as well as the reading
on the accelerometer's y axis, which we can pass to Ship.update() . Once the ship has updated,
we call updateInvaders() and updateShots() , which are responsible for updating these two
types of objects. After all the objects in the world have been updated, we start checking for a
collision. The checkShotCollision() method will check collisions between any shots and the
ship and/or invaders.
Finally, we check whether the invaders are dead. If they are, we generate a new wave of
invaders. For love of the garbage collector (GC), we could reuse the old Invader instances, for
example, via the Pool class. However, to keep things simple, we simply create new instances.
The same is true for shots, by the way. Given the small number of objects we create in one
game session, the GC is unlikely to fire. If you want to make the GC really happy, just use a Pool
instance to reuse dead invaders and shots. Also, note that we increase the speed multiplier here!
private void updateInvaders( float deltaTime) {
int len = invaders.size();
for ( int i = 0; i < len; i++) {
Invader invader = invaders.get(i);
invader.update(deltaTime, speedMultiplier);
if (invader.state == Invader. INVADER _ ALIVE ) {
if (random.nextFloat() < 0.001f) {
Shot shot = new Shot(invader.position.x,
invader.position.y,
invader.position.z,
Shot. SHOT _ VELOCITY );
shots.add(shot);
listener.shot();
}
}
 
Search WWH ::




Custom Search