Esempio n. 1
0
  /**
   * 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();
  }
Esempio n. 2
0
  /**
   * Changes the layer of an entity. You should call this method instead of calling directly
   * MapEntity.setLayer(), because the entity of the 3 layers are stored in 3 different structures.
   * If the entity is not known by the map (yet), this method just calls MapEntity.setLayer().
   *
   * @param entity the entity to change the layer
   * @param layer the new layer
   */
  public void setEntityLayer(MapEntity entity, Layer layer) {

    Layer oldLayer = entity.getLayer();

    if (layer != oldLayer) {
      entity.setLayer(layer);

      if (allEntities[oldLayer.getId()].remove(entity)) {
        allEntities[layer.getId()].add(entity);
      }

      setChanged();
      notifyObservers();
    }
  }
Esempio n. 3
0
  /**
   * 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;
  }
Esempio n. 4
0
  /**
   * 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;
  }
Esempio n. 5
0
  /** 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();
  }
Esempio n. 6
0
  /**
   * 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;
  }
Esempio n. 7
0
  /**
   * 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;
  }
Esempio n. 8
0
  /**
   * 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;
  }
Esempio n. 9
0
  /**
   * 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;
  }
Esempio n. 10
0
  /**
   * Returns the first entity under a point of the map, in the specified layer.
   *
   * @param layer the layer
   * @param x x of the point
   * @param y y of the point
   * @return the entity found, or null if there is no entity here
   */
  public MapEntity getEntityAt(Layer layer, int x, int y) {

    MapEntities entities = allEntities[layer.getId()];
    ListIterator<MapEntity> iterator = entities.listIterator(entities.size());
    while (iterator.hasPrevious()) {

      MapEntity entity = iterator.previous();
      if (entity.containsPoint(x, y)) {
        return entity;
      }
    }

    return null;
  }
Esempio n. 11
0
  /**
   * 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();
  }
Esempio n. 12
0
  /**
   * 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();
  }
Esempio n. 13
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;
  }
Esempio n. 14
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;
  }
Esempio n. 15
0
 /**
  * Returns the entities of the map on a given layer.
  *
  * @param layer the layer
  * @return the entities placed on that layer
  */
 public MapEntities getEntities(Layer layer) {
   return allEntities[layer.getId()];
 }
Esempio n. 16
0
  /**
   * 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());
    }
  }