Rogue: Savage Rats, a retro-themed dungeon crawler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
rogue-savage-rats/src/mightypork/rogue/world/tile/TileModel.java

56 lines
1.0 KiB

package mightypork.rogue.world.tile;
import java.io.IOException;
import mightypork.utils.ion.IonInput;
import mightypork.utils.ion.IonOutput;
/**
* Tile model (builder)
*
* @author Ondřej Hruška (MightyPork)
*/
public final class TileModel {
/** Model ID */
public final int id;
public final Class<? extends Tile> tileClass;
public TileModel(int id, Class<? extends Tile> tile) {
Tiles.register(id, this);
this.id = id;
this.tileClass = tile;
}
/**
* @return new tile of this type
*/
public <T extends Tile> T createTile()
{
try {
return (T) tileClass.getConstructor(TileModel.class).newInstance(this);
} catch (final Exception e) {
throw new RuntimeException("Could not instantiate a tile.", e);
}
}
public Tile loadTile(IonInput in) throws IOException
{
final Tile t = createTile();
t.load(in);
return t;
}
public void saveTile(IonOutput out, Tile tile) throws IOException
{
if (tileClass != tile.getClass()) throw new RuntimeException("Tile class mismatch.");
tile.save(out);
}
}