mhframework.tilemap
Class MHTileMap

java.lang.Object
  extended by mhframework.tilemap.MHTileMap
All Implemented Interfaces:
MHRenderable
Direct Known Subclasses:
MHIsometricMap

public class MHTileMap
extends java.lang.Object
implements MHRenderable

Base class for deriving tile-based map objects. It may be used by itself for creating rectangular tile maps and extended to make isometric and hexagonal maps. This class and its subclasses handle the presentation of and user interaction with the data contained in an MHMap object.


Field Summary
protected  java.awt.geom.Rectangle2D anchorSpace
          A rectangle defining the difference between the screen space and the world space which is used for clipping and stuff.
protected  MHMapCellAddress cursorAddress
           
protected  java.awt.Point cursorAnchor
           
protected  java.awt.Point cursorPoint
           
static int DIR_EAST
          East direction.
static int DIR_NORTH
          North direction.
static int DIR_NORTHEAST
          Northeast direction.
static int DIR_NORTHWEST
          Northwest direction.
static int DIR_SOUTH
          South direction.
static int DIR_SOUTHEAST
          Southeast direction.
static int DIR_SOUTHWEST
          Southwest direction.
static int DIR_WEST
          West direction.
static int MAX_DIRECTIONS
          Maximum number of directions.
protected  java.awt.Point screenAnchor
          The upper-left corner of the visible portion of the map.
protected  java.awt.geom.Rectangle2D screenSpace
          A rectangle containing the screen coordinates.
protected  java.awt.geom.Rectangle2D worldSpace
          A rectangle containing all of the coordinates of the entire virtual world.
 
Constructor Summary
MHTileMap(java.lang.String filename, MHObjectFactory vendor)
          Constructor.
 
Method Summary
 void advance()
          Tells an object to update its data.
protected  void calculateAnchorSpace()
          Calculate the anchor space for this tile map.
protected  void calculateWorldSpace()
          Calculate the world space for this tile map.
protected  void clipCursorAddress()
          Performs validation on the cursor's map cell address to ensure that it is within the bounds of the map.
protected  void clipScreenAnchor()
           
 MHMapCellAddress getCursorAddress()
           
 java.awt.Point getCursorPoint()
          Returns the cursorPoint.
 MHMap getMapData()
          Returns a reference to the MHMap object serving as the map's data structure.
 java.awt.Point getScreenAnchor()
          Returns the screenAnchor.
 java.awt.geom.Rectangle2D getScreenSpace()
          Returns the screenSpace.
 int getTileHeight()
          Returns the height of a base tile in this tile map.
 int getTileWidth()
          Returns the width of a base tile in this tile map.
 boolean isAnchorCoordinate(java.awt.Point p)
          Determines if the given point is a valid anchor coordinate.
 boolean isScreenCoordinate(java.awt.Point p)
          Determines if the given point is a valid screen coordinate.
 boolean isWorldCoordinate(java.awt.Point p)
          Determines if the given point is a valid world coordinate.
 MHMapCellAddress mapMouse(java.awt.Point mousePoint)
          Converts mouse coordinates into a map cell address.
 void mouseMoved(java.awt.event.MouseEvent e)
           
 java.awt.Point plotTile(int mapRow, int mapCol)
          Tile plotter.
 void render(java.awt.Graphics2D g)
          Tells an object to draw itself onto the sent Graphics object.
 java.awt.Point screenToWorld(java.awt.Point screen)
          Convert screen coordinates to world coordinates.
 void scrollMap(int scrollX, int scrollY)
          Scrolls the map in the distance specified by the input parameters.
 void setCursorPoint(java.awt.Point cursorPoint)
          Sets the cursorPoint.
 void setScreenAnchor(int x, int y)
          Sets the screen anchor.
 void setScreenAnchor(java.awt.Point screenAnchor)
          Sets the screenAnchor.
 void setScreenSpace(int x, int y, int width, int height)
          Defines the bounds of the screen space.
 void setTileWidth(int width)
          Sets the tile width value .
 java.awt.Point worldToScreen(java.awt.Point world)
          Convert world coordinates to screen coordinates.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_DIRECTIONS

public static final int MAX_DIRECTIONS
Maximum number of directions.

See Also:
Constant Field Values

DIR_NORTH

public static final int DIR_NORTH
North direction.

See Also:
Constant Field Values

DIR_NORTHEAST

public static final int DIR_NORTHEAST
Northeast direction.

See Also:
Constant Field Values

DIR_EAST

public static final int DIR_EAST
East direction.

See Also:
Constant Field Values

DIR_SOUTHEAST

public static final int DIR_SOUTHEAST
Southeast direction.

See Also:
Constant Field Values

DIR_SOUTH

public static final int DIR_SOUTH
South direction.

See Also:
Constant Field Values

DIR_SOUTHWEST

public static final int DIR_SOUTHWEST
Southwest direction.

See Also:
Constant Field Values

DIR_WEST

public static final int DIR_WEST
West direction.

See Also:
Constant Field Values

DIR_NORTHWEST

public static final int DIR_NORTHWEST
Northwest direction.

See Also:
Constant Field Values

screenSpace

protected java.awt.geom.Rectangle2D screenSpace
A rectangle containing the screen coordinates.


worldSpace

protected java.awt.geom.Rectangle2D worldSpace
A rectangle containing all of the coordinates of the entire virtual world.


anchorSpace

