Search in sources :

Example 21 with Animation

use of com.b3dgs.lionengine.Animation in project lionengine by b3dgs.

the class AttackerModelTest method testListener.

/**
 * Test the attack.
 *
 * @throws InterruptedException If error.
 */
@Test
void testListener() throws InterruptedException {
    canAttack.set(true);
    attacker.setAttackChecker(t -> canAttack.get());
    final AtomicBoolean preparing = new AtomicBoolean();
    final AtomicReference<Transformable> reaching = new AtomicReference<>();
    final AtomicReference<Transformable> started = new AtomicReference<>();
    final AtomicBoolean anim = new AtomicBoolean();
    final AtomicReference<Transformable> ended = new AtomicReference<>();
    final AtomicBoolean stopped = new AtomicBoolean();
    final AttackerListener listener;
    listener = UtilAttackable.createListener(preparing, reaching, started, ended, anim, stopped);
    attacker.addListener(listener);
    attacker.recycle();
    attacker.update(1.0);
    attacker.getFeature(Transformable.class).teleport(0, 0);
    attacker.setAttackDelay(5);
    attacker.setAttackFrame(1);
    attacker.setAttackDistance(new Range(0, 2));
    target.setSize(1, 1);
    target.teleport(5, 5);
    attacker.attack(target);
    attacker.update(1.0);
    attacker.update(1.0);
    // 2 ticks for attack interval
    attacker.update(1.0);
    attacker.getFeature(Animatable.class).play(new Animation("test", 1, 1, 1.0, false, false));
    assertEquals(target, reaching.get());
    assertFalse(preparing.get());
    assertFalse(attacker.isAttacking());
    target.teleport(0, 1);
    attacker.update(1.0);
    assertNotEquals(target, started.get());
    assertNotEquals(target, ended.get());
    attacker.update(1.0);
    assertTrue(attacker.isAttacking());
    attacker.update(1.0);
    attacker.getFeature(Animatable.class).update(1.0);
    attacker.update(1.0);
    assertFalse(preparing.get());
    attacker.update(1.0);
    attacker.update(1.0);
    attacker.update(1.0);
    assertTrue(preparing.get());
    assertTimeout(1000L, () -> {
        while (!target.equals(started.get())) {
            attacker.update(1.0);
        }
    });
    assertEquals(target, started.get());
    assertEquals(target, ended.get());
    attacker.update(1.0);
    assertTrue(attacker.isAttacking());
    object.getFeature(Animatable.class).update(1.0);
    attacker.update(1.0);
    assertTrue(anim.get());
    assertFalse(stopped.get());
    attacker.stopAttack();
    assertFalse(stopped.get());
    attacker.update(1.0);
    assertTrue(stopped.get());
    attacker.removeListener(listener);
    preparing.set(false);
    reaching.set(null);
    started.set(null);
    ended.set(null);
    anim.set(false);
    attacker.attack(target);
    attacker.update(1.0);
    attacker.update(1.0);
    assertFalse(preparing.get());
    assertNull(reaching.get());
    assertNull(started.get());
    assertNull(ended.get());
    assertFalse(anim.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Animatable(com.b3dgs.lionengine.game.feature.Animatable) UtilTransformable(com.b3dgs.lionengine.game.feature.UtilTransformable) Transformable(com.b3dgs.lionengine.game.feature.Transformable) Animation(com.b3dgs.lionengine.Animation) AtomicReference(java.util.concurrent.atomic.AtomicReference) Range(com.b3dgs.lionengine.Range) Test(org.junit.jupiter.api.Test)

Example 22 with Animation

use of com.b3dgs.lionengine.Animation in project lionengine by b3dgs.

the class StateAnimationBasedTest method setUp.

/**
 * Prepare test.
 */
@BeforeClass
public static void setUp() {
    Medias.setResourcesDirectory(System.getProperty("java.io.tmpdir"));
    media = Medias.create("animations.xml");
    final Xml root = new Xml("test");
    final Animation idle = new Animation("idle", 1, 2, 3.0, false, true);
    AnimationConfig.exports(root, idle);
    final Animation walk = new Animation("walk", 1, 2, 3.0, false, true);
    AnimationConfig.exports(root, walk);
    final Animation jump = new Animation("jump", 1, 2, 3.0, false, true);
    AnimationConfig.exports(root, jump);
    root.save(media);
}
Also used : Xml(com.b3dgs.lionengine.io.Xml) Animation(com.b3dgs.lionengine.Animation) BeforeClass(org.junit.BeforeClass)

Example 23 with Animation

use of com.b3dgs.lionengine.Animation in project lionengine by b3dgs.

the class UtilAttackable method prepare.

/**
 * Create the featurable.
 *
 * @param featurable The featurable to prepare.
 */
public static void prepare(Featurable featurable) {
    final Animator animator = new AnimatorMock();
    animator.play(new Animation("test", 1, 1, 1.0, false, false));
    featurable.addFeature(new AnimatableModel(animator));
    featurable.addFeature(new TransformableModel());
}
Also used : Animator(com.b3dgs.lionengine.Animator) AnimatableModel(com.b3dgs.lionengine.game.feature.AnimatableModel) TransformableModel(com.b3dgs.lionengine.game.feature.TransformableModel) Animation(com.b3dgs.lionengine.Animation) AnimatorMock(com.b3dgs.lionengine.core.drawable.AnimatorMock)

Example 24 with Animation

use of com.b3dgs.lionengine.Animation in project lionengine by b3dgs.

the class StateAnimationUtil method loadStates.

/**
 * Load all existing animations defined in the xml file.
 *
 * @param states The states values.
 * @param factory The factory reference.
 * @param provider The featurable reference.
 * @param setup The setup reference.
 */
public static void loadStates(StateAnimationBased[] states, StateFactory factory, FeatureProvider provider, Setup setup) {
    final AnimationConfig configAnimations = AnimationConfig.imports(setup);
    for (final StateAnimationBased type : states) {
        try {
            final Animation animation = configAnimations.getAnimation(type.getAnimationName());
            final State state = UtilReflection.create(type.getStateClass(), UtilReflection.getParamTypes(provider, animation), provider, animation);
            factory.addState(state);
        } catch (final NoSuchMethodException exception) {
            throw new LionEngineException(exception);
        }
    }
}
Also used : LionEngineException(com.b3dgs.lionengine.LionEngineException) State(com.b3dgs.lionengine.game.State) Animation(com.b3dgs.lionengine.Animation) AnimationConfig(com.b3dgs.lionengine.game.AnimationConfig)

Example 25 with Animation

use of com.b3dgs.lionengine.Animation in project lionengine by b3dgs.

the class AnimatorTest method testUpdateLoopReverse.

/**
 * Test update with loop and reverse.
 */
@Test
public void testUpdateLoopReverse() {
    final Animation animation = new Animation(Animation.DEFAULT_NAME, 1, 3, 1.0, true, true);
    final Animator animator = new AnimatorImpl();
    animator.play(animation);
    Assert.assertEquals(AnimState.PLAYING, animator.getAnimState());
    Assert.assertEquals(1, animator.getFrame());
    Assert.assertEquals(1, animator.getFrameAnim());
    animator.update(1.0);
    Assert.assertEquals(AnimState.PLAYING, animator.getAnimState());
    Assert.assertEquals(2, animator.getFrame());
    Assert.assertEquals(2, animator.getFrameAnim());
    animator.update(1.0);
    Assert.assertEquals(AnimState.PLAYING, animator.getAnimState());
    Assert.assertEquals(3, animator.getFrame());
    Assert.assertEquals(3, animator.getFrameAnim());
    animator.update(1.0);
    Assert.assertEquals(AnimState.REVERSING, animator.getAnimState());
    Assert.assertEquals(2, animator.getFrame());
    Assert.assertEquals(2, animator.getFrameAnim());
    animator.update(1.0);
    Assert.assertEquals(AnimState.REVERSING, animator.getAnimState());
    Assert.assertEquals(1, animator.getFrame());
    Assert.assertEquals(1, animator.getFrameAnim());
    animator.update(1.0);
    Assert.assertEquals(AnimState.PLAYING, animator.getAnimState());
    Assert.assertEquals(2, animator.getFrame());
    Assert.assertEquals(2, animator.getFrameAnim());
    animator.update(1.0);
    Assert.assertEquals(AnimState.PLAYING, animator.getAnimState());
    Assert.assertEquals(3, animator.getFrame());
    Assert.assertEquals(3, animator.getFrameAnim());
    animator.update(1.0);
    Assert.assertEquals(AnimState.REVERSING, animator.getAnimState());
    Assert.assertEquals(2, animator.getFrame());
    Assert.assertEquals(2, animator.getFrameAnim());
}
Also used : Animator(com.b3dgs.lionengine.Animator) Animation(com.b3dgs.lionengine.Animation) Test(org.junit.Test)

Aggregations

Animation (com.b3dgs.lionengine.Animation)41 Test (org.junit.jupiter.api.Test)19 Test (org.junit.Test)13 Animator (com.b3dgs.lionengine.Animator)9 SpriteAnimated (com.b3dgs.lionengine.graphic.SpriteAnimated)6 HashMap (java.util.HashMap)5 Xml (com.b3dgs.lionengine.Xml)4 AnimatorModel (com.b3dgs.lionengine.AnimatorModel)3 Media (com.b3dgs.lionengine.Media)3 AnimatableModel (com.b3dgs.lionengine.game.feature.AnimatableModel)3 TransformableModel (com.b3dgs.lionengine.game.feature.TransformableModel)3 Collision (com.b3dgs.lionengine.game.feature.collidable.Collision)3 Collection (java.util.Collection)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 AnimState (com.b3dgs.lionengine.AnimState)2 AnimatorListener (com.b3dgs.lionengine.AnimatorListener)2 LionEngineException (com.b3dgs.lionengine.LionEngineException)2 AnimationConfig (com.b3dgs.lionengine.game.AnimationConfig)2 Configurer (com.b3dgs.lionengine.game.Configurer)2 Animatable (com.b3dgs.lionengine.game.feature.Animatable)2