Search in sources :

Example 1 with Collision

use of com.b3dgs.lionengine.game.feature.collidable.Collision in project lionengine by b3dgs.

the class CollidableFramedModel method recycle.

/*
     * CollidableFramed
     */
@Override
public void recycle() {
    for (final Collision collision : config.getCollisions()) {
        collidable.addCollision(collision);
        collidable.setEnabled(false, collision);
    }
    animatable.addListener((AnimatorFrameListener) frame -> {
        for (final Collision collision : last) {
            collidable.setEnabled(false, collision);
        }
        last = config.getCollision(Integer.valueOf(frame));
        for (final Collision collision : last) {
            collidable.setEnabled(true, collision);
            collidable.forceUpdate();
        }
    });
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) Collidable(com.b3dgs.lionengine.game.feature.collidable.Collidable) Collection(java.util.Collection) Setup(com.b3dgs.lionengine.game.feature.Setup) AnimatorFrameListener(com.b3dgs.lionengine.AnimatorFrameListener) Configurer(com.b3dgs.lionengine.game.Configurer) Recyclable(com.b3dgs.lionengine.game.feature.Recyclable) Services(com.b3dgs.lionengine.game.feature.Services) Collision(com.b3dgs.lionengine.game.feature.collidable.Collision) Animatable(com.b3dgs.lionengine.game.feature.Animatable) FeatureGet(com.b3dgs.lionengine.game.feature.FeatureGet) Collections(java.util.Collections) Featurable(com.b3dgs.lionengine.game.feature.Featurable) FeatureModel(com.b3dgs.lionengine.game.feature.FeatureModel) Collision(com.b3dgs.lionengine.game.feature.collidable.Collision)

Example 2 with Collision

use of com.b3dgs.lionengine.game.feature.collidable.Collision in project lionengine by b3dgs.

the class CollidableFramedConfig method importFrame.

/**
 * Import frame collision data.
 *
 * @param node The node root reference.
 * @param framed The framed node reference.
 * @param start The collision start number.
 * @param collisions The imported collisions.
 */
