Search in sources :

Example 11 with Tile

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);
        }
    }
}
Also used : MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Tile(com.b3dgs.lionengine.game.feature.tile.Tile) IOException(java.io.IOException)

Example 12 with 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());
}
Also used : SpriteTiled(com.b3dgs.lionengine.graphic.SpriteTiled) Tile(com.b3dgs.lionengine.game.feature.tile.Tile) Test(org.junit.Test)

Example 13 with Tile

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());
}
Also used : Arrays(java.util.Arrays) LionEngineException(com.b3dgs.lionengine.LionEngineException) Test(org.junit.Test) TileGame(com.b3dgs.lionengine.game.feature.tile.TileGame) AtomicReference(java.util.concurrent.atomic.AtomicReference) SpriteTiled(com.b3dgs.lionengine.graphic.SpriteTiled) ArrayList(java.util.ArrayList) Tile(com.b3dgs.lionengine.game.feature.tile.Tile) Rule(org.junit.Rule) UtilTests(com.b3dgs.lionengine.util.UtilTests) Geom(com.b3dgs.lionengine.geom.Geom) Assert(org.junit.Assert) TemporaryFolder(org.junit.rules.TemporaryFolder) Tile(com.b3dgs.lionengine.game.feature.tile.Tile) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 14 with Tile

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;
}
Also used : Force(com.b3dgs.lionengine.game.Force) ArrayList(java.util.ArrayList) Tile(com.b3dgs.lionengine.game.feature.tile.Tile)

Example 15 with Tile

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;
}
Also used : Tile(com.b3dgs.lionengine.game.feature.tile.Tile) HashSet(java.util.HashSet)

Aggregations

Tile (com.b3dgs.lionengine.game.feature.tile.Tile)59 MapTile (com.b3dgs.lionengine.game.feature.tile.map.MapTile)26 Test (org.junit.jupiter.api.Test)13 TileGame (com.b3dgs.lionengine.game.feature.tile.TileGame)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 Transformable (com.b3dgs.lionengine.game.feature.Transformable)7 FeaturableModel (com.b3dgs.lionengine.game.feature.FeaturableModel)6 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)5 MapTileGroup (com.b3dgs.lionengine.game.feature.tile.map.MapTileGroup)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 LionEngineException (com.b3dgs.lionengine.LionEngineException)2 Media (com.b3dgs.lionengine.Media)2 Force (com.b3dgs.lionengine.game.Force)2 MapTileGame (com.b3dgs.lionengine.game.feature.tile.map.MapTileGame)2 MapTileTransition (com.b3dgs.lionengine.game.feature.tile.map.transition.MapTileTransition)2 SpriteTiled (com.b3dgs.lionengine.graphic.SpriteTiled)2 GroupTransition (com.b3dgs.lionengine.game.feature.tile.map.transition.GroupTransition)1