Coding the Observer pattern in Scrolling Shooter

Now we are well versed on how the Observer pattern works and we have had a good look at the interfaces we will need to write and how they will be used we can put all the theory into action in the Scrolling Shooter project.

As the specific use for our broadcaster and observers is to handle the player's input we will code a class to handle the screen touches for the HUD. As a reminder, the GameEngine class will be a Broadcaster and two separate classes that handle user input will be Observers. As the HUD and the player's spaceship are very different things it makes sense for each of them to handle their own input.

We will code the UIController class which will be our first Observer (for the HUD play/pause button) in this section and later in the project we will code our second Observer to handle the spaceship controls.

Tip

As we have learned, there is nothing stopping us adding more observers or even more broadcasters for different events if we need to.

Coding the Broadcaster interface

Create a new interface by selecting File | New Java Class. In the Name section type GameEngineBroadcaster. In the Type section drop-down selector choose Interface. Select Package private and then click the OK button to generate the empty interface.

Here is the entire code for the GameEngineBroadcaster interface with its single empty method called addObserver that takes an InputObserver as a parameter. The code to type is shown next highlighted amongst the code which was auto-generated.

interface GameEngineBroadcaster {

   void addObserver(InputObserver o);
}

Next, we will code the second interface of our Observer pattern the actual Observer called InputObserver.

Coding the InputObserver interface

Create a new interface by selecting File | New Java Class. In the Name section type InputObserver. In the Type section drop-down selector choose Interface. Select Package private and then click the OK button to generate the empty interface.

Here is the entire code for the InputObserver interface with its single empty method called handleInput that takes a MotionEvent, GameState and an ArrayList containing the position of the controls as parameters. The code to type is shown next highlighted amongst the code which was auto-generated.

import android.graphics.Rect;
import android.view.MotionEvent;
import java.util.ArrayList;

interface InputObserver {

    void handleInput(MotionEvent event, GameState gs,
         ArrayList<Rect> controls);
}

Next, we will implement/use the new GameEngineBroadcaster interface.

Making GameEngine a Broadcaster

Add the GameEngineBroadcaster to the list of interfaces that the GameEngine class implements.

class GameEngine extends SurfaceView implements Runnable, 
   GameStarter, GameEngineBroadcaster {

On screen you will observe the line with the new code will be underlined in red until we implement the required method of the interface. So, let's do that now. We also need to have a way of storing all our InputObservers. An ArrayList will do the job.

Declare and initialize a new empty ArrayList that holds objects of type InputObserver as highlighted next as a member of the GameEngine class.

private Thread mThread = null;
private long mFPS;

private ArrayList<InputObserver>
   inputObservers = new ArrayList();

private GameState mGameState;
private SoundEngine mSoundEngine;
HUD mHUD;
Renderer mRenderer;

Now implement the addObserver method as required by any class implementing the GameEngineBroadcaster interface. I put mine right after the constructor in GameEngine. Here is the method to add.

// For the game engine broadcaster interface
public void addObserver(InputObserver o) {
   
   inputObservers.add(o);
}

Finally, before we make our first InputObserver which can register for broadcasts, we will add the code that will call all the InputObservers' handleInput methods. Add this highlighted code in the onTouchEvent method.

@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
   // Handle the player's input here
   // But in a new way
   for (InputObserver o : inputObservers) {
         o.handleInput(motionEvent, mGameState, 
                mHUD.getControls());
   }   

   return true;
}

The code loops through all the InputObservers in the ArrayList and calls their handleInput method (which they are guaranteed to have implemented). If there is zero, just one or 1000 Observers in the ArrayList the code will work just the same.

Next, we will implement/use the new InputObserver interface at the same time as handling the screen touches for the user interface.

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

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