protected java.awt.geom.Rectangle2D anchorSpace
A rectangle defining the difference between the screen space and the world space which is used for clipping and stuff.


screenAnchor

protected java.awt.Point screenAnchor
The upper-left corner of the visible portion of the map.


cursorAddress

protected MHMapCellAddress cursorAddress

cursorPoint

protected java.awt.Point cursorPoint

cursorAnchor

protected java.awt.Point cursorAnchor
Constructor Detail

MHTileMap

public MHTileMap(java.lang.String filename,
                 MHObjectFactory vendor)
Constructor. Creates the map data structure and sets up the virtual spaces (screen space, world space, and anchor space).

Parameters:
data - A reference to the application's data model.
filename - The name of the map file to be loaded into this tile map.
vendor - A reference to an existing MHObjectVendor for instantiating interactive actor objects.
Method Detail

plotTile

public java.awt.Point plotTile(int mapRow,
                               int mapCol)
Tile plotter. Converts map coordinates to screen coordinates. Remember, this version is intended for rectangular tile maps. It must be overridden to work for isometric and hex maps.

Parameters:
mapRow - The row of the map whose pixel position is being calculated.
mapCol - The column of the map whose pixel position is being calculated.
Returns:
The pixel position on screen where the base tile is to be rendered.

scrollMap

public void scrollMap(int scrollX,
                      int scrollY)
Scrolls the map in the distance specified by the input parameters.

Parameters:
scrollX - The horizontal distance in pixels to scroll the map
scrollY - The vertical distance in pixels to scroll the map

clipScreenAnchor

protected void clipScreenAnchor()

screenToWorld

public java.awt.Point screenToWorld(java.awt.Point screen)
Convert screen coordinates to world coordinates.

Parameters:
screen - A point in screen coordinates.
Returns:
The input screen point translated into world coordinates.

worldToScreen

public java.awt.Point worldToScreen(java.awt.Point world)
Convert world coordinates to screen coordinates.

Parameters:
world - A point in world space.
Returns:
A point in screen space.

calculateAnchorSpace

protected void calculateAnchorSpace()
Calculate the anchor space for this tile map.


calculateWorldSpace

protected void calculateWorldSpace()
Calculate the world space for this tile map.


getTileWidth

public int getTileWidth()
Returns the width of a base tile in this tile map.


getTileHeight

public int getTileHeight()
Returns the height of a base tile in this tile map. For normal rectangular maps, the tile height is usually the same as the tile width because the tiles are square. Override this method in subclasses requiring something different, such as isometric maps.


isWorldCoordinate

public boolean isWorldCoordinate(java.awt.Point p)
Determines if the given point is a valid world coordinate.

Returns:
True if the given point is a valid point in world space; false otherwise.

isScreenCoordinate

public boolean isScreenCoordinate(java.awt.Point p)
Determines if the given point is a valid screen coordinate.

Returns:
True if the given point is a valid point in screen space; false otherwise.

isAnchorCoordinate

public boolean isAnchorCoordinate(java.awt.Point p)
Determines if the given point is a valid anchor coordinate.

Returns:
True if the given point is a valid point in anchor space; false otherwise.

setScreenSpace

public void setScreenSpace(int x,
                           int y,
                           int width,
                           int height)
Defines the bounds of the screen space. Screen space is the area on the screen where the game world is visible.


mapMouse

public MHMapCellAddress mapMouse(java.awt.Point mousePoint)
Converts mouse coordinates into a map cell address.

Parameters:
mousePoint - A Point indicating the current physical location of the mouse cursor on screen.
Returns:
The address of the map cell containing the mouse cursor.

getMapData

public MHMap getMapData()
Returns a reference to the MHMap object serving as the map's data structure.

Returns:
A reference to the map data object

setTileWidth

public void setTileWidth(int width)
Sets the tile width value .

Parameters:
width - The value to use as the tile width.

advance

public void advance()
Description copied from interface: MHRenderable
Tells an object to update its data.

Specified by:
advance in interface MHRenderable

render

public void render(java.awt.Graphics2D g)
Description copied from interface: MHRenderable
Tells an object to draw itself onto the sent Graphics object.

Specified by:
render in interface MHRenderable
Parameters:
g - The Graphics object on which this object is to draw itself.

clipCursorAddress

protected void clipCursorAddress()
Performs validation on the cursor's map cell address to ensure that it is within the bounds of the map.


mouseMoved

public void mouseMoved(java.awt.event.MouseEvent e)

getScreenSpace

public java.awt.geom.Rectangle2D getScreenSpace()
Returns the screenSpace.

Returns:
Rectangle2D

getCursorAddress

public MHMapCellAddress getCursorAddress()

getScreenAnchor

public java.awt.Point getScreenAnchor()
Returns the screenAnchor.

Returns:
Point

setScreenAnchor

public void setScreenAnchor(java.awt.Point screenAnchor)
Sets the screenAnchor.

Parameters:
screenAnchor - The screenAnchor to set

setScreenAnchor

public void setScreenAnchor(int x,
                            int y)
Sets the screen anchor.

Parameters:
x - The x coordinate of the screen anchor
y - The y coordinate of the screen anchor

getCursorPoint

public java.awt.Point getCursorPoint()
Returns the cursorPoint.

Returns:
Point

setCursorPoint

public void setCursorPoint(java.awt.Point cursorPoint)
Sets the cursorPoint.

Parameters:
cursorPoint - The cursorPoint to set