Spawning the Aliens

Now that all the alien components as well as the ALienLaserSpawner are coded we can put them all to work in the game. It will take three steps as follows:

  1. Update the GameEngine's deSpawnReSpawn method to spawn some of each alien.
  2. Update the Level class to add some aliens and alien lasers to the ArrayList of objects.
  3. Update GameObjectFactory to handle instantiating the correct component classes (that we just coded) when the level class requests the various alien GameObject instances be built.

Let's complete those steps now.

Updating GameEngine

Add this code to the end of the deSpawnReSpawn method.

for (int i = Level.FIRST_ALIEN; 
   i != Level.LAST_ALIEN + 1; i++) {
   
   objects.get(i).spawn(objects
      .get(Level.PLAYER_INDEX).getTransform());
}

This loops through the appropriate indexes of the objects ArrayList and spawns the aliens. The alien lasers will be spawned by the spawnAlienLaser method when requested by an alien (chaser or patroller).

Next, we will update the Level class

Updating Level

Add this code into the end of the buildGameObjects method. You can identify exactly where it goes by the pre-existing comments and return statement that I have highlighted in the next code:

// Create some aliens
objects.add(FIRST_ALIEN, factory
          .create(new AlienChaseSpec()));
objects.add(SECOND_ALIEN, factory
          .create(new AlienPatrolSpec()));
objects.add(THIRD_ALIEN, factory
          .create(new AlienPatrolSpec()));
objects.add(FOURTH_ALIEN, factory
          .create(new AlienChaseSpec()));
objects.add(FIFTH_ALIEN, factory
          .create(new AlienDiverSpec()));
objects.add(SIXTH_ALIEN, factory
          .create(new AlienDiverSpec()));

// Create some alien lasers
for (int i = FIRST_ALIEN_LASER; i != LAST_ALIEN_LASER + 1; i++) {
   objects.add(i, factory
               .create(new AlienLaserSpec()));
}
mNextAlienLaser = FIRST_ALIEN_LASER;

return objects;

In the previous code we use the final variables of the Level class to add aliens with different specifications into the objects ArrayList at the required positions.

Now that we are calling create with these specifications we will need to update GameObjectFactory so it knows how to handle them.

Updating GameObjectFactory

Add the highlighted case blocks to the switch statement in the create method of the GameObjectFactory class:

case "BackgroundSpawnComponent":
   object.setSpawner(new BackgroundSpawnComponent());
   break;
   
case "AlienChaseMovementComponent":
   object.setMovement(
                 new AlienChaseMovementComponent(
                 mGameEngineReference));
   break;
case "AlienPatrolMovementComponent":
   object.setMovement(
                 new AlienPatrolMovementComponent(
                 mGameEngineReference));
   break;
case "AlienDiverMovementComponent":
   object.setMovement(
                new AlienDiverMovementComponent());
   break;
case "AlienHorizontalSpawnComponent":
   object.setSpawner(
               new AlienHorizontalSpawnComponent());
   break;
case "AlienVerticalSpawnComponent":
   object.setSpawner(
               new AlienVerticalSpawnComponent());
   break;

default:
   // Error unidentified component
   break;

The new code simply detects the various alien-related components and then initializes them and adds them to the GameObject under construction in the same way that we initialized the other movement and spawn-related components when we handled the player's components.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.76.135