Search in sources :

Example 46 with Tile

use of com.b3dgs.lionengine.game.feature.tile.Tile in project lionengine by b3dgs.

the class MapTilePathModel method isTileNotAvailable.

/**
 * Check if area if used.
 *
 * @param mover The object moving on map.
 * @param ctx The horizontal tile index.
 * @param cty The vertical tile index.
 * @param ignoreObjectId The object ID to ignore.
 * @return <code>true</code> if area is used, <code>false</code> else.
 */
private boolean isTileNotAvailable(Pathfindable mover, int ctx, int cty, Integer ignoreObjectId) {
    final Collection<Integer> ids = getObjectsId(ctx, cty);
    final Tile tile = map.getTile(ctx, cty);
    if (tile != null) {
        final String category = getCategory(tile);
        return mover.isBlocking(category) || !ids.isEmpty() && (ignoreObjectId == null || !ids.contains(ignoreObjectId));
    }
    return true;
}
Also used : Tile(com.b3dgs.lionengine.game.feature.tile.Tile)

Example 47 with Tile

use of com.b3dgs.lionengine.game.feature.tile.Tile in project lionengine by b3dgs.

the class MapTileCollisionLoader method checkConstraints.

/**
 * Check the tile constraints and get the removable formulas.
 *
 * @param map The map surface reference.
 * @param mapGroup The map group reference.
 * @param tile The current tile to check.
 * @param h The horizontal location.
 * @param v The vertical location.
 * @return The formula to remove.
 */
private Collection<CollisionFormula> checkConstraints(MapTile map, MapTileGroup mapGroup, Tile tile, int h, int v) {
    final Tile top = map.getTile(h, v + 1);
    final Tile bottom = map.getTile(h, v - 1);
    final Tile left = map.getTile(h - 1, v);
    final Tile right = map.getTile(h + 1, v);
    final List<CollisionFormula> toRemove = new ArrayList<>();
    for (final CollisionFormula formula : tilesFormulas.computeIfAbsent(tile, t -> Collections.emptyList())) {
        final CollisionConstraint constraint = formula.getConstraint();
        if (checkConstraint(mapGroup, constraint.getConstraints(Orientation.NORTH), top) || checkConstraint(mapGroup, constraint.getConstraints(Orientation.SOUTH), bottom) || checkConstraint(mapGroup, constraint.getConstraints(Orientation.WEST), left) || checkConstraint(mapGroup, constraint.getConstraints(Orientation.EAST), right)) {
            toRemove.add(formula);
        }
    }
    return toRemove;
}
Also used : ArrayList(java.util.ArrayList) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Tile(com.b3dgs.lionengine.game.feature.tile.Tile)

Example 48 with Tile

use of com.b3dgs.lionengine.game.feature.tile.Tile in project lionengine by b3dgs.

the class MapTilePersisterModelTest method testSaveLoad.

/**
 * Test the save and load map from file.
 *
 * @throws IOException If error.
 */
@Test
void testSaveLoad() throws IOException {
    final MapTile map = UtilMapTilePersister.createMap();
    assertEquals(map.getInTileWidth() * (map.getInTileHeight() - 1), map.getTilesNumber());
    final Media level = Medias.create("level");
    UtilMapTilePersister.saveMap(map, level);
    final MapTile mapLoaded = UtilMapTilePersister.loadMap(level);
    assertEquals(map.getTileWidth(), mapLoaded.getTileWidth());
    assertEquals(map.getTileHeight(), mapLoaded.getTileHeight());
    assertEquals(map.getInTileWidth(), mapLoaded.getInTileWidth());
    assertEquals(map.getInTileHeight(), mapLoaded.getInTileHeight());
    assertEquals(map.getWidth(), mapLoaded.getWidth());
    assertEquals(map.getHeight(), mapLoaded.getHeight());
    for (int x = 0; x < mapLoaded.getInTileWidth(); x++) {
        for (int y = 0; y < mapLoaded.getInTileHeight(); y++) {
            final Tile tile = mapLoaded.getTile(x, y);
            if (y == 0) {
                assertNull(tile);
            } else {
                assertNotNull(tile);
                assertEquals(x * y, tile.getNumber());
                assertEquals(x * mapLoaded.getTileWidth(), tile.getX());
                assertEquals(y * mapLoaded.getTileHeight(), tile.getY());
            }
        }
    }
    assertEquals(map.getTilesNumber(), mapLoaded.getTilesNumber());
    assertTrue(level.getFile().delete());
}
Also used : Media(com.b3dgs.lionengine.Media) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Tile(com.b3dgs.lionengine.game.feature.tile.Tile) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Test(org.junit.jupiter.api.Test)

