Search in sources :

Example 31 with MapTile

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

the class MapTileCollisionComputer method computeCollision.

/**
 * Compute the collision from current location.
 *
 * @param map The map surface reference.
 * @param loader The loader reference.
 * @param category The collision category.
 * @param ox The current horizontal location.
 * @param oy The current vertical location.
 * @param x The current horizontal location.
 * @param y The current vertical location.
 * @return The computed collision result, <code>null</code> if none.
 */
private static CollisionResult computeCollision(MapTile map, Function<Tile, List<CollisionFormula>> loader, CollisionCategory category, double ox, double oy, double x, double y) {
    final Tile tile = map.getTileAt(getPositionToSide(ox, x), getPositionToSide(oy, y));
    if (tile != null) {
        Double cx = null;
        Double cy = null;
        CollisionFormula fx = null;
        CollisionFormula fy = null;
        final List<CollisionFormula> formulas = loader.apply(tile);
        for (int i = 0; i < formulas.size(); i++) {
            final CollisionFormula formula = formulas.get(i);
            if (cx == null) {
                cx = getCollisionX(tile, category, formulas, formula, x, y);
                fx = formula;
            }
            if (cy == null) {
                cy = getCollisionY(tile, category, formulas, formula, x, y);
                fy = formula;
            }
            if (cx != null && cy != null) {
                break;
            }
        }
        if (cx != null || cy != null) {
            return new CollisionResult(cx, cy, tile, fx, fy);
        }
    }
    return null;
}
Also used : MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Tile(com.b3dgs.lionengine.game.feature.tile.Tile)

Example 32 with MapTile

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

the class CircuitsExtractorImpl method getCircuits.

/**
 * Get map tile circuits.
 *
 * @param map The map reference.
 * @return The circuits found with their associated tiles.
 */
private static Map<Circuit, Collection<Integer>> getCircuits(MapTile map) {
    final MapTileGroup mapGroup = map.getFeature(MapTileGroup.class);
    final Map<Circuit, Collection<Integer>> circuits = new HashMap<>();
    final MapCircuitExtractor extractor = new MapCircuitExtractor(map);
    for (int ty = 1; ty < map.getInTileHeight() - 1; ty++) {
        for (int tx = 1; tx < map.getInTileWidth() - 1; tx++) {
            final Tile tile = map.getTile(tx, ty);
            if (tile != null && TileGroupType.CIRCUIT == mapGroup.getType(tile)) {
                checkCircuit(circuits, extractor, map, tile);
            }
        }
    }
    return circuits;
}
Also used : MapTileGroup(com.b3dgs.lionengine.game.feature.tile.map.MapTileGroup) HashMap(java.util.HashMap) Collection(java.util.Collection) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Tile(com.b3dgs.lionengine.game.feature.tile.Tile)

Example 33 with MapTile

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

the class CircuitsExtractorImpl method getCircuits.

@Override
public Map<Circuit, Collection<Integer>> getCircuits(Collection<MapTile> maps) {
    final Map<Circuit, Collection<Integer>> circuits = new HashMap<>();
    for (final MapTile map : maps) {
        final Map<Circuit, Collection<Integer>> currents = getCircuits(map);
        for (final Entry<Circuit, Collection<Integer>> entry : currents.entrySet()) {
            final Circuit circuit = entry.getKey();
            final Collection<Integer> tiles = entry.getValue();
            if (circuits.containsKey(circuit)) {
                circuits.get(circuit).addAll(tiles);
            } else {
                circuits.put(circuit, tiles);
            }
        }
    }
    return circuits;
}
Also used : HashMap(java.util.HashMap) Collection(java.util.Collection) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile)

Example 34 with MapTile

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

the class ServicesTest method testCreateGet.

/**
 * Test the service creation.
 */
@Test
public void testCreateGet() {
    final Services services = new Services();
    final Camera camera = services.create(Camera.class);
    final Factory factory = services.create(Factory.class);
    final MapTile map = services.create(MapTileGame.class);
    final MapTileGroup mapGroup = map.addFeatureAndGet(new MapTileGroupModel());
    Assert.assertEquals(services, services.get(Services.class));
    Assert.assertEquals(camera, services.get(Viewer.class));
    Assert.assertEquals(factory, services.get(Factory.class));
    Assert.assertEquals(map, services.get(MapTile.class));
    Assert.assertEquals(mapGroup, map.getFeature(MapTileGroup.class));
}
Also used : MapTileGroup(com.b3dgs.lionengine.game.feature.tile.map.MapTileGroup) MapTileGroupModel(com.b3dgs.lionengine.game.feature.tile.map.MapTileGroupModel) Viewer(com.b3dgs.lionengine.Viewer) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Test(org.junit.Test)

Example 35 with MapTile

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

the class MapTilePersisterModelTest method testInvalidSheet.

/**
 * Test the save and load map from file with invalid tile sheet number.
 *
 * @throws IOException If error.
 */
@Test
public void testInvalidSheet() throws IOException {
    final MapTile map = UtilMapTilePersister.createMap();
    final File levelFile = folder.newFile();
    final Media level = Medias.get(levelFile);
    map.setTile(map.createTile(Integer.valueOf(Integer.MAX_VALUE), 0, 0, 0));
    UtilMapTilePersister.saveMap(map, level);
    try {
        final MapTile mapLoaded = UtilMapTilePersister.loadMap(level);
        Assert.assertNull(mapLoaded);
    } catch (final IOException exception) {
        // Success
        Assert.assertNotNull(exception);
    }
    Assert.assertTrue(levelFile.delete());
}
Also used : Media(com.b3dgs.lionengine.Media) IOException(java.io.IOException) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) File(java.io.File) Test(org.junit.Test)

Aggregations

MapTile (com.b3dgs.lionengine.game.feature.tile.map.MapTile)58 Test (org.junit.jupiter.api.Test)34 Media (com.b3dgs.lionengine.Media)15 Tile (com.b3dgs.lionengine.game.feature.tile.Tile)11 MapTileGame (com.b3dgs.lionengine.game.feature.tile.map.MapTileGame)10 Collection (java.util.Collection)9 MapTileGroup (com.b3dgs.lionengine.game.feature.tile.map.MapTileGroup)8 MapTileGroupModel (com.b3dgs.lionengine.game.feature.tile.map.MapTileGroupModel)7 MapTileTransition (com.b3dgs.lionengine.game.feature.tile.map.transition.MapTileTransition)6 Test (org.junit.Test)6 HashMap (java.util.HashMap)4 FeaturableModel (com.b3dgs.lionengine.game.feature.FeaturableModel)3 Setup (com.b3dgs.lionengine.game.feature.Setup)3 TransformableModel (com.b3dgs.lionengine.game.feature.TransformableModel)3 Featurable (com.b3dgs.lionengine.game.feature.Featurable)2 Transformable (com.b3dgs.lionengine.game.feature.Transformable)2 UtilTransformable (com.b3dgs.lionengine.game.feature.UtilTransformable)2 FileReading (com.b3dgs.lionengine.io.FileReading)2 FileWriting (com.b3dgs.lionengine.io.FileWriting)2 HashSet (java.util.HashSet)2