📄 tiledlayer.java
字号:
* @param tileWidth the width in pixels of a single tile
* @param tileHeight the height in pixels of a single tile
* @throws NullPointerException if image is null
* @throws IllegalArgumentException if tileHeight or tileWidth is less than 1
* or if the image width is not an integer multiple of the tileWidth
* or if the image height is not an integer multiple of the tileHeight
*/
public void setStaticTileSet( Image image, int tileWidth, int tileHeight)
{
this.cellWidth = tileWidth;
this.cellHeight = tileHeight;
int columns = image.getWidth() / tileWidth;
int rows = image.getHeight() / tileHeight;
//#ifndef tmp.splitTiles
this.image = image;
this.tileXPositions = new int[ columns ];
int pos = 0;
for (int i = 0; i < columns; i++ ) {
this.tileXPositions[ i ] = pos;
pos += tileWidth;
}
this.tileYPositions = new int[ rows ];
pos = 0;
for (int i = 0; i < rows; i++ ) {
this.tileYPositions[ i ] = pos;
pos += tileHeight;
}
//#else
//# // split the image into single tiles:
//# Image[] tileImages = new Image[ columns * rows ];
//# int index = 0;
//# for (int row = 0; row < rows; row++) {
//# for (int column = 0; column < columns; column++) {
//#if polish.api.nokia-ui && !tmp.useBackBuffer
//# Image tile = DirectUtils.createImage( tileWidth, tileHeight, 0x00FFFFFF );
//# Graphics g = tile.getGraphics();
//# // when creating an transparent image, one must not "touch"
//# // that image with an ordinary Graphics-object --- instead
//# // ALWAYS an DirectGraphics-object needs to be used. Sigh!
//# //g.drawImage(this.image, 0, 0, Graphics.TOP | Graphics.LEFT );
//# DirectGraphics dg = DirectUtils.getDirectGraphics(g);
//# dg.drawImage(image, -1 * column * tileWidth, -1 * row * tileHeight, Graphics.TOP | Graphics.LEFT, 0 );
//#else
//# Image tile = Image.createImage( tileWidth, tileHeight );
//# Graphics g = tile.getGraphics();
//# g.drawImage( image, -1 * column * tileWidth, -1 * row * tileHeight, Graphics.TOP | Graphics.LEFT );
//#endif
//# tileImages[index] = tile;
//# index++;
//# }
//# }
//# this.tiles = tileImages;
//#endif
if ( columns * rows < this.numberOfTiles ) {
// clear the grid, when there are not as many tiles as in the previous set:
for (int i = 0; i < this.grid.length; i++) {
for (int j = 0; j < this.grid[i].length; j++) {
this.grid[i][j] = 0;
}
}
}
this.numberOfTiles = columns * rows;
this.numberOfColumns = columns;
//this.numberOfRows = rows;
}
/**
* Creates a new animated tile and returns the index that refers
* to the new animated tile. It is initially associated with
* the specified tile index (either a static tile or 0).
* <P>
* The indices for animated tiles are always negative. The first
* animated tile shall have the index -1, the second, -2, etc.
*
* @param staticTileIndex the index of the associated tile (must be 0 or a valid static tile index)
* @return the index of newly created animated tile
* @throws IndexOutOfBoundsException if the staticTileIndex is invalid
*/
public int createAnimatedTile(int staticTileIndex)
{
if (staticTileIndex >= this.numberOfTiles ) {
//#ifdef polish.debugVerbose
//# throw new IllegalArgumentException("invalid static tile index: " + staticTileIndex + " (there are only [" + this.numberOfTiles + "] tiles available.");
//#else
throw new IllegalArgumentException();
//#endif
}
if (this.animatedTiles == null) {
//#ifdef polish.TiledLayer.GridType:defined
//#= this.animatedTiles = new ${ polish.TiledLayer.GridType }[4];
//#else
this.animatedTiles = new byte[4];
//#endif
} else if (this.animatedTilesLength == this.animatedTiles.length) {
//#ifdef polish.TiledLayer.GridType:defined
//#= ${ polish.TiledLayer.GridType }[] newAnimatedTiles = new ${ polish.TiledLayer.GridType }[ this.animatedTilesLength * 2 ];
//#else
byte[] newAnimatedTiles = new byte[ this.animatedTilesLength * 2 ];
//#endif
System.arraycopy( this.animatedTiles, 0, newAnimatedTiles, 0, this.animatedTilesLength);
this.animatedTiles = newAnimatedTiles;
}
//#ifdef polish.TiledLayer.GridType:defined
//#= this.animatedTiles[ this.animatedTilesLength ] = (${ polish.TiledLayer.GridType }) staticTileIndex;
//#else
this.animatedTiles[ this.animatedTilesLength ] = (byte) staticTileIndex;
//#endif
this.animatedTilesLength++;
return -1 * this.animatedTilesLength;
}
/**
* Associates an animated tile with the specified static tile.
*
* @param animatedTileIndex the index of the animated tile
* @param staticTileIndex the index of the associated tile (must be 0 or a valid static tile index)
* @throws IndexOutOfBoundsException if the staticTileIndex is invalid
* or if the animated tile index is invalid
* @see #getAnimatedTile(int)
*/
public void setAnimatedTile(int animatedTileIndex, int staticTileIndex)
{
//#ifndef polish.skipArgumentCheck
if (staticTileIndex > this.numberOfTiles ) {
//#ifdef polish.debugVerbose
//# throw new IllegalArgumentException("invalid static tile index: " + staticTileIndex + " (there are only [" + this.numberOfTiles + "] tiles available.");
//#else
throw new IllegalArgumentException();
//#endif
}
//#endif
int animatedIndex = (-1 * animatedTileIndex) - 1;
//#ifdef polish.TiledLayer.GridType:defined
//#= this.animatedTiles[ animatedIndex ] = (${ polish.TiledLayer.GridType }) staticTileIndex;
//#else
this.animatedTiles[ animatedIndex ] = (byte) staticTileIndex;
//#endif
}
/**
* Gets the tile referenced by an animated tile.
* <p>
*
* Returns the tile index currently associated with the
* animated tile.
*
* @param animatedTileIndex the index of the animated tile
* @return the index of the tile reference by the animated tile
* @throws IndexOutOfBoundsException if the animated tile index is invalid
* @see #setAnimatedTile(int, int)
*/
public int getAnimatedTile(int animatedTileIndex)
{
int animatedIndex = (-1 * animatedTileIndex) - 1;
return this.animatedTiles[animatedIndex ];
}
/**
* Sets the contents of a cell.
*
* <P>
* The contents may be set to a static tile index, an animated
* tile index, or it may be left empty (index 0)
*
* @param col the column of cell to set
* @param row the row of cell to set
* @param tileIndex the index of tile to place in cell
* @throws IndexOutOfBoundsException if there is no tile with index tileIndex
* or if row or col is outside the bounds of the TiledLayer grid
* @see #getCell(int, int)
* @see #fillCells(int, int, int, int, int)
*/
public void setCell(int col, int row, int tileIndex)
{
//#ifndef polish.skipArgumentCheck
if (tileIndex > this.numberOfTiles ) {
//#ifdef polish.debugVerbose
//# throw new IllegalArgumentException("invalid static tile index: " + tileIndex + " (there are only [" + this.numberOfTiles + "] tiles available.");
//#else
throw new IllegalArgumentException();
//#endif
}
//#endif
//#ifdef polish.TiledLayer.GridType:defined
//#= this.grid[ col ][ row ] = (${ polish.TiledLayer.GridType }) tileIndex;
//#else
this.grid[ col ][ row ] = (byte) tileIndex;
//#endif
}
/**
* Gets the contents of a cell.
*
* <p>
* Gets the index of the static or animated tile currently displayed in
* a cell. The returned index will be 0 if the cell is empty.
*
* @param col the column of cell to check
* @param row the row of cell to check
* @return the index of tile in cell
* @throws IndexOutOfBoundsException if row or col is outside the bounds of the TiledLayer grid
* @see #setCell(int, int, int)
* @see #fillCells(int, int, int, int, int)
*/
public int getCell(int col, int row)
{
return this.grid[ col ][ row ];
}
/**
* Fills a region cells with the specific tile. The cells may be filled
* with a static tile index, an animated tile index, or they may be left
* empty (index <code>0</code>).
*
* @param col the column of top-left cell in the region
* @param row the row of top-left cell in the region
* @param numCols the number of columns in the region
* @param numRows the number of rows in the region
* @param tileIndex the Index of the tile to place in all cells in the specified region
* @throws IndexOutOfBoundsException if the rectangular region defined by the parameters extends beyond the bounds of the TiledLayer grid
* or if there is no tile with index tileIndex
* @throws IllegalArgumentException if numCols is less than zero
* or if numRows is less than zero
* @see #setCell(int, int, int)
* @see #getCell(int, int)
*/
public void fillCells(int col, int row, int numCols, int numRows, int tileIndex)
{
//#ifndef polish.skipArgumentCheck
if (tileIndex > this.numberOfTiles) {
//#ifdef polish.debugVerbose
//# throw new IllegalArgumentException("invalid static tile index: " + tileIndex + " (there are only [" + this.numberOfTiles + "] tiles available.");
//#else
throw new IllegalArgumentException();
//#endif
}
//#endif
int endCols = col + numCols;
int endRows = row + numRows;
for (int i = col; i < endCols; i++ ) {
for (int j = row; j < endRows; j++) {
//#ifdef polish.TiledLayer.GridType:defined
//#= this.grid[ i ][ j] = (${ polish.TiledLayer.GridType }) tileIndex;
//#else
this.grid[ i ][ j ] = (byte) tileIndex;
//#endif
}
}
}
/**
* Gets the width of a single cell, in pixels.
*
* @return the width in pixels of a single cell in the TiledLayer grid
*/
public final int getCellWidth()
{
return this.cellWidth;
}
/**
* Gets the height of a single cell, in pixels.
*
* @return the height in pixels of a single cell in the TiledLayer grid
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -