Game Development Reference
In-Depth Information
final Ship ship;
long lastShotTime;
Random random;
Our world keeps track of a couple of things. We have a listener that we invoke when an
explosion happens or a shot is fired. Also, we keep track of how many waves of invaders the
player has already destroyed. The score member keeps track of the current score, and the
speedMultiplier member allows us to speed up the movement of the invaders (remember
the Invaders.update() method). Also, we store lists of the shots, invaders, and shield
blocks currently alive in the world. Finally, we have an instance of a Ship , and we store the
last time a shot was fired by the ship. We store this time in nanoseconds, as returned by
System.nanoTime() —hence the long data type. The Random instance will come in handy when
we want to decide whether an invader should fire a shot or not.
public World() {
ship = new Ship(0, 0, 0);
generateInvaders();
generateShields();
lastShotTime = System. nanoTime ();
random = new Random();
}
In the constructor, we create the Ship at its initial position, generate the invaders and shields,
and initialize the rest of the members.
private void generateInvaders() {
for ( int row = 0; row < 4; row++) {
for ( int column = 0; column < 8; column++) {
Invader invader = new Invader(− WORLD _ MAX _ X / 2 + column * 2f,
0, WORLD _ MIN _ Z + row * 2f);
invaders.add(invader);
}
}
}
The generateInvaders() method simply creates a grid of invaders, eight by four, arranged as
shown in Figure 12-11 .
private void generateShields() {
for ( int shield = 0; shield < 3; shield++) {
shields.add( new Shield(−10 + shield * 10-1, 0, -3));
shields.add( new Shield(−10 + shield * 10 + 0, 0, -3));
shields.add( new Shield(−10 + shield * 10 + 1, 0, -3));
shields.add( new Shield(−10 + shield * 10-1, 0, -2));
shields.add( new Shield(−10 + shield * 10 + 1, 0, -2));
}
}
The generateShields() method does pretty much the same: instantiating three shields
composed of five shield blocks each, as laid out in Figure 12-2 .
 
Search WWH ::




Custom Search