ch.aplu.jgamegrid
Class GGTileMap

java.lang.Object
  extended by ch.aplu.jgamegrid.GGTileMap

public class GGTileMap
extends java.lang.Object

Class that implements a tile map, e.g. an arragement of rectangular images of the same size like tiles on a floor. A single tile is addressed by its horizontal and vertical indices, ranging from 0..nbHorzTiles-1 and 0..nbVertTiles-1 packed into a Location object. Same tile images are loaded only once to save memory space. A tile image set to null is like an invisible tile.

The tile map may be positioned relative to the playground by setting the position of the upper left vertex in pixel units. The origin of the coordinate system corresponds to the upper left vertex of the playground, x-coordinate to the right, y-coordinate downwards (negative coordinates allowed).

The tile map may be moved by setting the tile position. In every simulation loop or when refresh() is called the images are drawn in the following order:

- background (background image, grid, background drawing using Graphics2D)

- tile map

- actors


Be aware that the size of the tiles are given in pixel units, but an image using a x b pixels has the size (a-1) x (b-1) in pixel units.


Method Summary
 java.awt.Point getCenter(Location loc)
          Returns the current center of tile at given map location with respect to the playground coordinate system (origin at upper left vertex, x-coordinate to the left, y-coordinate downwards).
 int getHeigth()
          Returns total height (in pixels units) of the tile map.
 int getNbHorzTiles()
          Returns the number of horizontal tiles.
 int getNbVertTiles()
          Returns the number of vertical tiles.
 java.awt.Point getPosition()
          Returns current position of upper left vertex with respect to the playground coordinate system (origin at upper left vertex, x-coordinate to the left, y-coordinate downwards).
 java.awt.Point getUpperLeftVertex(Location loc)
          Returns the current coordinates of the upper left vertex of tile at given map location with respect to the playground coordinate system (origin at upper left vertex, x-coordinate to the left, y-coordinate downwards).
 int getWidth()
          Returns total width (in pixels units) of the tile map.
 boolean isTileCollisionEnabled(Location location)
          Returns true, if collision notification with the given tiles is enabled.
 void setCollisionCircle(Location location, java.awt.Point center, int radius)
          Selects the circle (in pixel units) relative to the tile center that is used for collision detection.
 void setCollisionLine(Location location, java.awt.Point startPoint, java.awt.Point endPoint)
          Selects the line segment (in pixel units) relative to the tile that is used for collision detection.
 void setCollisionRectangle(Location location, java.awt.Point center, int width, int height)
          Selects the rectangle (in pixel units) relative to the tile that is used for collision detection.
 void setCollisionSpot(Location location, java.awt.Point spot)
          Selects the hot spot relative to the sprite image that is used for collision detection.
 void setImage(java.lang.String imagePath, int horz, int vert)
          Sets the tile image of the tile with given indices.
 void setImage(java.lang.String imagePath, Location location)
          Sets the tile image of the tile with given map location.In order the image fits exactly on the tile, its size in number of pixels should be tileHeight x tileWidth given when constructing the tile map.
 void setPosition(java.awt.Point point)
          Sets the current position of the upper left vertex with respect to the playground coordinate system (origin at upper left vertex, x-coordinate to the left, y-coordinate downwards).
 void setTileCollisionEnabled(Location location, boolean enable)
          Enable/disable the detection of collisions with the given tile.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setImage

public void setImage(java.lang.String imagePath,
                     int horz,
                     int vert)
Sets the tile image of the tile with given indices. In order the image fits exactly on the tile, its size in number of pixels should be tileHeight x tileWidth given when constructing the tile map.

Parameters:
imagePath - the path to the image file; null if the tile is invisible
horz - the horizontal tile index in the range 0..nbHorzTiles-1
vert - the vertical tile index in the range 0..nbVertTiles-1

setImage

public void setImage(java.lang.String imagePath,
                     Location location)
Sets the tile image of the tile with given map location.In order the image fits exactly on the tile, its size in number of pixels should be tileHeight x tileWidth given when constructing the tile map.

Parameters:
imagePath - the path to the image file; null if the tile is invisible
location - the location of the tile within the map (0..nbHorzTiles-1, 0..nbVertTiles1)