Example 49 with Tile

use of com.b3dgs.lionengine.game.feature.tile.Tile in project lionengine by b3dgs.

the class MapTileTransitionModelTest method testResolution.

/**
 * Test the map resolution.
 *
 * @param tileNumber The initial tile number.
 * @param groupOrigin The initial group.
 * @param tileNew The new tile number.
 * @param groupNew The new tile group.
 * @param transition The transition between initial tile and new tile.
 */
private static void testResolution(int tileNumber, String groupOrigin, int tileNew, String groupNew, String transition) {
    final MapTile map = UtilMap.createMap(12);
    UtilMap.fill(map, tileNumber);
    map.getFeature(MapTileTransition.class).loadTransitions(config);
    final MapTileGroup mapGroup = map.getFeature(MapTileGroup.class);
    final MapTileTransition mapTransition = map.getFeature(MapTileTransitionModel.class);
    assertEquals(groupOrigin, mapGroup.getGroup(map.getTile(8, 8)));
    map.setTile(8, 8, tileNew);
    final Tile tile = map.getTile(8, 8);
    assertEquals(groupNew, mapGroup.getGroup(map.getTile(8, 8)));
    for (final Tile neighbor : map.getNeighbors(tile)) {
        assertEquals(groupOrigin, mapGroup.getGroup(neighbor));
    }
    mapTransition.resolve(tile);
    for (final Tile neighbor : map.getNeighbors(tile)) {
        assertEquals(transition, mapGroup.getGroup(neighbor));
    }
}
Also used : MapTileGroup(com.b3dgs.lionengine.game.feature.tile.map.MapTileGroup) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Tile(com.b3dgs.lionengine.game.feature.tile.Tile) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile)

Example 50 with Tile

use of com.b3dgs.lionengine.game.feature.tile.Tile in project lionengine by b3dgs.

the class TransitiveGroupTest method testTransitives.

/**
 * Test the transitive groups.
 */
@Test
void testTransitives() {
    final MapTile map = UtilMap.createMap(30);
    UtilMap.fill(map, TILE_WATER);
    final Media config = UtilMapTransition.createTransitions();
    final MapTileGroup mapGroup = map.getFeature(MapTileGroup.class);
    map.getFeature(MapTileTransition.class).loadTransitions(config);
    final TransitiveGroup transitive = new TransitiveGroup(map);
    transitive.load();
    assertEquals(Arrays.asList(new GroupTransition(WATER, GROUND), new GroupTransition(GROUND, TREE)), transitive.getTransitives(WATER, TREE));
    assertEquals(WATER, mapGroup.getGroup(map.getTile(15, 15)));
    map.setTile(15, 15, TILE_TREE);
    final Tile tile = map.getTile(15, 15);
    transitive.checkTransitives(tile);
    assertEquals(TREE, mapGroup.getGroup(map.getTile(15, 15)));
    assertEquals(WATER, mapGroup.getGroup(map.getTile(14, 14)));
    assertEquals(GROUND, mapGroup.getGroup(map.getTile(13, 13)));
    assertTrue(transitive.getTransitives("a", "b").isEmpty());
    assertTrue(transitive.getDirectTransitiveTiles(new Transition(TransitionType.CENTER, "a", "a")).isEmpty());
    assertArrayEquals(Arrays.asList(Integer.valueOf(TILE_TRANSITION2)).toArray(), transitive.getDirectTransitiveTiles(new Transition(TransitionType.UP_LEFT, WATER, TREE)).toArray());
    assertTrue(config.getFile().delete());
}
Also used : MapTileGroup(com.b3dgs.lionengine.game.feature.tile.map.MapTileGroup) Media(com.b3dgs.lionengine.Media) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Tile(com.b3dgs.lionengine.game.feature.tile.Tile) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Test(org.junit.jupiter.api.Test)

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