com.b3dgs.lionengine.game.map
Class AbstractTileMap<TTile extends DefaultTile>

java.lang.Object
  extended by com.b3dgs.lionengine.game.map.AbstractTileMap<TTile>
Type Parameters:
TTile - tile type used.
All Implemented Interfaces:
TileBasedMap<TTile>
Direct Known Subclasses:
AbstractPathMap, AbstractRasteredTileMap

public abstract class AbstractTileMap<TTile extends DefaultTile>
extends java.lang.Object
implements TileBasedMap<TTile>

Abstract representation of a standard tile based map. This class uses a List of List to store tiles, a TreeMap to store patterns references (TiledSprite), and collisions. The way to prepare a map is the following:

 create(width, height); // prepare memory to store tiles
 loadPatterns(this.tilesDir, this.theme); // load tilesheets
 
A simple call to load(...) will automatically perform theses operations.

See Also:
DefaultTile, CollisionType

Field Summary
protected  int heightInTile
          Map size (in tile number).
protected  int screenHeight
          Screen height value.
protected  java.lang.String theme
          Tiles directory and theme.
protected  int tileHeight
          Tile size.
protected  java.lang.String tilesDir
          Tiles directory and theme.
protected  int tileWidth
          Tile size.
protected  int widthInTile
          Map size (in tile number).
 
Constructor Summary
AbstractTileMap(java.lang.String tilesDir, int tileWidth, int tileHeight)
          Create a new tile map.
AbstractTileMap(java.lang.String tilesDir, int tileWidth, int tileHeight, int screenHeight)
          Create a new tile map.
 
Method Summary
protected  void checkDefaultCollision(TTile tile)
          This function is automatically called during load(...), in order to find tile collision if it was not saved (by checking collisions.txt data).
 void clear()
          Remove all tiles from map.
 void create(int widthInTile, int heightInTile)
          Create and prepare map memory area.
 void createMiniMap()
          Generate the minimap from the current map.
 TTile createTile()
          Create a blank tile.
abstract  TTile createTile(int pattern, int number, int x, int y, java.lang.String collision)
          Create a tile using specified data.
 CollisionType getCollision(java.lang.String name)
          Get a collision from its name.
 int getHeightInTiles()
          Get number of vertical tiles.
 java.awt.image.BufferedImage getMiniMap()
          Get minimap surface reference.
 TiledSprite getPattern(java.lang.Integer pattern)
          Get pattern (tilesheet) from its id.
 java.util.Set<java.lang.Integer> getPatterns()
          Get list of patterns id.
 int getPatternsNumber()
          Get the number of used pattern.
 java.lang.String getTheme()
          Get map theme.
 TTile getTile(int v, int h)
          Get tile from specified map location (in tile index).
 int getTileHeight()
          Get height of a tile.
protected  java.awt.Color getTilePixelColor(TTile tile)
          Get color corresponding to the specified tile.
 int getTilesNumber()
          Get number of active tiles (which are not null).
 int getTileWidth()
          Get width of a tile.
 int getWidthInTiles()
          Get number of horizontal tiles.
 void load(FileReader file)
          Load a map from a specified file as binary data.
 void load(XMLNode root)
          Load a map from a specified file as xml.
 void loadPatterns(java.lang.String dir, java.lang.String theme)
          Load map patterns (tiles surfaces) from theme name.
protected  void readCollisions(java.lang.String filename)
          Read collisions from external file.
 void render(java.awt.Graphics2D g, Camera camera, int inTileWidth, int inTileHeight)
          Render map from camera viewpoint, showing a specified area.
 void render(java.awt.Graphics2D g, int sx, int sy, int inTileWidth, int inTileHeight)
          Render map from starting position, showing a specified area.
 void render(java.awt.Graphics2D g, int sx, int sy, int inTileWidth, int inTileHeight, int screenHeight)
          Render map from starting position, showing a specified area.
 void render(java.awt.Graphics2D g, int sx, int sy, int inTileWidth, int inTileHeight, int offsetX, int offsetY)
          Render map from starting position, showing a specified area, including a specific offset.
 void renderMiniMap(java.awt.Graphics2D g, int x, int y)
          Render minimap on graphic output at specified location.
 void renderTile(java.awt.Graphics2D g, int x, int y, TTile tile)
          Render a tile at specified location (in real location).
protected  void renderTile(java.awt.Graphics2D g, TTile tile, int x, int y, int tx, int ty)
          Render a specific tile from specified location.
 void save(FileWriter file)
          Save map to specified file as binary data.
 void save(XMLNode root)
          Save map to specified file as xml.
 void setTile(int v, int h, TTile tile)
          Set a tile at specified map indexs.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tilesDir

protected java.lang.String tilesDir
Tiles directory and theme.


theme

protected java.lang.String theme
Tiles directory and theme.


tileWidth

protected int tileWidth
Tile size.


tileHeight

