Class GameObject

java.lang.Object
  |
  +--GameObject
All Implemented Interfaces:
java.lang.Comparable
Direct Known Subclasses:
Passage, RealObject

public class GameObject
extends java.lang.Object
implements java.lang.Comparable

This is the main class of objects in the game. Its instances represent tangible, rather than software, objects.


Inner Class Summary
(package private)  class GameObject.LeaveInfo
          This inner class has no methods, but is used as a convenient way to keep all the information about someone leaving a Room in one convenient place.
 
Field Summary
protected static MasterGame masterGame
          The MasterGame object that's running the game of which we're part.
protected  java.lang.String name
          Object names are supposed to be unique, but this is not currently enforced.
 
Constructor Summary
GameObject()
           
 
Method Summary
 void announce(java.lang.String message)
          If an object wants to say something, let the game handle it.
 int compareTo(java.lang.Object other)
          Compare this object to another, for sorting purposes.
 void doAction()
          Figure out what we want to do when we get our 15 microseconds of fame.
static MasterGame getMasterGame()
          Retrieve a class variable so we can find the master game object.
 void initialize(java.lang.String initializer, GameObject parentObjectArg)
          Initalize the object based on details in the input file, including registering it with the game and its parent.
 void kill()
          Remove this object from the game by notifying the master game.
 void listen(java.lang.String message)
          Listen to a message and respond.
static GameObject newInstance(java.lang.String objectType, java.lang.String initializer, GameObject owner)
          Create a new GameObject based on text information.
static void setMasterGame(MasterGame argMasterGame)
          Set a class variable so we can find the master game object.
 java.lang.String toString()
          Convert the object to a string.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

masterGame

protected static MasterGame masterGame
The MasterGame object that's running the game of which we're part.

name

protected java.lang.String name
Object names are supposed to be unique, but this is not currently enforced. Names come either from the input file or can be generated (as for cloned objects and Passages) by MasterGame.getNextName.
See Also:
MasterGame.getNextName(String)
Constructor Detail

GameObject

public GameObject()
Method Detail

announce

public void announce(java.lang.String message)
If an object wants to say something, let the game handle it.
Parameters:
message - Sentence the object wants to say. These are generally accumulated and displayed at checkpoints.

compareTo

public int compareTo(java.lang.Object other)
Compare this object to another, for sorting purposes. Boringly compares the lower-case versions of the two objects' names.
Specified by:
compareTo in interface java.lang.Comparable
Parameters:
other - typed as an Object to meet the requirements of the Comparable interface, but this method will Throw a ClassCastException it isn't really a GameObject.
Returns:
The usual -1, 0, +1, according to the relationship between the objects.

doAction

public void doAction()
Figure out what we want to do when we get our 15 microseconds of fame. Nothing, by default, but most interesting subclasses will come up with something creative.

getMasterGame

public static MasterGame getMasterGame()
Retrieve a class variable so we can find the master game object. Generally, other classes should keep their own copies of this, but the Predicate hierarchy really wants to be local classes within GameObject, only Java doesn't provide a mechanism for dynamic creation of local objects, so we end up with this kludge.
Returns:
pointer to the master game

initialize

public void initialize(java.lang.String initializer,
                       GameObject parentObjectArg)
                throws GameException
Initalize the object based on details in the input file, including registering it with the game and its parent. Because we're using indirection to create objects in this class, we can't have a constructor with arguments.
Parameters:
initializer - Not used here, only by subclasses. It's a String now, but I think that the calling method should split it up into an array. It would make the code much easier here.
parentObjectArg - What this is contained inside. Generally null for Rooms and Passages.
Throws:
GameException - The superclass never does, but declared this way to allow for subclasses to throw it.

kill

public void kill()
Remove this object from the game by notifying the master game.

listen

public void listen(java.lang.String message)
Listen to a message and respond. Doesn't return anything, but may generate messages--directly or indirectly--via announce. The default implementation just says that it doesn't understand, but subclasses may do something. And I hope some of them do, or this has been a lot of wasted effort.
Parameters:
message - English message telling the object what to do. The messages was probably typed in by a user, but could also have been a request by some other object during its doAction time.

newInstance

public static GameObject newInstance(java.lang.String objectType,
                                     java.lang.String initializer,
                                     GameObject owner)
                              throws CantCreateObjectException
Create a new GameObject based on text information.
Parameters:
objectType - the object type, currently the same as the class name)
initializer - tilde-delimited initialization string
owner - the owner of this object
Returns:
the new object
Throws:
CantCreateObjectException - if the objectType is not a valid class name, if the class is not a descendant of GameObject, or if the initialize() method throws an exception.

setMasterGame

public static void setMasterGame(MasterGame argMasterGame)
Set a class variable so we can find the master game object. Called only from the master game constructor.

toString

public java.lang.String toString()
Convert the object to a string.
Overrides:
toString in class java.lang.Object
Returns:
The name of the object. Some people would prefer to have it return class + name for debugging purposes, but I like the convenience of being able, in effect, to use a GameObject as a string without peppering the code with getName() calls.