class RealObject extends GameObject implements Cloneable, Spyable {
public void initialize (String initializer, GameObject parentObjectArg)
throws GameException
{
parentObject = (RealObject) parentObjectArg;
// Parse out the pieces of the initializer
name = Utility.stringPiece (initializer, "~", 1);
shortDescription = Utility.stringPiece (initializer, "~", 2);
longDescription = Utility.stringPiece (initializer, "~", 3);
// Tell our parent that we've been born
if (parentObject != null)
parentObject.register (this);
super.initialize (initializer, parentObjectArg);
initializeListeners (); // add ours after superclass
} // end initialize
/**
Add an object to the chain of things that process text messages we receive
@param l the object that listens
@param position where to add it on the list; either the constant HEAD or TAIL from the Listener interface
*/
protected void addListener(Listener l, int position)
{
if (position == Listener.HEAD)
listenerList.add(0, l);
else if (position == Listener.TAIL)
listenerList.add((listenerList.size()), l);
else
announce ("Bad add! Tried " + l + " at " + position);
}
/**
Initialize the listeners for this object. Not a bad idea to call the superclass to make sure that you have a default listener.
*/
protected void initializeListeners()
{
addListener(new DefaultListener("I don't understand"), Listener.TAIL);
}
/**
Process a random command (message). The message is usually entered by a user, but objects can send messages to each other if they want.
@param command an arbitrary text string to be processed by the object
*/
public void listen(String command)
{
for (int i = 0; i < listenerList.size(); i++)
{
if (((Listener)listenerList.get(i)).listen(command, this))
break;
}
}
final protected ArrayList listenerList = new ArrayList();
} /* end class RealObject */
class Toddler extends Wanderer {
public void initializeListeners ()
{
// "Go "
addListener(new FirstWordListener("go", new GoListener()), Listener.TAIL);
// "Go " -- he'll guess
addListener(new FirstWordListener("go", new Listener() {
public boolean listen (String command, RealObject actor)
{
actor.wanderVia ((Room.RoomPassage) actor.roomOf().passages.randomEntry(),
"Wandered randomly to @.");
return true;
}
}), Listener.TAIL);
// "Say "
addListener(new FirstWordListener("say", new Listener() {
public boolean listen (String command, RealObject actor)
{
actor.announce (command);
return true;
}
}), Listener.TAIL);
// Choo-choo-choo-choo-choo-choo-choo-choo-choo-choo-choo
addListener(new Listener() {
public boolean listen (String command, RealObject actor)
{
if (command.toLowerCase().indexOf ("train") > 0)
{
actor.announce ("Choo-choo!");
return true;
}
return false;
}
}, Listener.HEAD);
// Otherwise, No!
addListener(new DefaultListener("No!"), Listener.TAIL);
} // end initializeListeners (on Toddler, in case it's not obvious)
} // end class Toddler
class Dog extends Wanderer {
protected void initializeListeners()
{
// "Go "
addListener(new FirstWordListener("go", new GoListener()), Listener.TAIL);
// "Go "
addListener(new FirstWordListener("go", new DefaultListener ("Arf! Arf!")), Listener.TAIL);
// anything else
addListener(new DefaultListener ("Bow wow?"), Listener.TAIL);
// If the command starts with our name, ignore it& try again
addListener(new FirstWordListener(name, new Listener() {
public boolean listen (String command, RealObject actor)
{
actor.listen (command);
return true;
}
}), Listener.HEAD);
}
} // end class Dog
Last updated: 24 Jan 01