getNbHorzTiles

public int getNbHorzTiles()
Returns the number of horizontal tiles.

Returns:
the number of horizontal tiles

getNbVertTiles

public int getNbVertTiles()
Returns the number of vertical tiles.

Returns:
the number of vertical tiles

getWidth

public int getWidth()
Returns total width (in pixels units) of the tile map.

Returns:
the width of the tile map

getHeigth

public int getHeigth()
Returns total height (in pixels units) of the tile map.

Returns:
the height of the tile map

getPosition

public java.awt.Point getPosition()
Returns current position of upper left vertex with respect to the playground coordinate system (origin at upper left vertex, x-coordinate to the left, y-coordinate downwards).

Returns:
the current (ulx, uly)

setPosition

public void setPosition(java.awt.Point point)
Sets the current position of the upper left vertex with respect to the playground coordinate system (origin at upper left vertex, x-coordinate to the left, y-coordinate downwards).

Parameters:
point - the new (ulx, uly)

getUpperLeftVertex

public java.awt.Point getUpperLeftVertex(Location loc)
Returns the current coordinates of the upper left vertex of tile at given map location with respect to the playground coordinate system (origin at upper left vertex, x-coordinate to the left, y-coordinate downwards).

Parameters:
loc - the tile location within the map (0..nbHorzTiles-1, 0..nbVertTiles-1)
Returns:
the upper left vertex of the tile

getCenter

public java.awt.Point getCenter(Location loc)
Returns the current center of tile at given map location with respect to the playground coordinate system (origin at upper left vertex, x-coordinate to the left, y-coordinate downwards). The center is defined as half the distance (in pixel units) to the neighbour tile (rounded to an integer).

Parameters:
loc - the tile location within the map (0..nbHorzTiles-1, 0..nbVertTiles-1)
Returns:
the center of the tile

setTileCollisionEnabled

public void setTileCollisionEnabled(Location location,
                                    boolean enable)
Enable/disable the detection of collisions with the given tile.

Parameters:
location - the tile location within the tile map
enable - if true, collisions will be notified

isTileCollisionEnabled

public boolean isTileCollisionEnabled(Location location)
Returns true, if collision notification with the given tiles is enabled.

Parameters:
location - the tile location within the tile map
Returns:
true, if collision detection is enabled

setCollisionRectangle

public void setCollisionRectangle(Location location,
                                  java.awt.Point center,
                                  int width,
                                  int height)
Selects the rectangle (in pixel units) relative to the tile that is used for collision detection. The following coordinate system is used:

x-axis to the left, y-axis downward, zero at tile center

Any collision types defined earlier are removed.

Parameters:
location - to location of the tile within the tile map
center - the rectangle center (zero at tile center)
width - the width in pixel units of the rectangle (in x-direction)
height - the height in pixel units of the rectangle (in y-direction)

setCollisionCircle

public void setCollisionCircle(Location location,
                               java.awt.Point center,
                               int radius)
Selects the circle (in pixel units) relative to the tile center that is used for collision detection. The following coordinate system is used:

x-axis to the left, y-axis downward, zero at tile center.

Any collision types defined earlier are removed.

Parameters:
location - to location of the tile within the tile map
center - circle center (zero at tile center)
radius - the radius of the circle (in pixel units)

setCollisionLine

public void setCollisionLine(Location location,
                             java.awt.Point startPoint,
                             java.awt.Point endPoint)
Selects the line segment (in pixel units) relative to the tile that is used for collision detection. The following coordinate system is used:

x-axis to the left, y-axis downward, zero at image center

Any collision types defined earlier are removed.

Parameters:
location - to location of the tile within the tile map
startPoint - the start point of the line (zero at image center)
endPoint - the end point of the line (zero at image center)

setCollisionSpot

public void setCollisionSpot(Location location,
                             java.awt.Point spot)
Selects the hot spot relative to the sprite image that is used for collision detection. The following coordinate system is used:

x-axis to the left, y-axis downward, zero at image center

Any collision types defined earlier are removed.

Parameters:
location - to location of the tile within the tile map
spot - the hot spot (zero at image center)