netwar.game
Class Hex

java.lang.Object
  |
  +--netwar.game.Hex

public class Hex
extends java.lang.Object

This class represents a single hexagon-shaped region of game-space. These regions are used to simplify path-finding algorithms, graphical draw-order, and terrain logic and display. A Hex may contain up to one GameObject at a time.

Author:
Group N2 - Project Netwar, Daniel Grund, with adjustments by Kyle Kakligian

Field Summary
static double sqrt3
          The square root of three, which is particularly relavent to hexagon geometry.
 
Constructor Summary
protected Hex(HexType h)
          This constructor initializes the Hex according to a HexType's initializing data.
 
Method Summary
static void draw(GameViewer v)
          Draws all hexes onto GameViewer v.
static void drawGameObject(GameViewer v)
          Draws all the GameObjects in the game onto GameViewer v.
 boolean enter(GameObject u)
          Attempts to put the GameObject into this Hex.
static Hex getHex(int x, int y)
          Retrieves a Hex from the board.
static Point2D getHexDimension()
          Returns the size of a rectangle just big enough to contain one Hex.
static Point3D getMapPoint(int x, int y)
          Gets the ground-center of the Hex at hex coordinate (x,y).
 java.awt.Color getMinimapColor()
          Returns the Color to display on the minimap.
 GameObject getOccupant()
          Returns the GameObject which is in the Hex.
static Hex getXY(Point3D vr)
          Gets the Hex containing a game-space point, and alters the parameter to contain the hex coordinates, by storing the hex x in vr.x and the hex y in vr.y
 boolean isEmpty()
          Checks if the Hex is unoccupied.
 void leave(GameObject u)
          Removes the GameObject from the Hex, if it is the GameObject which was in the Hex.
static void makeBoard(int rad)
          Initializes the board, which is a static array of Hex objects, designed to produce a hexagon of hexagons.
static int radialX(int radial, int offset, boolean fromEdge)
          Used for determining initial base locations.
static int radialY(int radial, int offset, boolean fromEdge)
          Used for determining initial base locations.
 boolean reserve()
          Attempts to reserve this Hex for the caller.
 void unreserve()
          Unreserves this Hex.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sqrt3

public static final double sqrt3
The square root of three, which is particularly relavent to hexagon geometry.
Constructor Detail

Hex

protected Hex(HexType h)
This constructor initializes the Hex according to a HexType's initializing data.
Parameters:
h - The HexType containing the data for this type of Hex.
Method Detail

radialX

public static int radialX(int radial,
                          int offset,
                          boolean fromEdge)
Used for determining initial base locations.
Parameters:
radial - a number from 0 to 11 indicating the direction from the center.
offset - the distance along that radian.
fromEdge - if true, count the distance (offset) from the edge of the map, instead of the center.
Returns:
the X coordinate of the desired location (in hex coordinates).

radialY

public static int radialY(int radial,
                          int offset,
                          boolean fromEdge)
Used for determining initial base locations.
Parameters:
radial - a number from 0 to 11 indicating the direction from the center.
offset - the distance along that radian.
fromEdge - if true, count the distance (offset) from the edge of the map, instead of the center.
Returns:
the Y coordinate of the desired location (in hex coordinates).

makeBoard

public static void makeBoard(int rad)
Initializes the board, which is a static array of Hex objects, designed to produce a hexagon of hexagons. Also initializes the HexType objects, and assigns them to the appropriate Hexes.
Parameters:
rad - The radius of the board in Hexes.

getHex

public static Hex getHex(int x,
                         int y)
Retrieves a Hex from the board.
Parameters:
x - The x part of the Hex coordinate of the desired hex.
y - The y part of the Hex coordinate of the desired hex.
Returns:
The desired Hex, or null if the coordinates are invalid.

draw

public static void draw(GameViewer v)
Draws all hexes onto GameViewer v.
Parameters:
v - A GameViewer seeking to display the entire map (or at least the visible portion).

drawGameObject

public static void drawGameObject(GameViewer v)
Draws all the GameObjects in the game onto GameViewer v. These are drawn in order from south to north to enable the Z-order to be preserved.
Parameters:
v - The GameViewer seeking to display all of the GameObjects.

getMapPoint

public static Point3D getMapPoint(int x,
                                  int y)
Gets the ground-center of the Hex at hex coordinate (x,y).
Parameters:
x - The x part of the hex coordinate.
y - The y part of the hex coordinate.
Returns:
The ground-center of the specified hex.

getXY

public static Hex getXY(Point3D vr)
Gets the Hex containing a game-space point, and alters the parameter to contain the hex coordinates, by storing the hex x in vr.x and the hex y in vr.y
Parameters:
vr - The game-space point to locate, and the storage space for the x and y.
Returns:
The Hex containing the game-space point.

reserve

public boolean reserve()
Attempts to reserve this Hex for the caller.
Returns:
true if the Hex was reserved for the caller.

unreserve

public void unreserve()
Unreserves this Hex. This should only be called by something which successfully called reserve().

enter

public boolean enter(GameObject u)
Attempts to put the GameObject into this Hex.
Parameters:
u - The GameObject which is attempting to occupy the Hex.
Returns:
True if the GameObject was put into this Hex.

leave

public void leave(GameObject u)
Removes the GameObject from the Hex, if it is the GameObject which was in the Hex.
Parameters:
u - The GameObject which is leaving the Hex.

isEmpty

public boolean isEmpty()
Checks if the Hex is unoccupied.
Returns:
true if there is no GameObject in the Hex.

getOccupant

public GameObject getOccupant()
Returns the GameObject which is in the Hex.
Returns:
the occupant, or null if the Hex is empty.

getMinimapColor

public java.awt.Color getMinimapColor()
Returns the Color to display on the minimap.
Returns:
A Color for use on the Minimap.

getHexDimension

public static Point2D getHexDimension()
Returns the size of a rectangle just big enough to contain one Hex. This size is stored into a Point2D as x = width, y = height.
Returns:
the Point2D containing the size of the rectangle.