/** * Returns the highest layer where a specified rectangle overlaps an existing entity. * * @param rectangle a rectangle * @return the highest layer where an entity exists in this rectangle, or Layer.LOW if there is * nothing here */ public Layer getLayerInRectangle(Rectangle rectangle) { Layer[] layers = {Layer.HIGH, Layer.INTERMEDIATE, Layer.LOW}; for (Layer layer : layers) { for (MapEntity entity : allEntities[layer.getId()]) { if (rectangle.intersects(entity.getPositionInMap())) { return layer; } } } return Layer.LOW; }
/** * 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; }