Coding a stripped down GameObjectFactory

The next step is to code the GameObjectFactory class. This class will look very similar to the class of the same name in the previous project but there will be a few differences that I will point out. We will code just enough in order to build the game objects that are ready to be built and we will revisit this class in the next chapter once we have finished coding all the component classes.

Tip

In this and other upcoming classes remember to check that when you import your own classes (everything from the GOSpec package) make sure/change to the correct package name.

Add a new class called GameObjectFactory and add the following member variables and constructor method.

import android.content.Context;
import android.graphics.PointF;

import com.gamecodeschool.platformer.GOSpec.GameObjectSpec;

class GameObjectFactory {
    private Context mContext;
    private GameEngine mGameEngineReference;
    private int mPixelsPerMetre;

    GameObjectFactory(Context context, 
                      GameEngine gameEngine, 
                      int pixelsPerMetre) {
        
        mContext = context;
        mGameEngineReference = gameEngine;
        mPixelsPerMetre = pixelsPerMetre;
    }
}

The GameObjectFactory needs three members. A Context and an int with the number of pixels for every metre of the game world so it can pass them to the graphics-related component classes and a GameEngine reference, so it can pass it to any of the input-related component classes, so they can register as observers. These three members (mContext, mGameEnginrReference and mPixelsPerMetre) are initialized in the constructor.

Next add the create method. We will come back to this method in the next chapter and add more code to the switch block.

GameObject create(GameObjectSpec spec, PointF location) {
   GameObject object = new GameObject();

   int mNumComponents = spec.getComponents().length;
   object.setTag(spec.getTag());

   // First give the game object the
   // right kind of transform

   switch(object.getTag()){
          case "Background":
                // Code coming soon
                break;

          case "Player":
                // Code coming soon
                break;

          default:// normal transform
                object.setTransform(new Transform(
                            spec.getSpeed(),
                            spec.getSize().x,
                            spec.getSize().y,
                            location));
                break;
   }


   // Loop through and add/initialize all the components
   for (int i = 0; i < mNumComponents; i++) {
         switch (spec.getComponents()[i]) {
               case "PlayerInputComponent":
                     // Code coming soon
                     break;
               case "AnimatedGraphicsComponent":
                     // Code coming soon
                     break;
               case "PlayerUpdateComponent":
                    // Code coming soon
                    break;
               case "InanimateBlockGraphicsComponent":
                     object.setGraphics(new            
                              InanimateBlockGraphicsComponent(),
                              mContext, spec, spec.getSize(),
                              mPixelsPerMetre);
                     break;
               case "InanimateBlockUpdateComponent":
                     object.setMovement(new             
                     InanimateBlockUpdateComponent());
                     break;
               case "MovableBlockUpdateComponent":
                    // Code coming soon
                    break;
               case "DecorativeBlockUpdateComponent":
                     object.setMovement(new             
                     DecorativeBlockUpdateComponent());
                     break;
               case "BackgroundGraphicsComponent":
                     // Code coming soon
                     break;
               case "BackgroundUpdateComponent":
                     // Code coming soon
                     break;

               default:
                     // Error unidentified component
                     break;
      }
   }

   // Return the completed GameObject
   // to the LevelManager class
   return object;
}

First, in the create method a new instance of GameObject is declared and initialized. Just as we did in the previous project we capture the number of components in the current specification and then call the setTag method on the GameObject instance. The GameObject can now be properly identified by a tag.

Next, we see something new compared to the previous GameObjectFactory from the Scrolling Shooter project. We switch based on the tag of the specification. There are three possible case statements that can be executed. One for "Background", one for "Player" and a default as well.

For now, we just add code to the default option that calls the setTransform method on the GameObject instance and passes in a new Transform reference. In the next chapter we will extend Transform twice to make special versions for the player and the backgrounds. This is how we make sure that every object gets the Transform it needs.

Next, we loop round a for loop once for each component in the specification. And just as we did in the previous project we initialize the appropriate component at each case statement by calling either setGraphics or setMovement on the GameObject instance and passing in a new component of the appropriate type according to the specification.

Also note I have added all the case statements to handle all the other components although most of them are currently empty. This will make it easy to show where the new code goes in the next chapter.

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

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