/** * 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; }
/** * Sets the entities of the map. * * @param allEntities an array of 3 MapEntity sets: a set for each layer (this array is copied) */ public void setAllEntities(MapEntities[] allEntities) { for (Layer layer : Layer.values()) { this.allEntities[layer.getId()] = new MapEntities(allEntities[layer.getId()]); } setChanged(); notifyObservers(); }
/** Initializes the object. */ private void initialize() { this.allEntities = new MapEntities[3]; for (Layer layer : Layer.values()) { this.allEntities[layer.getId()] = new MapEntities(); } this.entitySelection = new MapEntitySelection(this); this.history = new MapEditorHistory(); }
/** * Returns an entity, specifying its type and its name. * * @param type the type of entity * @param name the name of the entity * @return the entity, or null if there is no entity with this name */ public MapEntity getEntityWithName(EntityType type, String name) { for (Layer layer : Layer.values()) { MapEntity entity = allEntities[layer.getId()].getEntityWithName(type, name); if (entity != null) { return entity; } } return null; }
/** * Returns the total number of active entities of the map. * * @return the total number of active entities of the map. */ public int getNbDynamicEntities() { int nbDynamicEntities = 0; // count the dynamic entities of each layer for (Layer layer : Layer.values()) { nbDynamicEntities += allEntities[layer.getId()].getNbDynamicEntities(); } return nbDynamicEntities; }
/** * Returns the total number of entities of the map. * * @return the total number of entities of the map. */ public int getNbEntities() { int nbEntities = 0; // count the entities of each layer for (Layer layer : Layer.values()) { nbEntities += allEntities[layer.getId()].size(); } return nbEntities; }
/** * Changes the tileset of the map. * * @param tilesetId id of the new tileset, or an empty string to set no tileset * @return true if the tileset was loaded successfuly, false if some tiles could not be loaded in * this tileset * @throws MapException if this tileset could be applied */ public boolean setTileset(String tilesetId) throws ZSDXException { this.badTiles = false; // if the tileset is removed if (tilesetId.length() == 0 && this.tilesetId.length() != 0) { this.tilesetId = tilesetId; this.tileset = null; setChanged(); notifyObservers(); } // if the tileset is changed else if (!tilesetId.equals(this.tilesetId)) { this.tileset = new Tileset(tilesetId); for (Layer layer : Layer.values()) { LinkedList<MapEntity> entitiesToRemove = new LinkedList<MapEntity>(); for (MapEntity entity : allEntities[layer.getId()]) { try { entity.setTileset(tileset); } catch (NoSuchTilePatternException ex) { // the entity is not valid anymore, we should remove it from the map entitiesToRemove.add(entity); badTiles = true; } } for (MapEntity entity : entitiesToRemove) { allEntities[layer.getId()].remove(entity); } } this.tilesetId = tilesetId; setChanged(); notifyObservers(tileset); } return !badTiles; }
/** * 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; }
/** * Saves the map into its file. * * @throws ZSDXException if the file could not be written for various reasons */ public void save() throws ZSDXException { // check that the map is valid checkValidity(); try { // open the map file File mapFile = Project.getMapFile(mapId); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(mapFile))); // print the map general info // syntax: width height world floor x y small_keys_variable tileset_id music_id out.print(size.width); out.print('\t'); out.print(size.height); out.print('\t'); out.print(world); out.print('\t'); out.print(floor); out.print('\t'); out.print(location.x); out.print('\t'); out.print(location.y); out.print('\t'); out.print(smallKeysVariable); out.print('\t'); out.print(tilesetId); out.print('\t'); out.print(musicId); out.println(); for (Layer layer : Layer.values()) { MapEntities entities = allEntities[layer.getId()]; // print the entities for (MapEntity entity : entities) { out.print(entity.toString()); out.println(); } } out.close(); history.setSaved(); // also update the map name in the global resource list Resource mapResource = Project.getResource(ResourceType.MAP); mapResource.setElementName(mapId, name); Project.getResourceDatabase().save(); // upate the dungeon elements of this map if (isInDungeon()) { Dungeon.saveMapInfo(this); } // create a script for the map if necessary File scriptFile = Project.getMapScriptFile(mapId); if (!scriptFile.exists()) { scriptFile.createNewFile(); } } catch (IOException ex) { throw new MapException(ex.getMessage()); } }