Search in sources :

Example 1 with FileWriting

use of com.b3dgs.lionengine.io.FileWriting in project lionengine by b3dgs.

the class HandlerPersisterTest method testWithMapCollidable.

/**
 * Test with map and collidable.
 *
 * @throws IOException If error.
 */
@Test
void testWithMapCollidable() throws IOException {
    services.add(new Camera());
    final MapTile map = services.add(new MapTileGame());
    map.create(16, 16, 5, 5);
    final Featurable featurable = factory.create(Medias.create("ObjectColl.xml"));
    featurable.getFeature(Transformable.class).teleport(16, 32);
    handler.add(featurable);
    handler.update(1.0);
    final HandlerPersister persister = new HandlerPersister(services);
    final Media media = Medias.create("persister.data");
    try (FileWriting writing = new FileWriting(media)) {
        persister.save(writing);
    }
    final Services services2 = new Services();
    services2.add(new Camera());
    services2.add(new Factory(services2));
    services2.add(map);
    final Handler handler2 = services2.add(new Handler(services2));
    final HandlerPersister persister2 = new HandlerPersister(services2);
    try (FileReading reading = new FileReading(media)) {
        persister2.load(reading);
    }
    handler2.update(1.0);
    final Featurable featurable2 = handler2.values().iterator().next();
    final Transformable transformable = featurable2.getFeature(Transformable.class);
    assertEquals(16.0, transformable.getX());
    assertEquals(32.0, transformable.getY());
    assertTrue(media.getFile().delete());
}
Also used : FileWriting(com.b3dgs.lionengine.io.FileWriting) MapTileGame(com.b3dgs.lionengine.game.feature.tile.map.MapTileGame) Media(com.b3dgs.lionengine.Media) FileReading(com.b3dgs.lionengine.io.FileReading) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Test(org.junit.jupiter.api.Test)

Example 2 with FileWriting

use of com.b3dgs.lionengine.io.FileWriting in project lionengine by b3dgs.

the class HandlerPersisterTest method testWithoutMap.

/**
 * Test without map.
 *
 * @throws IOException If error.
 */
@Test
void testWithoutMap() throws IOException {
    final Featurable featurable = factory.create(Medias.create("ObjectFeatures.xml"));
    featurable.getFeature(Transformable.class).teleport(1, 2);
    handler.add(featurable);
    handler.add(factory.create(Medias.create("ObjectIdentifiable.xml")));
    handler.update(1.0);
    final HandlerPersister persister = new HandlerPersister(services);
    final Media media = Medias.create("persister.data");
    try (FileWriting writing = new FileWriting(media)) {
        persister.save(writing);
    }
    final Services services2 = new Services();
    services2.add(new Factory(services2));
    final Handler handler2 = services2.add(new Handler(services2));
    final HandlerPersister persister2 = new HandlerPersister(services2);
    try (FileReading reading = new FileReading(media)) {
        persister2.load(reading);
    }
    handler2.update(1.0);
    final Transformable transformable = handler2.get(Transformable.class).iterator().next();
    final Iterator<Featurable> iterable = handler2.values().iterator();
    iterable.next();
    assertTrue(iterable.hasNext());
    assertEquals(1.0, transformable.getX());
    assertEquals(2.0, transformable.getY());
    assertTrue(media.getFile().delete());
}
Also used : FileWriting(com.b3dgs.lionengine.io.FileWriting) Media(com.b3dgs.lionengine.Media) FileReading(com.b3dgs.lionengine.io.FileReading) Test(org.junit.jupiter.api.Test)

Example 3 with FileWriting

use of com.b3dgs.lionengine.io.FileWriting in project lionengine by b3dgs.

the class HandlerPersisterTest method testCleanLoad.

/**
 * Test clean on load.
 *
 * @throws IOException If error.
 */
@Test
void testCleanLoad() throws IOException {
    final Featurable featurable = factory.create(Medias.create("ObjectFeatures.xml"));
    handler.add(featurable);
    handler.update(1.0);
    final HandlerPersister persister = new HandlerPersister(services);
    final Media media = Medias.create("persister.data");
    try (FileWriting writing = new FileWriting(media)) {
        persister.save(writing);
    }
    final Services services2 = new Services();
    services2.add(new Factory(services2));
    final Handler handler2 = services2.add(new Handler(services2));
    final HandlerPersister persister2 = new HandlerPersister(services2);
    try (FileReading reading = new FileReading(media)) {
        persister2.load(reading);
    }
    handler2.update(1.0);
    assertEquals(1, handler2.size());
    try (FileReading reading = new FileReading(media)) {
        persister2.load(reading);
    }
    handler2.update(1.0);
    assertEquals(1, handler2.size());
}
Also used : FileWriting(com.b3dgs.lionengine.io.FileWriting) Media(com.b3dgs.lionengine.Media) FileReading(com.b3dgs.lionengine.io.FileReading) Test(org.junit.jupiter.api.Test)

