/** * Finds all entities of a type on the map. * * @param entityType a type of entity * @return the list of the entities of this kind on the map */ public List<MapEntity> getEntitiesOfType(EntityType entityType) { List<MapEntity> list = new LinkedList<MapEntity>(); for (Layer layer : Layer.values()) { list.addAll(allEntities[layer.getId()].getEntitiesOfType(entityType)); } return list; }
/** * Brings the specified entities to the front, keeping their layer. The order of the specified * entities in the map is unchanged. * * @param entities the entities to move */ public void bringToFront(List<MapEntity> entities) { List<MapEntity> sortedEntities = getSortedEntities(entities); // bring to front each entity from sortedEntities ListIterator<MapEntity> iterator = sortedEntities.listIterator(0); while (iterator.hasNext()) { MapEntity entity = iterator.next(); Layer layer = entity.getLayer(); allEntities[layer.getId()].remove(entity); allEntities[layer.getId()].addLast(entity); } setChanged(); notifyObservers(); }
/** * Brings the specified entities to the back, keeping their layer. The order of the specified * entities in the map is unchanged. * * @param entities the entities to move */ public void bringToBack(List<MapEntity> entities) { List<MapEntity> sortedEntities = getSortedEntities(entities); // bring to back each entity from sortedEntities ListIterator<MapEntity> iterator = sortedEntities.listIterator(sortedEntities.size()); while (iterator.hasPrevious()) { MapEntity entity = iterator.previous(); Layer layer = entity.getLayer(); allEntities[layer.getId()].remove(entity); allEntities[layer.getId()].addFirst(entity); } setChanged(); notifyObservers(); }
/** * 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; }
/** * 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; }