ACTOR Management

ActorManager is a class responsible for all of the activities of the Actor classes in the game. This manager must make sure that all of the key functions of the objects can be accessed and presented to the game logic. The class is also responsible for ensuring that entities are properly updated, logged, and drawn to screen in games that don’t have a specific display list or separate control mechanism for rendering. This class uses a LinkedList and implements a series of methods designed to handle the various components in each segment. Let’s look at the ActorManager class:

import java.util.*; public class ActorManager {   LinkedList actorList;   ActorManager()  {   //make a linked list to hold all the actors.    actorList = new LinkedList();   }   public void createEntity(String s, int number)   {     for (int i = 0; i<number; i++)      {     actorList.add(entityFactory(s));   }   }   Object entityFactory(String name)    {     Class temp;    Object targetClass =null;    try     {      //Find the class in question      temp = Class.forName(name);       // Make an instance of the desired class      targetClass = temp.newInstance();        }       catch (Exception e)        {        System.out.println("Cannot create insance of" +name);       }      System.out.println("Created the " +name+ "!");      return targetClass;    } public void clearEntities() {    for (int i = 0; i<actorList.size(); i++)     {       actorList.remove(i);   } System.out.println("The entity list has been cleared"); } public void updateAll() {    Updater u;  for (int i = 0; i<actorList.size(); i++)   {     u = (Updater)actorList.get(i);     u.update();  } }  public void drawAll()  {     Drawable d;   boolean  b;   for (int i = 0; i<actorList.size(); i++)    {        d = (Drawable)actorList.get(i);        if(d.isVisible() == true)        d.draw();   }   } }//end ActorManager.java

This class is designed around the precepts of a Factory pattern. In the method createEntity(), the string and number of specified types of object are passed through. The call to the entityFactory() method is made and resolved based on string comparison with available classes that will result in the creation of the listed number of entities based on type. These new objects are then immediately added to an internal LinkedList that will hold them for processing.

One advantage to this method is that redefinition isn’t needed to use new classes. A check can be made to see if the class is available and then the creation process occurs. This is an example of one of the most powerful aspects of the Java language. New classes need only to be added into the project to be included in this step. The largest disadvantage to using the Class.newInstance() method is that it calls only the default constructor on the given objects. Objects that need nondefault constructor calls must be created separately or redefined to map within this methodology.

The converse method to createEntity() is clearEntities(), in which the entire list of elements in the game is removed from the list. This method is required when making transitions from level to level or when the eventual completion of the game is over. Java handles the garbage collection on its own, but this is a good methodology to follow, nevertheless.

Looking at the functional calls to both updateAll() and drawAll(), we can see how we are leveraging the earlier definitions of Actor and its interfaces. Regardless of the individual implementation in subclasses of Actor, the LinkedList still processes those methods as they are defined in one sweeping task. One particular note on the drawAll() method is that it makes a minor optimization on the draw routines when it checks to see if the class is actually visible before processing the actual draw() method. This optimization prevents unwanted calls to draw objects that are outside the boundaries of the particular screen or situation in the game.

A sample program that uses the ActorManager follows:

public static void main(String args[]) {      ActorManager am = new ActorManager();     am.createEntity("Player",1);     am.createEntity("Enemy",10);     am.updateAll();     am.drawAll(); }

As you can see, this code cleans up the implementation of the process a bit. If this class were to be implemented in the earlier mentioned game class, the ActorManager would have been constructed with the preparation of the Actor classes and the list in the GAME_INIT. The calls to update and draw would be made in the GAME_MAIN method.

Looking forward a bit, it’s not too hard to envision scripted files that can parse out the string names of entities and the number needed for a particular level, and then have them automatically set up by the game itself. Remember the engine metaphor at the beginning of this chapter. The key is to create structures that process this work in a way that allows for the designers to focus on making great gameplay. These are just the first few steps of that process. The ActorManager class is an attempt to control and limit access to the entities in the game. Other more extensive implementations exist, and experimentation with this methodology is encouraged.



Practical Java Game Programming
Practical Java Game Programming (Charles River Media Game Development)
ISBN: 1584503262
EAN: 2147483647
Year: 2003
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net