protected int tileHeight
Tile size.


widthInTile

protected int widthInTile
Map size (in tile number).


heightInTile

protected int heightInTile
Map size (in tile number).


screenHeight

protected int screenHeight
Screen height value.

Constructor Detail

AbstractTileMap

public AbstractTileMap(java.lang.String tilesDir,
                       int tileWidth,
                       int tileHeight)
Create a new tile map.

Parameters:
tilesDir - tiles directory.
tileWidth - tile width.
tileHeight - tile height.

AbstractTileMap

public AbstractTileMap(java.lang.String tilesDir,
                       int tileWidth,
                       int tileHeight,
                       int screenHeight)
Create a new tile map.

Parameters:
tilesDir - tiles directory.
tileWidth - tile width.
tileHeight - tile height.
screenHeight - screen height value.
Method Detail

create

public void create(int widthInTile,
                   int heightInTile)
Create and prepare map memory area. Must be called before assigning tiles.

Parameters:
widthInTile - map width in tile.
heightInTile - map height in tile.

clear

public void clear()
Remove all tiles from map.


loadPatterns

public void loadPatterns(java.lang.String dir,
                         java.lang.String theme)
Load map patterns (tiles surfaces) from theme name. Must be called after map creation. A file called collisions.txt has to be in the same dir, as collisions are loaded at the same time. Collisions are stored this way: #NAME# = {pattern|firstTile-lastTile}, and called with: getCollision(name).

Parameters:
dir - patterns directory.
theme - theme name.

readCollisions

protected void readCollisions(java.lang.String filename)
                       throws java.io.IOException
Read collisions from external file.

Parameters:
filename - file containing collisions.
Throws:
java.io.FileNotFoundException - thrown if file not found.
java.io.IOException - thrown if an error occured while writing.

getCollision

public CollisionType getCollision(java.lang.String name)
Get a collision from its name.

Parameters:
name - collision name.
Returns:
collision data.

render

public void render(java.awt.Graphics2D g,
                   Camera camera,
                   int inTileWidth,
                   int inTileHeight)
Render map from camera viewpoint, showing a specified area.

Parameters:
g - graphic output.
camera - camera viewpoint.
inTileWidth - number of rendered tiles in width.
inTileHeight - number of rendered tiles in height.

render

public void render(java.awt.Graphics2D g,
                   int sx,
                   int sy,
                   int inTileWidth,
                   int inTileHeight)
Render map from starting position, showing a specified area.

Parameters:
g - graphic output.
sx - starting x (view real location x).
sy - starting y (view real location y).
inTileWidth - number of rendered tiles in width.
inTileHeight - number of rendered tiles in height.

render

public void render(java.awt.Graphics2D g,
                   int sx,
                   int sy,
                   int inTileWidth,
                   int inTileHeight,
                   int screenHeight)
Render map from starting position, showing a specified area. This function is usualy used in resizable panels (start rendering from bottom).

Parameters:
g - graphic output.
sx - starting x (view real location x).
sy - starting y (view real location y).
inTileWidth - number of rendered tiles in width.
inTileHeight - number of rendered tiles in height.
screenHeight - manual screen height offset (to start from bottom screen to top).

render

public void render(java.awt.Graphics2D g,
                   int sx,
                   int sy,
                   int inTileWidth,
                   int inTileHeight,
                   int offsetX,
                   int offsetY)
Render map from starting position, showing a specified area, including a specific offset.

Parameters:
g - graphic output.
sx - starting x (view real location x).
sy - starting y (view real location y).
inTileWidth - number of rendered tiles in width.
inTileHeight - number of rendered tiles in height.
offsetX - horizontal map offset (usually used as safe area to avoid negative tiles).
offsetY - vertical map offset (usually used as safe area to avoid negative tiles).

renderTile

protected void renderTile(java.awt.Graphics2D g,
                          TTile tile,
                          int x,
                          int y,
                          int tx,
                          int ty)
Render a specific tile from specified location.

Parameters:
g - graphic output.
tile - tile to render.
x - location x.
y - location y.
tx - current h tile.
ty - current v tile.

renderTile

public void renderTile(java.awt.Graphics2D g,
                       int x,
                       int y,
                       TTile tile)
Description copied from interface: TileBasedMap
Render a tile at specified location (in real location).

Specified by:
renderTile in interface TileBasedMap<TTile extends DefaultTile>
Parameters:
g - graphic output.
x - real location x.
y - real location y.
tile - tile to render.

createTile

public TTile createTile()
Create a blank tile. Data has to be filled later.

Returns:
created tile.

createTile

public abstract TTile createTile(int pattern,
                                 int number,
                                 int x,
                                 int y,
                                 java.lang.String collision)
Create a tile using specified data.

Parameters:
pattern - tile pattern number.
number - tile number inside pattern.
x - tile real location x.
y - tile real location y.
collision - tile collision name.
Returns:
created tile.

