wyvern.lib
Class Direction

java.lang.Object
  extended bywyvern.lib.Direction

public class Direction
extends java.lang.Object

Encapsulates the cardinal game directions (n, s, e, w, ne, nw ,se, sw). Contains various utilities for converting directions to strings, Points and so on.

Version:
1.0, Aug 28, 1997
Author:
Steve Yegge

Nested Class Summary
static interface Direction.Dirs
          You can implement this interface to get syntactic access to the Direction constants (NORTH, SOUTH, etc.)
 
Field Summary
static int DOWN
           
static int E
           
static int EAST
           
static int ILLEGAL_DIR
           
static int N
           
static int NE
           
static int NONE
           
static int NORTH
           
static int NW
           
static Point POINT_EAST
           
static Point POINT_NE
           
static Point POINT_NORTH
           
static Point POINT_NW
           
static Point POINT_SE
           
static Point POINT_SOUTH
           
static Point POINT_SW
           
static Point POINT_WEST
           
static int S
           
static int SE
           
static int SOUTH
           
static int SW
           
static int UP
           
static int W
           
static int WEST
           
 
Constructor Summary
Direction()
           
 
Method Summary
static boolean cardinalDirection(int dir)
          Returns true if the passed direction is one of the eight cardinal directions (n,s,e,w,ne,nw,sw,se).
static int computeClosestDirection(Point src, Point dest)
          Figures out the nearest cardinal direction to the specified direction.
static int computeDirection(Point p1, Point p2)
          Computes the relative direction of one point to another.
protected static void createDirectionTables()
          Establishes all the direction commands we recognize, for checking the arguments to "move" or "go".
static java.lang.String dirToLongString(int dir)
          Converts a direction constant to a user-readable string.
static java.lang.String dirToString(int dir)
          Converts a direction constant to a string that can be used as a movement command.
static int getDir(Point p)
          Turns a movement Point (with offsets -1,0,1) into a direction
static int getDirection(int xoffset, int yoffset)
          Returns the direction for the specified offsets.
static int getDirection(Point offsets)
          Returns the direction for the specified offsets.
static java.lang.String getDirString(int xoffset, int yoffset)
          Returns the direction for the specified offsets.
static java.lang.String getDirString(Point offsets)
          Returns the direction for the specified offsets.
static Point getNextMove(Point p, int dir)
          Returns the next Point if you go in the passed direction
static java.lang.String getSuffix(int direction)
          Return the suffix associated with the image-file naming convention for the specified direction.
static Point getXYOffsets(int dir)
          Turns a movement direction into x & y offsets.
static void main(java.lang.String[] args)
          Test.
static int oppositeOf(int direction)
          Return opposite direction (e.g., N -> S)
static int parseDirection(java.lang.String dir)
          Figures out what direction the user specified from the string.
static int randomDirection()
          Returns a random cardinal direction.
static int rotateLeft(int direction)
          Return point counter-clockwise to this.
static int rotateRight(int direction)
          Return direction clockwise to this one.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ILLEGAL_DIR

public static final int ILLEGAL_DIR
See Also:
Constant Field Values

NONE

public static final int NONE
See Also:
Constant Field Values

N

public static final int N
See Also:
Constant Field Values

NORTH

public static final int NORTH
See Also:
Constant Field Values

S

public static final int S
See Also:
Constant Field Values

SOUTH

public static final int SOUTH
See Also:
Constant Field Values

E

public static final int E
See Also:
Constant Field Values

EAST

public static final int EAST
See Also:
Constant Field Values

W

public static final int W
See Also:
Constant Field Values

WEST

public static final int WEST
See Also:
Constant Field Values

NE

public static final int NE
See Also:
Constant Field Values

NW

public static final int NW
See Also:
Constant Field Values

SE

public static final int SE
See Also:
Constant Field Values

SW

public static final int SW
See Also:
Constant Field Values

UP

public static final int UP
See Also:
Constant Field Values

DOWN

public static final int DOWN
See Also:
Constant Field Values

POINT_NORTH

public static final Point POINT_NORTH

POINT_SOUTH

public static final Point POINT_SOUTH

POINT_EAST

public static final Point POINT_EAST

POINT_WEST

public static final Point POINT_WEST

POINT_NE

public static final Point POINT_NE

POINT_NW

public static final Point POINT_NW

POINT_SE

public static final Point POINT_SE

POINT_SW

public static final Point POINT_SW
Constructor Detail

Direction

public Direction()
Method Detail

getSuffix

public static java.lang.String getSuffix(int direction)
Return the suffix associated with the image-file naming convention for the specified direction.

