/** @return a byte array with the vector tile */ public byte[] encode() { VectorTile.Tile tile = new VectorTile.Tile(); List<VectorTile.Tile.Layer> tileLayers = new ArrayList<VectorTile.Tile.Layer>(); for (Map.Entry<String, Layer> e : layers.entrySet()) { String layerName = e.getKey(); Layer layer = e.getValue(); VectorTile.Tile.Layer tileLayer = new VectorTile.Tile.Layer(); tileLayer.version = 1; tileLayer.name = layerName; tileLayer.keys = layer.keys(); List<VectorTile.Tile.Value> values = new ArrayList<VectorTile.Tile.Value>(); for (Object value : layer.values()) { VectorTile.Tile.Value tileValue = new VectorTile.Tile.Value(); if (value instanceof String) { tileValue.setStringValue((String) value); } else if (value instanceof Integer) { tileValue.setSintValue(((Integer) value).intValue()); } else if (value instanceof Long) { tileValue.setSintValue(((Long) value).longValue()); } else if (value instanceof Float) { tileValue.setFloatValue(((Float) value).floatValue()); } else if (value instanceof Double) { tileValue.setDoubleValue(((Double) value).doubleValue()); } else { tileValue.setStringValue(value.toString()); } values.add(tileValue); } tileLayer.values = values.toArray(new VectorTile.Tile.Value[values.size()]); tileLayer.setExtent(extent); List<VectorTile.Tile.Feature> features = new ArrayList<VectorTile.Tile.Feature>(); for (Feature feature : layer.features) { Geometry geometry = feature.geometry; VectorTile.Tile.Feature featureBuilder = new VectorTile.Tile.Feature(); featureBuilder.tags = toIntArray(feature.tags); featureBuilder.setType(toGeomType(geometry)); featureBuilder.geometry = toIntArray(commands(geometry)); features.add(featureBuilder); } tileLayer.features = features.toArray(new VectorTile.Tile.Feature[features.size()]); tileLayers.add(tileLayer); } tile.layers = tileLayers.toArray(new VectorTile.Tile.Layer[tileLayers.size()]); return MessageNano.toByteArray(tile); }
/** * 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; }
private void setFlags() { if (!npc.isSpawned()) return; SkinnableEntity skinnable = NMS.getSkinnable(npc.getEntity()); if (skinnable == null) return; int flags = 0xFF; for (Layer layer : Layer.values()) { if (!isVisible(layer)) { flags &= ~layer.flag; } } skinnable.setSkinFlags((byte) flags); }
/** * 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()); } }