Exemplo n.º 1
0
  /**
   * Returns the specified entities, sorted in the order of the map. The first entity is the lowest
   * one, the last entity is the highest one.
   *
   * @param entities the entities to sort
   * @return the same entities, sorted as they are in the map
   */
  public List<MapEntity> getSortedEntities(List<MapEntity> entities) {

    List<MapEntity> sortedEntities = new LinkedList<MapEntity>();

    // sort the entities so that they have the same order as in the map
    for (Layer layer : Layer.values()) {

      for (MapEntity entity : allEntities[layer.getId()]) {

        if (entities.contains(entity)) {
          sortedEntities.add(entity);
        }
      }
    }

    // now sortedEntities contains all entities of the list,
    // sorted in the same order as in the map
    return sortedEntities;
  }
Exemplo n.º 2
0
  /**
   * Returns the entities located in a rectangle defined by two points.
   *
   * @param x1 x coordinate of the first point
   * @param y1 y coordinate of the first point
   * @param x2 x coordinate of the second point
   * @param y2 y coordinate of the second point
   */
  public List<MapEntity> getEntitiesInRectangle(int x1, int y1, int x2, int y2) {

    List<MapEntity> entitiesInRectangle = new LinkedList<MapEntity>();

    int x = Math.min(x1, x2);
    int width = Math.abs(x2 - x1);

    int y = Math.min(y1, y2);
    int height = Math.abs(y2 - y1);

    Rectangle rectangle = new Rectangle(x, y, width, height);

    for (Layer layer : Layer.values()) {

      for (MapEntity entity : allEntities[layer.getId()]) {
        if (rectangle.contains(entity.getPositionInMap())) {
          entitiesInRectangle.add(entity);
        }
      }
    }

    return entitiesInRectangle;
  }