Example 4 with FileWriting

use of com.b3dgs.lionengine.io.FileWriting in project lionengine by b3dgs.

the class HandlerPersisterTest method testWithMap.

/**
 * Test with map.
 *
 * @throws IOException If error.
 */
@Test
void testWithMap() throws IOException {
    services.add(new Camera());
    final MapTile map = services.add(new MapTileGame());
    map.create(16, 16, 5, 5);
    final Featurable featurable = factory.create(Medias.create("ObjectFeatures.xml"));
    featurable.getFeature(Transformable.class).teleport(16, 32);
    handler.add(featurable);
    handler.update(1.0);
    final HandlerPersister persister = new HandlerPersister(services);
    final Media media = Medias.create("persister.data");
    try (FileWriting writing = new FileWriting(media)) {
        persister.save(writing);
    }
    final Services services2 = new Services();
    services2.add(new Factory(services2));
    services2.add(map);
    final Handler handler2 = services2.add(new Handler(services2));
    final HandlerPersister persister2 = new HandlerPersister(services2);
    try (FileReading reading = new FileReading(media)) {
        persister2.load(reading);
    }
    handler2.update(1.0);
    final Featurable featurable2 = handler2.values().iterator().next();
    final Transformable transformable = featurable2.getFeature(Transformable.class);
    assertEquals(16.0, transformable.getX());
    assertEquals(32.0, transformable.getY());
    assertTrue(media.getFile().delete());
}
Also used : FileWriting(com.b3dgs.lionengine.io.FileWriting) MapTileGame(com.b3dgs.lionengine.game.feature.tile.map.MapTileGame) Media(com.b3dgs.lionengine.Media) FileReading(com.b3dgs.lionengine.io.FileReading) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Test(org.junit.jupiter.api.Test)

Example 5 with FileWriting

use of com.b3dgs.lionengine.io.FileWriting in project lionengine by b3dgs.

the class MapTileHelper method importAndSave.

/**
 * Import and save the level.
 *
 * @param levelrip The level rip.
 * @param sheetsConfig The file that define the sheets configuration.
 * @param out The output media.
 * @param mapPersister The persister reference.
 */
public static void importAndSave(Media levelrip, Media sheetsConfig, Media out, MapTilePersister mapPersister) {
    final Services services = new Services();
    final MapTileGame map = services.create(MapTileGame.class);
    map.create(levelrip, sheetsConfig);
    map.addFeature(mapPersister);
    services.add(new Factory(services));
    services.add(new Handler(services));
    final HandlerPersister persister = new HandlerPersister(services);
    try (FileWriting output = new FileWriting(out)) {
        mapPersister.save(output);
        persister.save(output);
    } catch (final IOException exception) {
        Verbose.exception(exception, "Error on saving map !");
    }
}
Also used : Services(com.b3dgs.lionengine.game.feature.Services) FileWriting(com.b3dgs.lionengine.io.FileWriting) MapTileGame(com.b3dgs.lionengine.game.feature.tile.map.MapTileGame) HandlerPersister(com.b3dgs.lionengine.game.feature.HandlerPersister) Factory(com.b3dgs.lionengine.game.feature.Factory) Handler(com.b3dgs.lionengine.game.feature.Handler) IOException(java.io.IOException)

Aggregations

FileWriting (com.b3dgs.lionengine.io.FileWriting)5 Media (com.b3dgs.lionengine.Media)4 FileReading (com.b3dgs.lionengine.io.FileReading)4 Test (org.junit.jupiter.api.Test)4 MapTileGame (com.b3dgs.lionengine.game.feature.tile.map.MapTileGame)3 MapTile (com.b3dgs.lionengine.game.feature.tile.map.MapTile)2 Factory (com.b3dgs.lionengine.game.feature.Factory)1 Handler (com.b3dgs.lionengine.game.feature.Handler)1 HandlerPersister (com.b3dgs.lionengine.game.feature.HandlerPersister)1 Services (com.b3dgs.lionengine.game.feature.Services)1 IOException (java.io.IOException)1