getPatternsNumber

public int getPatternsNumber()
Description copied from interface: TileBasedMap
Get the number of used pattern.

Specified by:
getPatternsNumber in interface TileBasedMap<TTile extends DefaultTile>
Returns:
number of used pattern.

save

public void save(FileWriter file)
          throws java.io.IOException
Save map to specified file as binary data. Data are saved this way (using specific types to save space):
 (String) theme
 (short) width in tiles
 (short) height in tiles
 (byte) tile width (use of byte because tile width < 255)
 (byte) tile height (use of byte because tile height < 255)
 (short) tiles number
 for each tile, call tile.save(file)
 
Collisions are not saved, because it is possible to retrieve them from collisions.txt

Parameters:
file - output file.
Throws:
java.io.IOException - thrown in case of output error.

load

public void load(FileReader file)
          throws java.io.IOException
Load a map from a specified file as binary data. Data are loaded this way (see save(file) order):
 (String) theme
 (short) width in tiles
 (short) height in tiles
 (byte) tile width
 (byte) tile height
 (short) tiles number
 for n = 0 to tileNumber
        create blank tile
        call tile.load(file)
        call this.setTile(...) to update map with this new tile
 

Parameters:
file - input file.
Throws:
java.io.IOException - thrown in case of output error.

save

public void save(XMLNode root)
Save map to specified file as xml. Data are saved this way:
 (String) theme
 (integer) width in tiles
 (integer) height in tiles
 (integer) tile width
 (integer tile height
 (integer) tiles number
 for each tile, call tile.save(node)
 
Collisions are not saved, because it is possible to retrieve them from collisions.txt

Parameters:
root - output node.

load

public void load(XMLNode root)
Load a map from a specified file as xml. Data are loaded this way (see save(file) order):
 (String) theme
 (short) width in tiles
 (short) height in tiles
 (byte) tile width
 (byte) tile height
 (short) tiles number
 for n = 0 to tileNumber
        create blank tile
        call tile.load(file)
        call this.setTile(...) to update map with this new tile
 

Parameters:
root - input node.

checkDefaultCollision

protected void checkDefaultCollision(TTile tile)
This function is automatically called during load(...), in order to find tile collision if it was not saved (by checking collisions.txt data).

Parameters:
tile - current tile to check.

setTile

public void setTile(int v,
                    int h,
                    TTile tile)
Set a tile at specified map indexs.

Parameters:
v - vertical index.
h - horizontal index.
tile - tile reference.

getTile

public TTile getTile(int v,
                     int h)
Description copied from interface: TileBasedMap
Get tile from specified map location (in tile index).

Specified by:
getTile in interface TileBasedMap<TTile extends DefaultTile>
Parameters:
v - vertical tile index location.
h - horizontal tile index location.
Returns:
tile reference.

getPattern

public TiledSprite getPattern(java.lang.Integer pattern)
Get pattern (tilesheet) from its id.

Parameters:
pattern - pattern id.
Returns:
pattern found.

getPatterns

public java.util.Set<java.lang.Integer> getPatterns()
Get list of patterns id.

Returns:
set of patterns id.

getTheme

public java.lang.String getTheme()
Get map theme.

Returns:
map theme.

getTilesNumber

public int getTilesNumber()
Get number of active tiles (which are not null).

Returns:
number of non null tile.

getTileWidth

public int getTileWidth()
Description copied from interface: TileBasedMap
Get width of a tile.

Specified by:
getTileWidth in interface TileBasedMap<TTile extends DefaultTile>
Returns:
tile width.

getTileHeight

public int getTileHeight()
Description copied from interface: TileBasedMap
Get height of a tile.

Specified by:
getTileHeight in interface TileBasedMap<TTile extends DefaultTile>
Returns:
tile height.

getWidthInTiles

public int getWidthInTiles()
Description copied from interface: TileBasedMap
Get number of horizontal tiles.

Specified by:
getWidthInTiles in interface TileBasedMap<TTile extends DefaultTile>
Returns:
number of horizontal tiles.

getHeightInTiles

public int getHeightInTiles()
Description copied from interface: TileBasedMap
Get number of vertical tiles.

Specified by:
getHeightInTiles in interface TileBasedMap<TTile extends DefaultTile>
Returns:
number of vertical tiles.

createMiniMap

public void createMiniMap()
Generate the minimap from the current map.


getTilePixelColor

protected java.awt.Color getTilePixelColor(TTile tile)
Get color corresponding to the specified tile.

Parameters:
tile - input tile.
Returns:
color representing the tile on minimap.

renderMiniMap

public void renderMiniMap(java.awt.Graphics2D g,
                          int x,
                          int y)
Render minimap on graphic output at specified location.

Parameters:
g - graphic output.
x - location x.
y - location y.

getMiniMap

public java.awt.image.BufferedImage getMiniMap()
Get minimap surface reference.

Returns:
minimap surface reference.