Search in sources :

Example 6 with Handler

use of com.b3dgs.lionengine.game.feature.Handler 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 7 with Handler

use of com.b3dgs.lionengine.game.feature.Handler in project lionengine by b3dgs.

the class LauncherModelTest method testListenerAutoAdd.

/**
 * Test the launcher with listener auto add.
 *
 * @throws InterruptedException If error.
 */
@Test
void testListenerAutoAdd() throws InterruptedException {
    final LaunchableObjectSelf object = new LaunchableObjectSelf(services, setup);
    final Launcher launcher = UtilLaunchable.createLauncher(services, setup, object);
    launcher.checkListener(object);
    assertFalse(object.fired.get());
    assertNull(object.firedLaunchable.get());
    assertTimeout(1000L, () -> {
        while (!launcher.fire()) {
            launcher.update(1.0);
            continue;
        }
    });
    final Handler handler = services.get(Handler.class);
    handler.update(1.0);
    assertTrue(object.fired.get());
    assertNotNull(object.firedLaunchable.get());
    launcher.removeListener(object);
    object.fired.set(false);
    object.firedLaunchable.set(null);
    assertFalse(object.fired.get());
    assertNull(object.firedLaunchable.get());
    handler.update(1.0);
    handler.removeAll();
    handler.update(1.0);
    assertEquals(0, handler.size());
}
Also used : Handler(com.b3dgs.lionengine.game.feature.Handler) Test(org.junit.jupiter.api.Test)

Example 8 with Handler

use of com.b3dgs.lionengine.game.feature.Handler in project lionengine by b3dgs.

the class LauncherModelTest method testLauncherFailure.

/**
 * Test the launcher failure.
 */
@Test
void testLauncherFailure() {
    final Media launchableMedia = UtilSetup.createMedia(Featurable.class);
    final Media launcherMedia = UtilLaunchable.createLauncherMedia(launchableMedia);
    final Setup setup = new Setup(launcherMedia);
    final Launcher launcher = UtilLaunchable.createLauncher(services, setup, featurable);
    try {
        assertThrowsTimeout(1000L, () -> {
            while (!launcher.fire()) {
                launcher.update(1.0);
                continue;
            }
        }, "No recognized constructor found for: Featurable.xml");
    } finally {
        final Handler handler = services.get(Handler.class);
        handler.removeAll();
        handler.update(1.0);
        assertEquals(0, handler.size());
        assertTrue(launchableMedia.getFile().delete());
    }
}
Also used : Media(com.b3dgs.lionengine.Media) 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 9 with Handler

use of com.b3dgs.lionengine.game.feature.Handler 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 10 with Handler

use of com.b3dgs.lionengine.game.feature.Handler in project lionengine by b3dgs.

the class LauncherModelTest method testLauncherLevel.

/**
 * Test the launcher level.
 *
 * @throws InterruptedException If error.
 */
@Test
void testLauncherLevel() throws InterruptedException {
    final AtomicBoolean fired = new AtomicBoolean();
    final AtomicReference<Launchable> firedLaunchable = new AtomicReference<>();
    launcher.addListener(UtilLaunchable.createListener(fired));
    launcher.addListener(UtilLaunchable.createListener(firedLaunchable));
    assertTimeout(1000L, () -> {
        while (!launcher.fire()) {
            launcher.update(1.0);
            continue;
        }
    });
    assertTrue(fired.get());
    assertNotNull(firedLaunchable.get());
    firedLaunchable.set(null);
    final Handler handler = services.get(Handler.class);
    handler.update(1.0);
    assertEquals(1, handler.size());
    launcher.setLevel(1);
    assertNull(firedLaunchable.get());
    assertTimeout(1000L, () -> {
        while (!launcher.fire()) {
            launcher.update(1.0);
            continue;
        }
    });
    assertTimeout(1000L, () -> {
        while (!launcher.fire()) {
            launcher.update(1.0);
            continue;
        }
    });
    assertNotNull(firedLaunchable.get());
    handler.update(1.0);
    assertEquals(3, handler.size());
    handler.removeAll();
    handler.update(1.0);
    assertEquals(0, handler.size());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Handler(com.b3dgs.lionengine.game.feature.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Aggregations

Handler (com.b3dgs.lionengine.game.feature.Handler)13 Test (org.junit.jupiter.api.Test)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Media (com.b3dgs.lionengine.Media)4 Factory (com.b3dgs.lionengine.game.feature.Factory)4 Setup (com.b3dgs.lionengine.game.feature.Setup)4 Transformable (com.b3dgs.lionengine.game.feature.Transformable)4 UtilSetup (com.b3dgs.lionengine.game.feature.UtilSetup)4 TransformableModel (com.b3dgs.lionengine.game.feature.TransformableModel)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Localizable (com.b3dgs.lionengine.Localizable)2 Force (com.b3dgs.lionengine.game.Force)2 Services (com.b3dgs.lionengine.game.feature.Services)2 ContextMock (com.b3dgs.lionengine.ContextMock)1 Medias (com.b3dgs.lionengine.Medias)1 Resolution (com.b3dgs.lionengine.Resolution)1 UtilAssert.assertEquals (com.b3dgs.lionengine.UtilAssert.assertEquals)1 UtilAssert.assertFalse (com.b3dgs.lionengine.UtilAssert.assertFalse)1 UtilAssert.assertNotNull (com.b3dgs.lionengine.UtilAssert.assertNotNull)1