public class TiledConverter
extends java.lang.Object
The TiledConverter class, along with the TiledArea class, allow Cell2D games to incorporate information from files created with the map editor Tiled. They accomplish this by interfacing with the TiledReader library, which is a dependency of Cell2D. Instances of TiledConverter have methods that convert data structures from the TiledReader library into data structures native to Cell2D.
A TiledConverter has an associated instance of the TiledReader class (specifically of the FileSystemTiledReader subclass), which is specified upon the TiledConverter's construction. A TiledConverter can only convert TiledResources that were read by its associated TiledReader; the TiledReader and TiledConverter thus form a pipeline that eventually converts Tiled resource file data into Cell2D data structures.
A TiledConverter stores a cache of pointers to the Sprites and Animations corresponding to the TiledTilesets it has converted, as well as to the TiledAreas associated with it, corresponding to the TiledMaps they have been constructed from. A TiledConverter uses this cache to return the very same Sprites and Animations if asked to convert the same TiledTileset multiple times. This is mainly to ensure that, if multiple TiledMaps reference the same TiledTileset, the TiledTileset will not be wastefully converted and stored in memory multiple times. However, a TiledConverter also contains methods that can be called manually to remove pointers from the cache. Removing the pointers to no-longer-needed Sprites, Animations, and TiledAreas is necessary to make those objects vulnerable to the Java garbage collector. (Keep in mind, however, that unloading the Sprites is also necessary to fully free the memory they take up.)
Tiled stores the durations of tile animation frames in milliseconds, but Cell2D stores the durations of Animation frames in fracunits. A TiledConverter converts milliseconds to fracunits using a consistent rate, which by default is one fracunit for every 16 milliseconds. A TiledConverter contains methods for getting and setting this conversion rate.
TiledArea
Constructor and Description |
---|
TiledConverter(org.tiledreader.FileSystemTiledReader reader)
Constructs a new TiledConverter with the specified associated
TiledReader.
|
Modifier and Type | Method and Description |
---|---|
void |
clearAssets()
Removes all of this TiledConverter's cached pointers to the assets that
it has converted from Tiled resources.
|
Animatable |
getAnimatable(org.tiledreader.TiledTile tile,
boolean load,
Filter... filters)
Converts the TiledTileset to which the specified TiledTile belongs, if it
has not already been converted, and returns the Animatable representation
of the specified TiledTile.
|
long |
getFracunitsPerMS()
Returns this TiledConverter's milliseconds-to-fracunits conversion rate,
in fracunits per millisecond.
|
org.tiledreader.FileSystemTiledReader |
getReader()
Returns this TiledConverter's associated TiledReader.
|
java.lang.Iterable<Sprite> |
getSprites(org.tiledreader.TiledTileset tileset,
boolean load,
Filter... filters)
Converts the specified TiledTileset, if it has not already been
converted, and returns an Iterable of the Sprites corresponding to all of
the TiledTileset's TiledTiles.
|
boolean |
removeAssets(org.tiledreader.TiledResource resource,
boolean cleanUp,
boolean removeResources)
Removes this TiledConverter's cached pointer to the assets converted from
the specified resource, if it has converted that resource before.
|
void |
setFracunitsPerMS(long fracunitsPerMS)
Sets this TiledConverter's milliseconds-to-fracunits conversion rate to
the specified value.
|
public TiledConverter(org.tiledreader.FileSystemTiledReader reader)
reader
- This TiledConverter's associated TiledReaderpublic final org.tiledreader.FileSystemTiledReader getReader()
public final long getFracunitsPerMS()
public final void setFracunitsPerMS(long fracunitsPerMS)
fracunitsPerMS
- The new conversion rate, in fracunits per
millisecondpublic final java.lang.Iterable<Sprite> getSprites(org.tiledreader.TiledTileset tileset, boolean load, Filter... filters)
tileset
- The TiledTileset whose corresponding Sprites are to be
returnedload
- If this is true, all of the TiledTileset's Sprites (that are
not already loaded) will be loaded before this method returns.filters
- If the TiledTileset has not yet been converted, this
parameter determines the Filters that should have an effect on the
TiledTileset's Sprites when applied to them with draw().public final Animatable getAnimatable(org.tiledreader.TiledTile tile, boolean load, Filter... filters)
tile
- The TiledTile for which the representative Animatable is to
be returnedload
- If this is true, all of the TiledTileset's Sprites (that are
not already loaded) will be loaded before this method returns.filters
- If the TiledTile's TiledTileset has not yet been
converted, this parameter determines the Filters that should have an
effect on the TiledTileset's Sprites when applied to them with draw().public final boolean removeAssets(org.tiledreader.TiledResource resource, boolean cleanUp, boolean removeResources)
resource
- The resource to forget aboutcleanUp
- If true, also remove the cached pointers to all of the
assets referenced by the assets from the specified resource, and not
referenced by any of the other assets that this TiledConverter still
remembers. This parameter applies recursively, so if the removal of any
of these "orphaned" assets causes more assets to be orphaned, those will
be removed as well.removeResources
- If true, also remove this TiledConverter's
TiledReader's cached pointer to the specified resource, if that pointer
exists. Note that the removal is based on the resource's source path
rather than its identity as an Object, and so if the resource from that
same path has already been removed from and re-read by the TiledReader,
the TiledReader will remove its cached pointer to the new
resource. The cleanUp parameter applies here as well; if it is true, the
TiledReader will also clean up its orphaned resources.public final void clearAssets()