Search in sources :

Example 16 with Services

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());
}
Also used : Medias(com.b3dgs.lionengine.Medias) FeaturableModel(com.b3dgs.lionengine.game.feature.FeaturableModel) Setup(com.b3dgs.lionengine.game.feature.Setup) Transformable(com.b3dgs.lionengine.game.feature.Transformable) UtilTransformable(com.b3dgs.lionengine.game.feature.UtilTransformable) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) UtilAssert.assertEquals(com.b3dgs.lionengine.UtilAssert.assertEquals) TransformableModel(com.b3dgs.lionengine.game.feature.TransformableModel) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) UtilMap(com.b3dgs.lionengine.game.feature.tile.map.UtilMap) Services(com.b3dgs.lionengine.game.feature.Services) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) UtilAssert.assertTrue(com.b3dgs.lionengine.UtilAssert.assertTrue) Media(com.b3dgs.lionengine.Media) MapTileSurface(com.b3dgs.lionengine.game.feature.tile.map.MapTileSurface) UtilMapTransition(com.b3dgs.lionengine.game.feature.tile.map.transition.UtilMapTransition) TransformableModel(com.b3dgs.lionengine.game.feature.TransformableModel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MapTileSurface(com.b3dgs.lionengine.game.feature.tile.map.MapTileSurface) Transformable(com.b3dgs.lionengine.game.feature.Transformable) UtilTransformable(com.b3dgs.lionengine.game.feature.UtilTransformable) FeaturableModel(com.b3dgs.lionengine.game.feature.FeaturableModel) MapTile(com.b3dgs.lionengine.game.feature.tile.map.MapTile) Test(org.junit.jupiter.api.Test)

Example 17 with Services

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

Example 18 with Services

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());
}
Also used : Medias(com.b3dgs.lionengine.Medias) BeforeEach(org.junit.jupiter.api.BeforeEach) UtilAssert.assertFalse(com.b3dgs.lionengine.UtilAssert.assertFalse) FeaturableModel(com.b3dgs.lionengine.game.feature.FeaturableModel) Setup(com.b3dgs.lionengine.game.feature.Setup) UtilAssert.assertTimeout(com.b3dgs.lionengine.UtilAssert.assertTimeout) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) UtilSetup(com.b3dgs.lionengine.game.feature.UtilSetup) UtilAssert.assertThrowsTimeout(com.b3dgs.lionengine.UtilAssert.assertThrowsTimeout) AfterAll(org.junit.jupiter.api.AfterAll) UtilAssert.assertNull(com.b3dgs.lionengine.UtilAssert.assertNull) Resolution(com.b3dgs.lionengine.Resolution) Services(com.b3dgs.lionengine.game.feature.Services) UtilAssert.assertNotNull(com.b3dgs.lionengine.UtilAssert.assertNotNull) BeforeAll(org.junit.jupiter.api.BeforeAll) Force(com.b3dgs.lionengine.game.Force) Handler(com.b3dgs.lionengine.game.feature.Handler) SourceResolutionDelegate(com.b3dgs.lionengine.graphic.engine.SourceResolutionDelegate) Transformable(com.b3dgs.lionengine.game.feature.Transformable) UtilAssert.assertEquals(com.b3dgs.lionengine.UtilAssert.assertEquals) TransformableModel(com.b3dgs.lionengine.game.feature.TransformableModel) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) UtilAssert.assertTrue(com.b3dgs.lionengine.UtilAssert.assertTrue) Media(com.b3dgs.lionengine.Media) Factory(com.b3dgs.lionengine.game.feature.Factory) Localizable(com.b3dgs.lionengine.Localizable) Featurable(com.b3dgs.lionengine.game.feature.Featurable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Media(com.b3dgs.lionengine.Media) Factory(com.b3dgs.lionengine.game.feature.Factory) Handler(com.b3dgs.lionengine.game.feature.Handler) Setup(com.b3dgs.lionengine.game.feature.Setup) UtilSetup(com.b3dgs.lionengine.game.feature.UtilSetup) Test(org.junit.jupiter.api.Test)

Example 19 with Services

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);
    }
}
Also used : Medias(com.b3dgs.lionengine.Medias) BeforeEach(org.junit.jupiter.api.BeforeEach) UtilAssert.assertFalse(com.b3dgs.lionengine.UtilAssert.assertFalse) FeaturableModel(com.b3dgs.lionengine.game.feature.FeaturableModel) Setup(com.b3dgs.lionengine.game.feature.Setup) UtilTransformable(com.b3dgs.lionengine.game.feature.UtilTransformable) UtilAssert.assertEquals(com.b3dgs.lionengine.UtilAssert.assertEquals) AtomicReference(java.util.concurrent.atomic.AtomicReference) UtilAssert.assertCause(com.b3dgs.lionengine.UtilAssert.assertCause) Test(org.junit.jupiter.api.Test) Animation(com.b3dgs.lionengine.Animation) UtilAssert.assertNull(com.b3dgs.lionengine.UtilAssert.assertNull) Services(com.b3dgs.lionengine.game.feature.Services) UtilAssert.assertThrows(com.b3dgs.lionengine.UtilAssert.assertThrows) UtilAssert.assertTrue(com.b3dgs.lionengine.UtilAssert.assertTrue) Media(com.b3dgs.lionengine.Media) UtilReflection(com.b3dgs.lionengine.UtilReflection) Featurable(com.b3dgs.lionengine.game.feature.Featurable) AtomicReference(java.util.concurrent.atomic.AtomicReference) FeaturableModel(com.b3dgs.lionengine.game.feature.FeaturableModel) Setup(com.b3dgs.lionengine.game.feature.Setup) Featurable(com.b3dgs.lionengine.game.feature.Featurable) Test(org.junit.jupiter.api.Test)

Aggregations

Services (com.b3dgs.lionengine.game.feature.Services)19 Media (com.b3dgs.lionengine.Media)10 Setup (com.b3dgs.lionengine.game.feature.Setup)10 FeaturableModel (com.b3dgs.lionengine.game.feature.FeaturableModel)9 Test (org.junit.jupiter.api.Test)9 TransformableModel (com.b3dgs.lionengine.game.feature.TransformableModel)8 Medias (com.b3dgs.lionengine.Medias)7 UtilAssert.assertEquals (com.b3dgs.lionengine.UtilAssert.assertEquals)6 UtilAssert.assertTrue (com.b3dgs.lionengine.UtilAssert.assertTrue)6 Featurable (com.b3dgs.lionengine.game.feature.Featurable)6 Transformable (com.b3dgs.lionengine.game.feature.Transformable)6 Camera (com.b3dgs.lionengine.game.feature.Camera)5 AfterAll (org.junit.jupiter.api.AfterAll)5 BeforeAll (org.junit.jupiter.api.BeforeAll)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Handler (com.b3dgs.lionengine.game.feature.Handler)4 MirrorableModel (com.b3dgs.lionengine.game.feature.MirrorableModel)4 UtilSetup (com.b3dgs.lionengine.game.feature.UtilSetup)4 UtilAssert.assertFalse (com.b3dgs.lionengine.UtilAssert.assertFalse)3 Xml (com.b3dgs.lionengine.Xml)3