Managing Game Objects

To keep track of all the objects in the world, you'll create the GameObjectManager interface, shown in Listing 9.10. We use a rather simple implementation of this interface in this chapter, but in later chapters you'll create a new implementation. The GameObjectManager interface provides methods to keep track of every object in the world. Also, it has two methods, markVisible() and markAllVisible(), to mark which objects are visible to be drawn. The markVisible() method takes a Rectangle as a parameter, which specifies a 2D area (bird's-eye view) that is visible. Only objects marked as visible should be drawn; after they are drawn, all the objects are marked as invisible again.

Listing 9.10 GameObjectManager.java
package com.brackeen.javagamebook.game;
import java.awt.Rectangle;
import java.awt.Graphics2D;
import com.brackeen.javagamebook.game.GameObjectRenderer;
/**
 The GameObjectManager interface provides methods to keep
 track of and draw GameObjects.
*/
public interface GameObjectManager {
 /**
 Marks all objects within the specified 2D bounds
 as potentially visible (should be drawn).
 */
 public void markVisible(Rectangle bounds);
 /**
 Marks all objects as potentially visible (should be drawn).
 */
 public void markAllVisible();
 /**
 Adds a GameObject to this manager.
 */
 public void add(GameObject object);
 /**
 Adds a GameObject to this manager, specifying it as the
 player object. An existing player object, if any,
 is not removed.
 */
 public void addPlayer(GameObject player);
 /**
 Gets the object specified as the Player object, or null
 if no player object was specified.
 */
 public GameObject getPlayer();
 /**
 Removes a GameObject from this manager.
 */
 public void remove(GameObject object);
 /**
 Updates all objects based on the amount of time passed
 from the last update.
 */
 public void update(long elapsedTime);
 /**
 Draws all visible objects.
 */
 public void draw(Graphics2D g, GameObjectRenderer r);
}


The draw() method in GameObjectManager draws every visible object managed by the game object manager and marks every object as invisible. It uses a GameObjectRenderer to draw the objects, which is just an interface to draw an object, as shown in Listing 9.11. The ZBufferedRenderer class implements this interface.

Listing 9.11 GameObjectRenderer.java
package com.brackeen.javagamebook.game;
import java.awt.Graphics2D;
import com.brackeen.javagamebook.game.GameObject;
/**
 The GameObjectRenderer interface provides a method for
 drawing a GameObject.
*/
public interface GameObjectRenderer {
 /**
 Draws the object and returns true if any part of the
 object is visible.
 */
 public boolean draw(Graphics2D g, GameObject object);
}


The SimpleGameObjectManager implements the GameObjectManager interface by just keeping all the objects in a list. When an object is marked as visible, it's put in a "visible" list, which is cleared after all the objects in the visible list are drawn. You'll create a more advanced GameObjectManager later in , but this will work fine for now.

Screenshot


   
Comments