wyvern.kernel.motion
Class MoveCommand

java.lang.Object
  extended bywyvern.kernel.motion.MoveCommand
All Implemented Interfaces:
BuiltInCommand, Command

public class MoveCommand
extends java.lang.Object
implements BuiltInCommand

This class implements movement. It also serves as the main switchboard for converting moves into pushes, attacks and other types of events, depending on context.

Version:
1.0, Nov 04, 1997
Author:
Steve Yegge

Field Summary
protected  java.util.HashMap dirs_
           
 
Method Summary
 void addCommands(java.util.Map commands, CommandList list)
          Tells the Command to add the commands that it wants to implement into the passed data structure.
protected  void checkLegalMove(MoveEvent event, GameObject agent)
          Checks every proposed destination location for the move to see if the agent can actually move there.
static java.util.List computeDestination(GameObject agent, int dir, Point offsets)
          Given an agent to move and a direction to move it, figures out what the resulting locations would be upon completion of the move.
protected  void createDirectionTable()
          Establishes all the direction commands we recognize, for checking the arguments to "move" or "go".
 CommandEvent createEvent(CommandEvent e)
          Creates a MoveEvent from the command "move" or "go".
 boolean doMove(GameObject agent, Point offsets, int dir)
          Moves an object in the specified direction.
 void ensureFacingNewDirection(MoveEvent e)
          Turns the agent in the direction of the move, even if it was vetoed, to show their intent.
 boolean execute(CommandEvent e)
          Executes the passed MoveEvent by moving the agent in the map.
static MoveCommand getImpl()
           
 boolean knowsCommand(java.lang.String command)
          Returns true if this Command wants to handle the command.
 MoveEvent makeMoveEvent(java.lang.String command, Commandable agent, int dir, java.lang.String fail)
          Creates a standard MoveEvent and stuffs the event parameters.
protected  int parseMoveArgs(CommandEvent e)
          Gets the direction to move from the command and arguments.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dirs_

protected java.util.HashMap dirs_
Method Detail

getImpl

public static MoveCommand getImpl()

createDirectionTable

protected void createDirectionTable()
Establishes all the direction commands we recognize, for checking the arguments to "move" or "go".


addCommands

public void addCommands(java.util.Map commands,
                        CommandList list)
Description copied from interface: BuiltInCommand
Tells the Command to add the commands that it wants to implement into the passed data structure.

Specified by:
addCommands in interface BuiltInCommand
Parameters:
commands - a Map whose keys are commands, and whose values are Commands that handle those commands.
list - the CommandList the Command is being added to.

knowsCommand

public boolean knowsCommand(java.lang.String command)
Description copied from interface: Command
Returns true if this Command wants to handle the command. The Command can examine the arguments and determine that it doesn't actually understand the command, in which case it should return false.

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.

Specified by:
knowsCommand in interface Command
Parameters:
command - the entire command string, including arguments
Returns:
true if we want to handle the command

createEvent

public CommandEvent createEvent(CommandEvent e)
Creates a MoveEvent from the command "move" or "go". Depending on the conditions, we may generate a MoveEvent, a a PushEvent, or an AttackEvent.

Specified by:
createEvent in interface Command
Parameters:
e - an event with the original command text and agent
Returns:
a CommandEvent subclass encapsulating this command's execution parameters and state. It should copy in the fields from the passed-in event.

parseMoveArgs

protected int parseMoveArgs(CommandEvent e)
Gets the direction to move from the command and arguments.

Returns:
the direction to move, -1 if it's not a valid direction

makeMoveEvent

public MoveEvent makeMoveEvent(java.lang.String command,
                               Commandable agent,
                               int dir,
                               java.lang.String fail)
Creates a standard MoveEvent and stuffs the event parameters. Doesn't perform any of the checks to see if the move is legal (or if the move is actually supposed to be an attack or push).

Parameters:
command - the original command text (e.g. "move e" or "n")
agent - the agent that's moving
dir - the Direction to move
fail - any failure message to use
Returns:
a MoveEvent for the move

computeDestination

public static java.util.List computeDestination(GameObject agent,
                                                int dir,
                                                Point offsets)
Given an agent to move and a direction to move it, figures out what the resulting locations would be upon completion of the move. Normally this just means translating all the points in the direction of the move. However, some objects specify "deformers" that change the shape of the object when they move.

Parameters:
agent - the object to move; doesn't have to be Commandable
dir - the direction to move
offsets - the result of calling Direction.getXYOffsets(dir).
Returns:
the destination locations, in List form.

checkLegalMove

protected void checkLegalMove(MoveEvent event,
                              GameObject agent)
Checks every proposed destination location for the move to see if the agent can actually move there. Invokes the agent's Mover object to perform the logic. If the Mover finds that the agent can't move there, the event will be vetoed.

This method has side effects. It can set parameters in the event: the failure message, the movement speed and the veto flag. It's broken into its own method precisely to make the public methods of MoveCommand free of side-effects, so they can be shared by the PushCommand and AttackCommand code.

Parameters:
event - the event
agent - the agent

execute

public boolean execute(CommandEvent e)
Executes the passed MoveEvent by moving the agent in the map. All precondition-checks must have been executed before calling this method. This method performs the move, even if it would result in a bad situation (such as sticking the agent in a wall).

Specified by:
execute in interface Command
Parameters:
e - the CommandEvent to execute
Returns:
true if the event completed successfully, else false.

ensureFacingNewDirection

public void ensureFacingNewDirection(MoveEvent e)
Turns the agent in the direction of the move, even if it was vetoed, to show their intent. Doesn't turn them in place if they have a deformer, or it'll screw them up. If you change the way this works, or comment it out, it'll affect the usability of dungeons. If you want to keep players from turning in place, make sure you check the reason for the veto - if they just hit a solid barrier, then turning should be OK.


doMove

public boolean doMove(GameObject agent,
                      Point offsets,
                      int dir)
Moves an object in the specified direction. Doesn't deform it; just performs a translation.

Parameters:
agent - the object to move
offsets - the x & y offsets to move it
dir - the Direction constant matching the (x,y) offsets
Returns:
true if the move was successful