Coding the player's and the background's empty component classes

By coding an empty class for each player related component, it will allow us to more quickly write the code to get the game running. We can then flesh out the real/full code for each component as we proceed without the need of dipping into the same class (mainly GameObject) multiple times.

In this chapter we will deal with the player (and his lasers) and the background. Coding the empty outlines will also allow us to code an error free GameObject class that will hold all these components and we will be able to see how the components interact with the game engine via the GameObject class before coding the details inside each component.

Each of the components will implement one of the interfaces we coded in the previous section. We will add just enough code for each class to fulfil its contractual obligations to the interface and thus not cause any errors. We will also make very minor changes outside the component classes to smooth development along, but I will cover the details as we get to the appropriate part.

We will put the missing code in after we have coded the GameObject class later in the chapter. If you want to sneak a look at the details inside the various component methods, you can look ahead to the Completing the player's and the background's components section later in this chapter.

StdGraphicsComponent

Let's start with the most used of all the component classes, the StdGraphicsComponent. Add the outline of the class as shown next.

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;

class StdGraphicsComponent implements GraphicsComponent {  

   private Bitmap mBitmap;
   private Bitmap mBitmapReversed;

    @Override
    public void initialize(Context context, 
                           ObjectSpec spec, 
                           PointF objectSize){
        
    }

    @Override
    public void draw(Canvas canvas, 
                            Paint paint, 
                            Transform t) {
        
    }
}

There are a few import statements that are currently unused, but they will all be used by the end of the chapter. The class implements GraphicsComponent and therefore must provide an implementation for the initialize and draw methods. These implementations are empty for now and we will return to them once we have coded the GameObject and Transform classes.

PlayerMovementComponent

Now code the PlayerMovementComponent that implements MovementComponent.

import android.graphics.PointF;

class PlayerMovementComponent implements MovementComponent {
    
   @Override
   public boolean move(long fps, Transform t,
                       Transform playerTransform){

        return true;
    }
}

Be sure to add the required move method that returns true.

Tip

All the component classes that we are coding will initially be left in a similar half done state until we revisit them later in the chapter.

PlayerSpawnComponent

Next code the near-empty PlayerSpawnComponent that implements SpawnComponent.

class PlayerSpawnComponent implements SpawnComponent {
    
   @Override
   public void spawn(Transform playerTransform, Transform t) {
        
    }
}

Add the empty spawn method to avoid any errors.

PlayerInputComponent and the PlayerLaserSpawner interface

Now we can code the outline to the PlayerInputComponent and the PlayerLaserSpawn interface. We will cover them both together because they have a connection to one another.

Starting with the PlayerInputComponent, we will also add a few member variables to this class and we will then discuss them. Create a new class PlayerInputComponent and code it as follows.

import android.graphics.Rect;
import android.view.MotionEvent;

import java.util.ArrayList;

class PlayerInputComponent implements InputComponent,
 InputObserver {

    private Transform mTransform;
    private PlayerLaserSpawner mPLS;

    PlayerInputComponent(GameEngine ger) {
        
    }

   @Override
    public void setTransform(Transform transform) {
        
    }

    // Required method of InputObserver 
   // interface called from the onTouchEvent method
   @Override
   public void handleInput(MotionEvent event, 
                           GameState gameState, 
                           ArrayList<Rect> buttons) {            
        
    }
}

Notice the class implements two interfaces. InputComponent and our old friend from the chapter 18, InputObserver. The code implements both the required methods for both interfaces, setTransform and handleInput (so GameEngine can call it with the player's screen touches).

There is also a Transform member that will show as an error until we code it shortly and another member too. There is a member called mPLS of type PlayerLaserSpawner.

Cast your mind back to Chapter 18 when we coded the GameStarter interface. We coded the GameStarter interface, so we could pass a reference to it into GameState. We then implemented the interface including the startNewGame method in GameEngine thus allowing GameState to call the startNewGame method in GameEngine.

We will do something very similar now to allow the PlayerInputComponent to call a method in GameEngine and spawn a laser.

The PlayerLaserSpawner interface will have one method, spawnPlayerLaser. By having an instance of PlayerLaserSpawner in PlayerInputComponent we will be able to call its method and have the GameEngine spawn lasers whenever we need it to.

Create a new class and code the PlayerLaserSpawner interface shown next.

public interface PlayerLaserSpawner {
        boolean spawnPlayerLaser(Transform transform);
}

Switch to the GameEngine class in Android Studio and make it implement PlayerLaserSpawner as highlighted next.

class GameEngine extends SurfaceView 
                      implements Runnable, 
                      GameStarter, 
                      GameEngineBroadcaster, 
                      PlayerLaserSpawner {

Now add the required method, spawnPlayerLaser to GameEngine, it will be empty- for now.

@Override
public boolean spawnPlayerLaser(Transform transform) {
   return false;
}

Now we can move on to the next component.

LaserMovementComponent

Code the LaserMovementComponent which implements MovementComponent.

import android.graphics.PointF;

class LaserMovementComponent implements MovementComponent {

    @Override
    public boolean move(long fps, 
                        Transform t, 
                        Transform playerTransform) {
        
        return true;
    }
}

The class implements the required move method.

LaserSpawnComponent

Code the LaserSpawnComponent which implements SpawnComponent.

import android.graphics.PointF;

class LaserSpawnComponent implements SpawnComponent {

    @Override
    public void spawn(Transform playerTransform, 
                      Transform t) {
        
    }
}

The code includes the empty but required spawn method.

BackgroundGraphicsComponent

Code the BackgroundGraphicsComponent that implements GraphicsComponent.

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;

class BackgroundGraphicsComponent implements GraphicsComponent {

    private Bitmap mBitmap;
    private Bitmap mBitmapReversed;

    @Override
    public void initialize(Context c,
                          ObjectSpec s,
                          PointF objectSize) {

    }

    @Override
    public void draw(Canvas canvas, 
                            Paint paint, 
                            Transform t) {

    }   
}

The two required methods of GraphicsComponent, initialize and draw have been implemented and there are also a couple of member variables ready for use when we code the class in full later this chapter.

Tip

The two variable names and the import of the Matrix class give a hint at how we will create the scrolling background effect.

BackgroundMovementComponent

Code the BackgroundMovementComponent which implements MovementComponent.

class BackgroundMovementComponent implements MovementComponent {
    @Override
    public boolean move(long fps, 
         Transform t, 
         Transform playerTransform) {       

        return true;
    }
}

The code includes the required move method.

BackgroundSpawnComponent

Code the BackgroundSpawnComponent that implements SpawnComponent.

class BackgroundSpawnComponent implements SpawnComponent {
    @Override
    public void spawn(Transform playerTransform, Transform t) {
        
    }
}

Notice it contains the required spawn method.

We now have an outline for all the component classes that we will complete by the end of the chapter. Next, we will code the Transform class.

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

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