Search in sources :

Example 1 with Range

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

the class DamagesTest method testConstructor.

/**
 * Test constructor.
 */
@Test
void testConstructor() {
    final Damages damages = new Damages();
    assertEquals(0, damages.getMin());
    assertEquals(0, damages.getMax());
    assertEquals(0, damages.getLast());
    final Range range = damages.getDamages();
    assertEquals(0, range.getMin());
    assertEquals(0, range.getMax());
    for (int i = 0; i < 100; i++) {
        assertEquals(0, damages.getRandom());
    }
}
Also used : Range(com.b3dgs.lionengine.Range) Test(org.junit.jupiter.api.Test)

Example 2 with Range

use of com.b3dgs.lionengine.Range 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 3 with Range

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

the class AttackerModelTest method testStopAttack.

/**
 * Test the stop attack.
 */
@Test
void testStopAttack() {
    canAttack.set(true);
    attacker.setAttackChecker(t -> canAttack.get());
    attacker.setAttackDistance(new Range(0, 2));
    final Transformable target = new TransformableModel(services, setup);
    target.teleport(1, 1);
    attacker.attack(target);
    attacker.update(1.0);
    attacker.update(1.0);
    assertTrue(attacker.isAttacking());
    attacker.stopAttack();
    assertTrue(attacker.isAttacking());
    attacker.update(1.0);
    assertFalse(attacker.isAttacking());
}
Also used : TransformableModel(com.b3dgs.lionengine.game.feature.TransformableModel) UtilTransformable(com.b3dgs.lionengine.game.feature.UtilTransformable) Transformable(com.b3dgs.lionengine.game.feature.Transformable) Range(com.b3dgs.lionengine.Range) Test(org.junit.jupiter.api.Test)

Example 4 with Range

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

the class UtilAttackable method createAttacker.

/**
 * Create an attacker.
 *
 * @param object The object.
 * @param services The services.
 * @param setup The setup reference.
 * @return The attacker.
 */
public static AttackerModel createAttacker(Featurable object, Services services, Setup setup) {
    final AttackerModel attacker = new AttackerModel(services, setup);
    attacker.recycle();
    attacker.setAttackDamages(new Range(1, 2));
    attacker.setAttackDistance(new Range(1, 2));
    attacker.setAttackFrame(1);
    attacker.setAttackDelay(2);
    attacker.prepare(object);
    return attacker;
}
Also used : Range(com.b3dgs.lionengine.Range)

Example 5 with Range

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

the class DamagesTest method testConstructorParam.

/**
 * Test constructor with parameters.
 */
@Test
void testConstructorParam() {
    final Damages damages = new Damages(1, 2);
    assertEquals(1, damages.getMin());
    assertEquals(2, damages.getMax());
    assertEquals(0, damages.getLast());
    final Range range = damages.getDamages();
    assertEquals(1, range.getMin());
    assertEquals(2, range.getMax());
    for (int i = 0; i < 100; i++) {
        final int damage = damages.getRandom();
        assertTrue(UtilMath.isBetween(damage, 1, 2));
    }
}
Also used : Range(com.b3dgs.lionengine.Range) Test(org.junit.jupiter.api.Test)

Aggregations

Range (com.b3dgs.lionengine.Range)6 Test (org.junit.jupiter.api.Test)5 Transformable (com.b3dgs.lionengine.game.feature.Transformable)2 UtilTransformable (com.b3dgs.lionengine.game.feature.UtilTransformable)2 Animation (com.b3dgs.lionengine.Animation)1 Animatable (com.b3dgs.lionengine.game.feature.Animatable)1 TransformableModel (com.b3dgs.lionengine.game.feature.TransformableModel)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1