Exemplo n.º 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;
  }
Exemplo n.º 2
0
  /**
   * Creates a mob and adds it to the list of entities, but only if it doesn't intersect with
   * another character.
   *
   * @param x the initial x coordinate
   * @param y the initial y coordinate
   * @param health the initial health of the mob
   * @param textureSet the texture set to use
   * @param speed how fast the mob moves in pixels per second
   * @return true if the mob was successfully added, false if there was an intersection and the mob
   *     wasn't added
   */
  public boolean createMob(
      Mob mob, float x, float y, int health, TextureSet textureSet, int speed) {

    // 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;
      }
    }

    entities.add(mob);
    return true;
  }