use of com.b3dgs.lionengine.game.feature.tile.Tile in project lionengine by b3dgs.
the class MapTilePersisterModel method load.
/**
* Load a map from a specified file as binary data.
* <p>
* Data are loaded this way (see {@link #save(FileWriting)} order):
* </p>
*
* <pre>
* <code>(String)</code> sheets file configuration
* <code>(short)</code> width in tiles
* <code>(short)</code> height in tiles
* <code>(byte)</code> tile width
* <code>(byte)</code> tile height
* <code>(short)</code> number of {@value #BLOC_SIZE} horizontal blocs (widthInTile / {@value #BLOC_SIZE})
* for each blocs tile
* <code>(short)</code> number of tiles in this bloc
* for each tile in this bloc
* create blank tile
* call load(file)
* call setTile(...) to update map with this new tile
* </pre>
*
* @param input The input level file.
* @throws IOException If error on reading.
*/
@Override
public void load(FileReading input) throws IOException {
map.create(input.readInteger(), input.readInteger(), input.readInteger(), input.readInteger());
if (input.readBoolean()) {
map.loadSheets(Medias.create(input.readString()));
}
final int t = input.readShort();
for (int v = 0; v < t; v++) {
final int n = input.readShort();
for (int h = 0; h < n; h++) {
final Tile tile = loadTile(input, v);
if (tile.getSheet().intValue() > map.getSheetsNumber()) {
throw new IOException(ERROR_SHEET_MISSING + Constant.DOUBLE_DOT + tile.getSheet());
}
map.setTile(tile);
}
}
}
use of com.b3dgs.lionengine.game.feature.tile.Tile in project lionengine by b3dgs.
the class MapTileGameTest method testCreate.
/**
* Test map creation.
*/
@Test
public void testCreate() {
Assert.assertFalse(map.isCreated());
map.create(16, 32, 2, 3);
Assert.assertTrue(map.isCreated());
map.loadSheets(new ArrayList<SpriteTiled>());
Assert.assertEquals(16, map.getTileWidth());
Assert.assertEquals(32, map.getTileHeight());
Assert.assertEquals(2 * 16, map.getWidth());
Assert.assertEquals(3 * 32, map.getHeight());
Assert.assertEquals(2, map.getInTileWidth());
Assert.assertEquals(3, map.getInTileHeight());
Assert.assertEquals((int) Math.ceil(StrictMath.sqrt(2.0 * 2.0 + 3.0 * 3.0)), map.getInTileRadius());
final Tile tile = map.createTile(Integer.valueOf(1), 2, 16.0, 32.0);
Assert.assertEquals(1, tile.getSheet().intValue());
Assert.assertEquals(2, tile.getNumber());
Assert.assertEquals(16.0, tile.getX(), UtilTests.PRECISION);
Assert.assertEquals(32.0, tile.getY(), UtilTests.PRECISION);
Assert.assertEquals(1, tile.getInTileX());
Assert.assertEquals(1, tile.getInTileY());
Assert.assertEquals(16, tile.getWidth());
Assert.assertEquals(32, tile.getHeight());
Assert.assertEquals(1, tile.getInTileWidth());
Assert.assertEquals(1, tile.getInTileHeight());
}
use of com.b3dgs.lionengine.game.feature.tile.Tile in project lionengine by b3dgs.
the class MapTileGameTest method testTileSetListener.
/**
* Test map tile set listener.
*/
@Test
public void testTileSetListener() {
map.create(16, 16, 3, 3);
final AtomicReference<Tile> set = new AtomicReference<>();
final TileSetListener listener = tile -> set.set(tile);
map.addListener(listener);
final Tile tile = map.createTile(Integer.valueOf(0), 0, 0.0, 0.0);
map.setTile(tile);
Assert.assertEquals(tile, set.get());
set.set(null);
map.removeListener(listener);
map.setTile(tile);
Assert.assertNull(set.get());
}
use of com.b3dgs.lionengine.game.feature.tile.Tile in project lionengine by b3dgs.
the class MapTileSurfaceModel method getTilesHit.
@Override
public Collection<Tile> getTilesHit(double ox, double oy, double x, double y) {
final Force force = Force.fromVector(ox, oy, x, y);
final double sx = force.getDirectionHorizontal();
final double sy = force.getDirectionVertical();
double h = ox;
double v = oy;
final Collection<Tile> found = new ArrayList<>();
for (int count = 0; count < force.getVelocity(); count++) {
v += sy;
Tile tile = getTileAt(UtilMath.getRound(sx, h), UtilMath.getRound(sy, v));
if (tile != null && !found.contains(tile)) {
found.add(tile);
}
h += sx;
tile = getTileAt(UtilMath.getRound(sx, h), UtilMath.getRound(sy, v));
if (tile != null && !found.contains(tile)) {
found.add(tile);
}
}
return found;
}
use of com.b3dgs.lionengine.game.feature.tile.Tile in project lionengine by b3dgs.
the class MapTileSurfaceModel method getNeighbors.
@Override
public Collection<Tile> getNeighbors(Tile tile) {
final int tx = tile.getInTileX();
final int ty = tile.getInTileY();
final Collection<Tile> neighbors = new HashSet<>(8);
for (int ox = -1; ox < 2; ox++) {
for (int oy = -1; oy < 2; oy++) {
final Tile neighbor = getTile(tx + ox, ty + oy);
if (neighbor != null && (ox != 0 || oy != 0)) {
neighbors.add(neighbor);
}
}
}
return neighbors;
}
Aggregations