Separate the notion of tile passability from tile objects since

passability is specific to the miso layer.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@405 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2001-10-08 21:04:26 +00:00
parent d7fb56d203
commit 95ba5ef9cf
14 changed files with 293 additions and 213 deletions
+93 -76
View File
@@ -1,5 +1,5 @@
//
// $Id: TileSet.java,v 1.15 2001/08/30 01:13:08 shaper Exp $
// $Id: TileSet.java,v 1.16 2001/10/08 21:04:25 shaper Exp $
package com.threerings.media.tile;
@@ -26,25 +26,9 @@ public class TileSet
/**
* Construct a new <code>TileSet</code> object.
*/
public TileSet (
String name, int tsid, String imgFile,
int[] rowWidth, int[] rowHeight, int[] tileCount, int[] passable,
Point offsetPos, Point gapDist)
public TileSet ()
{
_name = name;
_tsid = tsid;
_imgFile = imgFile;
_rowWidth = rowWidth;
_rowHeight = rowHeight;
_tileCount = tileCount;
_passable = passable;
_offsetPos = offsetPos;
_gapDist = gapDist;
// determine the total number of tiles in the set
for (int ii = 0; ii < _tileCount.length; ii++) {
_numTiles += _tileCount[ii];
}
_model = new TileSetModel();
}
/**
@@ -52,7 +36,7 @@ public class TileSet
*/
public int getId ()
{
return _tsid;
return _model.tsid;
}
/**
@@ -60,7 +44,7 @@ public class TileSet
*/
public String getName ()
{
return _name;
return _model.name;
}
/**
@@ -68,7 +52,7 @@ public class TileSet
*/
public int getNumTiles ()
{
return _numTiles;
return _model.numTiles;
}
/**
@@ -84,9 +68,9 @@ public class TileSet
{
// load the full tile image if we don't already have it
if (_imgTiles == null) {
if ((_imgTiles = imgmgr.getImage(_imgFile)) == null) {
if ((_imgTiles = imgmgr.getImage(_model.imgFile)) == null) {
Log.warning("Failed to retrieve full tileset image " +
"[file=" + _imgFile + "].");
"[file=" + _model.imgFile + "].");
return null;
}
}
@@ -96,19 +80,19 @@ public class TileSet
ridx = tcount = 0;
// start tile image position at image start offset
tx = _offsetPos.x;
ty = _offsetPos.y;
tx = _model.offsetPos.x;
ty = _model.offsetPos.y;
while ((tcount += _tileCount[ridx]) < tid + 1) {
while ((tcount += _model.tileCount[ridx]) < tid + 1) {
// increment tile image position by row height and gap distance
ty += (_rowHeight[ridx++] + _gapDist.y);
ty += (_model.rowHeight[ridx++] + _model.gapDist.y);
}
// determine the horizontal index of this tile in the row
int xidx = tid - (tcount - _tileCount[ridx]);
int xidx = tid - (tcount - _model.tileCount[ridx]);
// final image x-position is based on tile width and gap distance
tx += (xidx * (_rowWidth[ridx] + _gapDist.x));
tx += (xidx * (_model.rowWidth[ridx] + _model.gapDist.x));
// Log.info("Retrieving tile image [tid=" + tid + ", ridx=" +
// ridx + ", xidx=" + xidx + ", tx=" + tx +
@@ -116,7 +100,7 @@ public class TileSet
// crop the tile-sized image chunk from the full image
return imgmgr.getImageCropped(
_imgTiles, tx, ty, _rowWidth[ridx], _rowHeight[ridx]);
_imgTiles, tx, ty, _model.rowWidth[ridx], _model.rowHeight[ridx]);
}
/**
@@ -133,18 +117,18 @@ public class TileSet
public Tile getTile (ImageManager imgmgr, int tid)
{
// bail if there's no such tile
if (tid > (_numTiles - 1)) {
if (tid > (_model.numTiles - 1)) {
return null;
}
// create the tile object
Tile tile = new Tile(_tsid, tid);
Tile tile = createTile(tid);
// retrieve the tile image
tile.img = getTileImage(imgmgr, tid);
if (tile.img == null) {
Log.warning("Null tile image " +
"[tsid=" + _tsid + ", tid=" + tid + "].");
"[tsid=" + _model.tsid + ", tid=" + tid + "].");
}
// populate the tile's dimensions
@@ -152,61 +136,94 @@ public class TileSet
tile.height = (short)bimg.getHeight();
tile.width = (short)bimg.getWidth();
// and its passability
tile.passable = (_passable[tid] == 1);
return tile;
}
/**
* Returns the tile set model.
*/
public TileSetModel getModel ()
{
return _model;
}
/**
* Return a string representation of the tileset information.
*/
public String toString ()
{
StringBuffer buf = new StringBuffer();
buf.append("[name=").append(_name);
buf.append(", file=").append(_imgFile);
buf.append(", tsid=").append(_tsid);
buf.append(", numtiles=").append(_numTiles);
return buf.append("]").toString();
return _model.toString();
}
/** The tileset name. */
protected String _name;
/** The file containing the tile images. */
protected String _imgFile;
/** The tileset unique identifier. */
protected int _tsid;
/** The width of the tiles in each row in pixels. */
protected int _rowWidth[];
/** The height of each row in pixels. */
protected int _rowHeight[];
/** The number of tiles in each row. */
protected int _tileCount[];
/** Whether each tile is passable. */
protected int _passable[];
/**
* The offset distance (x, y) in pixels from the top-left of the
* image to the start of the first tile image.
* Construct and return a new tile object for further population
* with tile-specific information.
*/
protected Point _offsetPos;
/**
* The distance (x, y) in pixels between each tile in each row
* horizontally, and between each row of tiles vertically.
*/
protected Point _gapDist;
/** The total number of tiles. */
protected int _numTiles;
protected Tile createTile (int tid)
{
return new Tile(_model.tsid, tid);
}
/** The image containing all tile images for this set. */
protected Image _imgTiles;
/** The tile set data model. */
protected TileSetModel _model;
/**
* The model that details the attributes of each tile in a
* tileset. Storing the data separately from the tile set object
* itself allows for the wealth of information associated with a
* tile set to be more cleanly gathered and passed on to the tile
* set constructor by those that need to do so.
*/
public static class TileSetModel
{
/** The tileset name. */
public String name;
/** The tileset unique identifier. */
public int tsid;
/** The file containing the tile images. */
public String imgFile;
/** The width of the tiles in each row in pixels. */
public int[] rowWidth;
/** The height of the tiles in each row in pixels. */
public int[] rowHeight;
/** The number of tiles in each row. */
public int[] tileCount;
/** The number of tiles in the tileset. */
public int numTiles;
/**
* The offset distance (x, y) in pixels from the top-left of the
* image to the start of the first tile image.
*/
public Point offsetPos = new Point();
/**
* The distance (x, y) in pixels between each tile in each row
* horizontally, and between each row of tiles vertically.
*/
public Point gapDist = new Point();
public TileSetModel ()
{
}
public String toString ()
{
StringBuffer buf = new StringBuffer();
buf.append("[name=").append(name);
buf.append(", file=").append(imgFile);
buf.append(", tsid=").append(tsid);
buf.append(", numtiles=").append(numTiles);
return buf.append("]").toString();
}
}
}