private static void importFrame(XmlReader node, XmlReader framed, int start, Map<Integer, Collection<Collision>> collisions) {
    final String name = getFrameName(node, framed);
    if (framed.hasAttribute(ATT_NUMBER)) {
        final int number = start + framed.getInteger(ATT_NUMBER);
        final Collision collision = createCollision(name, framed, number - start);
        final Integer key = Integer.valueOf(number - 1);
        collisions.computeIfAbsent(key, k -> new ArrayList<>()).add(collision);
    } else {
        final int end = node.getInteger(AnimationConfig.ANIMATION_END);
        for (int number = start; number <= end; number++) {
            final Collision collision = createCollision(name, framed, number - start + 1);
            final Integer key = Integer.valueOf(number);
            collisions.computeIfAbsent(key, k -> new ArrayList<>()).add(collision);
        }
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) Collection(java.util.Collection) Check(com.b3dgs.lionengine.Check) HashMap(java.util.HashMap) Configurer(com.b3dgs.lionengine.game.Configurer) ArrayList(java.util.ArrayList) Constant(com.b3dgs.lionengine.Constant) Xml(com.b3dgs.lionengine.Xml) XmlReader(com.b3dgs.lionengine.XmlReader) Collision(com.b3dgs.lionengine.game.feature.collidable.Collision) Map(java.util.Map) Entry(java.util.Map.Entry) Collections(java.util.Collections) AnimationConfig(com.b3dgs.lionengine.game.AnimationConfig) Collision(com.b3dgs.lionengine.game.feature.collidable.Collision) ArrayList(java.util.ArrayList)

Example 3 with Collision

use of com.b3dgs.lionengine.game.feature.collidable.Collision in project lionengine by b3dgs.

the class CollidableFramedConfigTest method testExportsImports.

/**
 * Test exports imports.
 */
@Test
void testExportsImports() {
    final Map<Integer, Collection<Collision>> collisions = new HashMap<>();
    collisions.put(Integer.valueOf(1), Arrays.asList(new Collision("anim%1", 0, 1, 2, 3, true)));
    final CollidableFramedConfig config = new CollidableFramedConfig(collisions);
    final Xml root = new Xml("test");
    final Animation animation = new Animation("anim", 1, 2, 3.0, false, true);
    AnimationConfig.exports(root, animation);
    final Xml framed = root.getChildXml(AnimationConfig.NODE_ANIMATIONS).getChildXml(AnimationConfig.NODE_ANIMATION);
    CollidableFramedConfig.exports(framed, collisions);
    final Media media = Medias.create("Object.xml");
    root.save(media);
    assertEquals(config, CollidableFramedConfig.imports(new Configurer(media)));
    assertEquals(collisions.values().iterator().next(), config.getCollisions());
    assertEquals(collisions.get(Integer.valueOf(1)), config.getCollision(Integer.valueOf(1)));
    assertTrue(config.getCollision(Integer.valueOf(2)).isEmpty());
    assertTrue(media.getFile().delete());
}
Also used : HashMap(java.util.HashMap) Xml(com.b3dgs.lionengine.Xml) Collision(com.b3dgs.lionengine.game.feature.collidable.Collision) Media(com.b3dgs.lionengine.Media) Collection(java.util.Collection) Animation(com.b3dgs.lionengine.Animation) Configurer(com.b3dgs.lionengine.game.Configurer) Test(org.junit.jupiter.api.Test)

Example 4 with Collision

use of com.b3dgs.lionengine.game.feature.collidable.Collision in project lionengine by b3dgs.

the class CollidableFramedConfigTest method testExportsImportsNumber.

/**
 * Test exports imports with number.
 */
@Test
void testExportsImportsNumber() {
    final Map<Integer, Collection<Collision>> collisions = new HashMap<>();
    collisions.put(Integer.valueOf(1), Arrays.asList(new Collision("coll%anim%1", 0, 1, 2, 3, true)));
    final Xml root = new Xml("test");
    final Animation animation = new Animation("anim", 1, 2, 3.0, false, true);
    AnimationConfig.exports(root, animation);
    final Xml framed = root.getChildXml(AnimationConfig.NODE_ANIMATIONS).getChildXml(AnimationConfig.NODE_ANIMATION);
    CollidableFramedConfig.exports(framed, collisions);
    framed.getChildXml(CollidableFramedConfig.NODE_COLLISION_FRAMED).removeAttribute(CollidableFramedConfig.ATT_NUMBER);
    framed.getChildXml(CollidableFramedConfig.NODE_COLLISION_FRAMED).writeString(CollidableFramedConfig.ATT_PREFIX, "coll");
    final Media media = Medias.create("Object.xml");
    root.save(media);
    final CollidableFramedConfig imported = CollidableFramedConfig.imports(new Configurer(media));
    assertEquals(collisions.get(Integer.valueOf(1)), imported.getCollision(Integer.valueOf(1)));
    assertFalse(imported.getCollision(Integer.valueOf(2)).isEmpty());
    assertTrue(media.getFile().delete());
}
Also used : HashMap(java.util.HashMap) Xml(com.b3dgs.lionengine.Xml) Collision(com.b3dgs.lionengine.game.feature.collidable.Collision) Media(com.b3dgs.lionengine.Media) Collection(java.util.Collection) Animation(com.b3dgs.lionengine.Animation) Configurer(com.b3dgs.lionengine.game.Configurer) Test(org.junit.jupiter.api.Test)

Example 5 with Collision

use of com.b3dgs.lionengine.game.feature.collidable.Collision in project lionengine by b3dgs.

the class CollidableFramedModelTest method beforeTests.

/**
 * Prepare test.
 */
@BeforeAll
public static void beforeTests() {
    Graphics.setFactoryGraphic(new FactoryGraphicMock());
    Medias.setResourcesDirectory(System.getProperty("java.io.tmpdir"));
    config = UtilSetup.createConfig(CollidableFramedModelTest.class);
    final Map<Integer, Collection<Collision>> collisions = new HashMap<>();
    collisions.put(Integer.valueOf(1), Arrays.asList(new Collision("anim%1", 0, 0, 2, 2, false)));
    final Xml root = new Xml(config);
    AnimationConfig.exports(root, new Animation("anim", 1, 2, 1.0, false, false));
    final Xml framed = root.getChildXml(AnimationConfig.NODE_ANIMATIONS).getChildXml(AnimationConfig.NODE_ANIMATION);
    CollidableFramedConfig.exports(framed, collisions);
    root.save(config);
}
Also used : HashMap(java.util.HashMap) Xml(com.b3dgs.lionengine.Xml) Collision(com.b3dgs.lionengine.game.feature.collidable.Collision) FactoryGraphicMock(com.b3dgs.lionengine.graphic.FactoryGraphicMock) Collection(java.util.Collection) Animation(com.b3dgs.lionengine.Animation) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

Collision (com.b3dgs.lionengine.game.feature.collidable.Collision)6 Collection (java.util.Collection)6 Xml (com.b3dgs.lionengine.Xml)5 Configurer (com.b3dgs.lionengine.game.Configurer)4 HashMap (java.util.HashMap)4 Animation (com.b3dgs.lionengine.Animation)3 LionEngineException (com.b3dgs.lionengine.LionEngineException)2 Media (com.b3dgs.lionengine.Media)2 Collections (java.util.Collections)2 Test (org.junit.jupiter.api.Test)2 AnimatorFrameListener (com.b3dgs.lionengine.AnimatorFrameListener)1 Check (com.b3dgs.lionengine.Check)1 Constant (com.b3dgs.lionengine.Constant)1 XmlReader (com.b3dgs.lionengine.XmlReader)1 AnimationConfig (com.b3dgs.lionengine.game.AnimationConfig)1 Animatable (com.b3dgs.lionengine.game.feature.Animatable)1 Featurable (com.b3dgs.lionengine.game.feature.Featurable)1 FeatureGet (com.b3dgs.lionengine.game.feature.FeatureGet)1 FeatureModel (com.b3dgs.lionengine.game.feature.FeatureModel)1 Recyclable (com.b3dgs.lionengine.game.feature.Recyclable)1