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());
}
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());
}
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());
}
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());
}
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 !");
}
}
Aggregations