Returns:
the suffix we use in the image name, E.g. Direction.EAST returns "E".

dirToString

public static java.lang.String dirToString(int dir)
Converts a direction constant to a string that can be used as a movement command.

Parameters:
dir - the direction to convert

dirToLongString

public static java.lang.String dirToLongString(int dir)
Converts a direction constant to a user-readable string. Uses the long form, e.g. "west", "southwest".

Parameters:
dir - the direction to convert
Returns:
the long string version of the direction

oppositeOf

public static int oppositeOf(int direction)
Return opposite direction (e.g., N -> S)

Parameters:
direction - direction to reverse

rotateLeft

public static int rotateLeft(int direction)
Return point counter-clockwise to this.

Parameters:
direction - direction to rotate

rotateRight

public static int rotateRight(int direction)
Return direction clockwise to this one.

Parameters:
direction - direction to rotate

getDir

public static int getDir(Point p)
Turns a movement Point (with offsets -1,0,1) into a direction

Parameters:
p - contains offsets
Returns:
integer containing direction

getXYOffsets

public static Point getXYOffsets(int dir)
Turns a movement direction into x & y offsets. Don't modify the point that's returned!

Parameters:
dir - the direction to move
Returns:
a Point containing the offsets from the current square. Don't modify it, or the whole game will stop working!

getNextMove

public static Point getNextMove(Point p,
                                int dir)
Returns the next Point if you go in the passed direction

Parameters:
p - square to move from
dir - the direction to move
Returns:
a Point containing the next square

parseDirection

public static int parseDirection(java.lang.String dir)
Figures out what direction the user specified from the string.

Parameters:
dir - a string (e.g. "northwest", "left", "s", "down" )
Returns:
a Direction constant (Direction.NW, Direction.W, ...) Returns Direction.ILLEGAL_DIR if the string couldn't be parsed

createDirectionTables

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


getDirString

public static java.lang.String getDirString(int xoffset,
                                            int yoffset)
Returns the direction for the specified offsets. The only valid values for xoffset and yoffset are 1, 0 and -1, and they cannot both be zero.

Parameters:
xoffset - 1, 0 or -1
yoffset - 1, 0 or -1
Returns:
a direction ("n", "s", "e", "w", "ne", etc.)
Throws:
java.lang.IllegalArgumentException - if the offsets are invalid

getDirString

public static java.lang.String getDirString(Point offsets)
Returns the direction for the specified offsets. The only valid values for xoffset and yoffset are 1, 0 and -1, and they cannot both be zero.

Parameters:
offsets - a Point whose x & y specify one of the 8 points around (0,0).
Returns:
a direction ("n", "s", "e", "w", "ne", etc.)
Throws:
java.lang.IllegalArgumentException - if the offsets are invalid

getDirection

public static int getDirection(int xoffset,
                               int yoffset)
Returns the direction for the specified offsets. The only valid values for xoffset and yoffset are 1, 0 and -1, and they cannot both be zero.

Parameters:
xoffset - 1, 0 or -1
yoffset - 1, 0 or -1
Returns:
a Direction constant (Direction.NORTH, etc.)
Throws:
java.lang.IllegalArgumentException - if the offsets are invalid

getDirection

public static int getDirection(Point offsets)
Returns the direction for the specified offsets. The only valid values for xoffset and yoffset are 1, 0 and -1, and they cannot both be zero.

Parameters:
offsets - a Point whose x & y specify one of the 8 points around (0,0).
Returns:
a Direction constant (Direction.NORTH, etc.)
Throws:
java.lang.IllegalArgumentException - if the offsets are invalid

cardinalDirection

public static boolean cardinalDirection(int dir)
Returns true if the passed direction is one of the eight cardinal directions (n,s,e,w,ne,nw,sw,se).

Parameters:
dir - the direction to test
Returns:
true if it's a cardinal direction

computeDirection

public static int computeDirection(Point p1,
                                   Point p2)
Computes the relative direction of one point to another. Works best for points that are reasonably close together.

Parameters:
p1 - the start point
p2 - the end point
Returns:
the direction that p2 lies from p1

randomDirection

public static int randomDirection()
Returns a random cardinal direction.


computeClosestDirection

public static int computeClosestDirection(Point src,
                                          Point dest)
Figures out the nearest cardinal direction to the specified direction. Useful for choosing which image to use for objects (e.g. projectiles or bolt spells) traveling in non-cardinal directions.

Parameters:
src - start point
dest - end point
Returns:
a cardinal direction (NORTH, S, E, W, NE, NW, SW, SE).

main

public static void main(java.lang.String[] args)
Test.