use of com.b3dgs.lionengine.game.feature.Services in project lionengine by b3dgs.
the class MapTileFogTest method testFog.
/**
* Test the fog.
*/
@Test
void testFog() {
final MapTile map = UtilMap.createMap(5);
services.add(map);
final FovableModel fovable = new FovableModel(services, setup);
final FeaturableModel object = new FeaturableModel(services, setup);
final Transformable transformable = object.addFeatureAndGet(new TransformableModel(services, setup));
transformable.teleport(3, 3);
fovable.prepare(object);
fovable.setFov(1);
final MapTileFog fog = new MapTileFog();
Medias.setLoadFromJar(MapTileFog.class);
fog.create(map.getFeature(MapTileSurface.class), Medias.create("fog.xml"), null);
Medias.setLoadFromJar(null);
final AtomicInteger rtx = new AtomicInteger();
final AtomicInteger rty = new AtomicInteger();
final AtomicInteger count = new AtomicInteger();
final RevealedListener listener = (tx, ty) -> {
rtx.set(tx);
rty.set(ty);
count.incrementAndGet();
};
fog.addListener(listener);
assertEquals(16, fog.getTile(2, 3).getNumber());
assertEquals(16, fog.getTile(3, 3).getNumber());
assertEquals(16, fog.getTile(4, 3).getNumber());
fog.updateFov(fovable);
assertEquals(10, fog.getTile(2, 2).getNumber());
assertEquals(1, fog.getTile(3, 2).getNumber());
assertEquals(11, fog.getTile(4, 2).getNumber());
assertEquals(2, fog.getTile(2, 3).getNumber());
assertEquals(17, fog.getTile(3, 3).getNumber());
assertEquals(3, fog.getTile(4, 3).getNumber());
assertEquals(8, fog.getTile(2, 4).getNumber());
assertEquals(0, fog.getTile(3, 4).getNumber());
assertEquals(9, fog.getTile(4, 4).getNumber());
assertEquals(1, count.get());
assertEquals(3, rtx.get());
assertEquals(3, rty.get());
fog.reset(fovable);
fog.removeListener(listener);
count.set(0);
assertEquals(16, fog.getTile(2, 3).getNumber());
assertEquals(16, fog.getTile(3, 3).getNumber());
assertEquals(16, fog.getTile(4, 3).getNumber());
fog.updateFov(fovable);
assertEquals(0, count.get());
}
use of com.b3dgs.lionengine.game.feature.Services 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 !");
}
}
use of com.b3dgs.lionengine.game.feature.Services in project lionengine by b3dgs.
the class LauncherModelTest method testCheckListener.
/**
* Test check listener conditions.
*/
@Test
void testCheckListener() {
final Media launchableMedia = UtilSetup.createMedia(LaunchableObjectException.class);
final Media launcherMedia = UtilLaunchable.createLauncherMedia(launchableMedia);
final Setup setup = new Setup(launcherMedia);
services.add(new Factory(services));
services.add(new Handler(services));
final AtomicBoolean launchableListener = new AtomicBoolean();
final AtomicBoolean launcherListener = new AtomicBoolean();
final Launcher launcher = new LauncherModel(services, setup) {
@Override
public void addListener(LaunchableListener listener) {
launchableListener.set(true);
}
@Override
public void addListener(LauncherListener listener) {
launcherListener.set(true);
}
};
assertFalse(launchableListener.get());
assertFalse(launcherListener.get());
launcher.checkListener(launcher);
assertFalse(launchableListener.get());
assertFalse(launcherListener.get());
launcher.checkListener((LaunchableListener) l -> l.update(1.0));
assertTrue(launchableListener.get());
assertFalse(launcherListener.get());
launcher.checkListener((LauncherListener) () -> launcher.update(1.0));
launchableListener.set(false);
assertFalse(launchableListener.get());
assertTrue(launcherListener.get());
assertTrue(launchableMedia.getFile().delete());
}
use of com.b3dgs.lionengine.game.feature.Services in project lionengine by b3dgs.
the class StateHandlerTest method testListener.
/**
* Test is state with listener.
*/
@Test
void testListener() {
final AtomicReference<Class<? extends State>> old = new AtomicReference<>();
final AtomicReference<Class<? extends State>> next = new AtomicReference<>();
Medias.setLoadFromJar(StateHandlerTest.class);
try {
final Setup setup = new Setup(Medias.create("Object.xml"));
final Featurable featurable = new FeaturableModel(services, setup);
final StateHandler handler;
handler = featurable.addFeatureAndGet(new StateHandler(services, setup));
handler.prepare(featurable);
final StateTransitionListener listener = (o, n) -> {
old.set(o);
next.set(n);
};
handler.addListener(listener);
handler.changeState(StateIdle.class);
handler.postUpdate();
assertNull(old.get());
assertEquals(StateIdle.class, next.get());
handler.changeState(StateWalk.class);
handler.postUpdate();
assertEquals(StateIdle.class, old.get());
assertEquals(StateWalk.class, next.get());
handler.removeListener(listener);
old.set(null);
next.set(null);
handler.changeState(StateIdle.class);
handler.postUpdate();
assertNull(old.get());
assertNull(next.get());
} finally {
Medias.setLoadFromJar(null);
}
}
Aggregations