Example #1
0
  /**
   * Spawns a mob somewhere on the map. Ensures it doesn't intersect anything and is on a spawn tile
   *
   * @param mob
   * @return
   */
  public boolean spawnMob(Mob mob) {
    float x = mob.getX();
    float y = mob.getY();
    TextureSet textureSet = mob.getWalkingTextureSet();

    // Check mob isn't out of bounds.
    if (x < 0
        || x > getMapWidth() - textureSet.getWidth()
        || y > getMapHeight() - textureSet.getHeight()) {
      return false;
    }

    // Check mob doesn't intersect anything.
    for (Entity entity : entities) {
      if (entity instanceof Character
          && (mob.intersects(entity.getX(), entity.getY(), entity.getWidth(), entity.getHeight())
              || mob.collidesX(0)
              || mob.collidesY(0))) {
        return false;
      }
    }

    if (getSpawnLayer().getCell((int) x / getTileWidth(), (int) y / getTileHeight()) == null) {
      return false;
    }

    entities.add(mob);
    return true;
  }
  @Test
  public void projectileMovementTest() {

    // simple movement tests
    // x direction test
    Mob testMob = new Mob(testRound, 300, 300, 5, Assets.badGuyNormal, 100);
    Projectile testProjectile =
        new Projectile(
            testRound,
            testMob.getX() + 100,
            testMob.getY() + 100,
            testMob.getX() + 100,
            testMob.getY() + 100,
            10,
            10,
            testMob);
    int[] expectedCoord = new int[] {400, 400};
    int[] actualCoord = new int[] {(int) testProjectile.getX(), (int) testProjectile.getY()};
    assertArrayEquals(expectedCoord, actualCoord);
    testProjectile.update(1);
    expectedCoord = new int[] {410, 400};
    actualCoord = new int[] {(int) testProjectile.getX(), (int) testProjectile.getY()};
    assertArrayEquals(expectedCoord, actualCoord);

    // y direction test
    testProjectile =
        new Projectile(
            testRound,
            testMob.getX(),
            testMob.getY(),
            testMob.getX(),
            testMob.getY() + 100,
            10,
            10,
            testMob);
    testProjectile.update(1);
    expectedCoord = new int[] {300, 310};
    actualCoord = new int[] {(int) testProjectile.getX(), (int) testProjectile.getY()};
    assertArrayEquals(expectedCoord, actualCoord);
  }
  @Test
  public void projectileCollisionTest() {
    Mob testMob = new Mob(testRound, 300, 300, 5, Assets.badGuyNormal, 100);
    Projectile testProjectile =
        new Projectile(
            testRound,
            testMob.getX() + 100,
            testMob.getY() + 100,
            testMob.getX() + 100,
            testMob.getY() + 100,
            10,
            10,
            testMob);
    testRound.addEntity(testMob);

    // collision detection tests
    // move the mob to some boundary of the map
    testMob.setVelocity(-1, 0);
    for (int i = 0; i < 100; i++) {
      testMob.update((float) 0.1);
    }

    // the mob should be stuck colliding against something
    testProjectile =
        new Projectile(
            testRound,
            testMob.getX(),
            testMob.getY() + 100,
            testMob.getX(),
            testMob.getY(),
            100,
            100,
            null);
    // fire projectile at mob and check it is removed
    testProjectile.update(1);
    assertEquals(true, testProjectile.isRemoved());
  }