I created a TileMap-class to load tile sets, create and display tile maps. It is far from perfect so I want your opinions on how to improve it!
How it works:
- Create TileMap object with "new TileMap( int tilesize )".
- Create a tile set with "createSet( String path )" and specify the tile types with "setTileSet( int row, int col, Integer type )". This tile get's saved in a HashMap to later access it by it's type-variable.
- Create a tile map from a file with "createMap( String path )". I want to add in another method to create an empty tile map which you then can edit with "setTileMap( int row, int col, Integer type )" and output this map as a file to later load it (got it working but it's buggy).
I created a second class Map which you can use to output a map file. Please also take a look at that.
(Problem with that: I made a simple method in TileMap called write(path, Tile[][])
which created a Map object and then outputted it but it was buggy. I loaded a map and let the same map be written by this method but it came out differently. Rows and columns were exchange and the whole map were turned 90°. Appreciate your help on that one too).
The Code: I got rid of all JavaDoc since I did my best to explain it above. The code also isn't done so you can find some of my annotations about future ideas in there.
TileMap class:
// Settings
private int tilesize;
// TileSet
private BufferedImage sourceImage;
private HashMap<Integer, Tile> tileList;
// TileMap
private Tile[][] tileMap;
private int mapRows;
private int mapCols;
public TileMap( int tilesize ) {
this.tilesize = tilesize;
}
public void createSet( String path ) throws IOException {
sourceImage = ImageIO.read( getClass().getResourceAsStream( path ) );
tileList = new HashMap<Integer, Tile>();
}
public void setTileSet( int row, int col, Integer type ) {
BufferedImage subImgae = sourceImage.getSubimage( row*tilesize, col*tilesize, tilesize, tilesize );
tileList.put( type, new Tile( subImgae, type ) );
}
public Tile getSetTile( Integer type ) {
return tileList.get( type );
}
public void createMap( String path ) throws NumberFormatException, IOException {
InputStream in = getClass().getResourceAsStream( path );
BufferedReader reader = new BufferedReader( new InputStreamReader( in ) );
mapCols = Integer.parseInt( reader.readLine() );
mapRows = Integer.parseInt( reader.readLine() );
tileMap = new Tile[mapRows][mapCols];
for( int row = 0; row < mapRows; row++ ) {
String line = reader.readLine();
String[] tokens = line.split( " " );
for( int col = 0; col < mapCols; col++ ) {
tileMap[row][col] = getSetTile( Integer.parseInt( tokens[col] ) );
}
}
in.close();
reader.close();
}
public void setTileMap( int row, int col, Integer type ) {
tileMap[row][col] = tileList.get( type );
}
public void drawMap( int x, int y, int width, int height, java.awt.Graphics2D g ) {
for( int row = 0; row < mapRows; row++ ) {
for( int col = 0; col < mapCols; col++ ) {
g.drawImage( tileMap[row][col].getImage(), x + col * tilesize, y + row * tilesize, null );
}
} // add out-of-bound-checking
}
Map class:
private Tile[][] tileList;
private int rows;
private int cols;
public Map( int rows, int cols, Tile[][] tiles ) {
this.rows = rows;
this.cols = cols;
this.tileList = tiles;
}
public void write( String path ) throws FileNotFoundException {
PrintWriter writer = new PrintWriter( path );
// Write Map Size
writer.println( rows );
writer.println( cols );
// Write bytes
for (int col = 0; col < cols; col++) {
for (int row = 0; row < rows; row++) {
writer.print( tileList[row][col].getType() + " " );
}
writer.println( "" );
}
System.out.println( "Map written to " + path );
writer.close();
}
To quickly add something: That's my first ever approach on a tile map and I did it without looking up many tutorials since they all looked confusing to me. Now that I ruffly know how a tile map works I understand them better but still not enough to get along without help - your help :)