Search in sources :

Example 1 with Handler

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

the class LauncherModelTest method testLauncherException.

/**
 * Test the launcher failure.
 */
@Test
void testLauncherException() {
    final Media launchableMedia = UtilSetup.createMedia(LaunchableObjectException.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;
            }
        }, "Feature not found: " + Launchable.class.getName());
    } 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 2 with Handler

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

the class LauncherModelTest method testLauncher.

/**
 * Test the launcher.
 *
 * @throws InterruptedException If error.
 */
@Test
void testLauncher() 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());
    final Transformable transformable = new TransformableModel(services, setup);
    assertTimeout(1000L, () -> {
        while (!launcher.fire(transformable)) {
            launcher.update(1.0);
            continue;
        }
    });
    assertNotNull(firedLaunchable.get());
    firedLaunchable.set(null);
    handler.update(1.0);
    assertEquals(2, handler.size());
    final Localizable localizable = UtilLaunchable.createLocalizable();
    assertTimeout(1000L, () -> {
        while (!launcher.fire(localizable)) {
            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) TransformableModel(com.b3dgs.lionengine.game.feature.TransformableModel) Handler(com.b3dgs.lionengine.game.feature.Handler) Transformable(com.b3dgs.lionengine.game.feature.Transformable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Localizable(com.b3dgs.lionengine.Localizable) Test(org.junit.jupiter.api.Test)

Example 3 with Handler

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

the class LauncherModelTest method testLauncherSelfListener.

/**
 * Test the launcher with listener itself.
 *
 * @throws InterruptedException If error.
 */
@Test
void testLauncherSelfListener() throws InterruptedException {
    final LaunchableObjectSelf object = new LaunchableObjectSelf(services, setup);
    final Launcher launcher = UtilLaunchable.createLauncher(services, setup, object);
    launcher.addListener((LauncherListener) object);
    launcher.addListener((LaunchableListener) 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());
    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 4 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 5 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)

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