|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
This is how you implement new game commands, or provide new implementation for existing commands.
The Command interface is, in a sense, the core of the programming model for the Wyvern engine, and takes a bit of explanation.
Game commands are plain text strings, such as "move west" or "get all from bag". The command verb is defined as the first word of the string.
An Command registers a command with an "agent" (often the agent is often a Player, but it can be a Monster or any other object that implements the Commandable interface). When the agent enters the command, it's sent through the agent's EventQueue and ultimately to the Command that registered it. It's also possible for the object to register the command with a map, or a "room" in a map, using the various command-registration methods provided by the wyvern.lib.GameMap interface.
There are four significant events in the life of a Command:
A simple example should help clarify things. Let's say you're making Cube Puzzle object that knows what to do when the user types "turn cube". The cube must implement the Command interface in order to be able to register its "turn" command.
Let's suppose the cube only wants "turn cube" to work if the agent (a player) is actually holding the cube in the agent's top-level inventory (i.e., not in a bag in the inventory):
First its knowsCommand() will be called, in which the cube can check the arguments to see if the player typed "turn cube" (return true) or if they intended to turn something else (return false).
If it returned true, its createEvent() method will be called, and if there is any other interesting context about the command that hook callbacks should be aware of, the cube can add this context into the event by setting properties in the event and documenting them thoroughly.
Next, the execute() method is called, passing in the event that the cube created in createEvent(), but after any hook callbacks have had a chance to modify properties of the event. The cube should expect that some properties may have been modified, and re-check them before executing the event.
Finally, the appropriate success/failure post-hook is invoked for the event, depending on the return value from execute().
Method Summary | |
CommandEvent |
createEvent(CommandEvent initialInfo)
Creates a CommandEvent that encapsulates all the state necessary to perform the command. |
boolean |
execute(CommandEvent event)
Executes the specified CommandEvent. |
boolean |
knowsCommand(java.lang.String command)
Returns true if this Command wants to handle the command. |
Method Detail |
public boolean knowsCommand(java.lang.String command)
This method exists to allow more than one game object to implement the same command verb, but with different expected arguments.
If two objects register for the same command and the same arguments, the object that was registered most recently gets to handle at the command.
command
- the entire command string, including arguments
public CommandEvent createEvent(CommandEvent initialInfo)
This method is called by the kernel. The kernel passes the event to the pre-hook, then calls Command.execute() (below) to execute the event, and finally calls the post-hook.
initialInfo
- a "blank" CommandEvent containing only the
command text and the agent who's performing the command. This
initial event is created by the originator of the event (e.g.
the AI or player's command preprocessor).
public boolean execute(CommandEvent event)
event
- the CommandEvent to execute
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |