Пример #1
0
  /**
   * Loads the map from its file.
   *
   * @throws ZSDXException if the file could not be read
   */
  public void load() throws ZSDXException {

    int lineNumber = 0;
    try {

      // get the map name in the game resource database
      Resource mapResource = Project.getResource(ResourceType.MAP);
      setName(mapResource.getElementName(mapId));

      File mapFile = Project.getMapFile(mapId);
      BufferedReader in = new BufferedReader(new FileReader(mapFile));

      // read the map general info
      // syntax: width height world floor x y small_keys_variable tileset_id music_id
      String line = in.readLine();

      if (line == null) {
        throw new ZSDXException("The map file is empty");
      }

      lineNumber++;
      StringTokenizer tokenizer = new StringTokenizer(line);

      int width = Integer.parseInt(tokenizer.nextToken());
      int height = Integer.parseInt(tokenizer.nextToken());
      setSize(new Dimension(width, height));

      setWorld(Integer.parseInt(tokenizer.nextToken()));
      setFloor(Integer.parseInt(tokenizer.nextToken()));

      int x = Integer.parseInt(tokenizer.nextToken());
      int y = Integer.parseInt(tokenizer.nextToken());
      setLocation(new Point(x, y));

      setSmallKeysVariable(Integer.parseInt(tokenizer.nextToken()));
      setTileset(tokenizer.nextToken());
      setMusic(tokenizer.nextToken());

      // read the map entities
      line = in.readLine();
      while (line != null) {
        lineNumber++;

        try {
          MapEntity entity = MapEntity.createFromString(this, line);
          addEntity(entity);
        } catch (NoSuchTilePatternException ex) {
          badTiles = true;
        }
        line = in.readLine();
      }

      in.close();

      history.setSaved();
    } catch (NumberFormatException ex) {
      throw new ZSDXException("Line " + lineNumber + ": Integer expected");
    } catch (IndexOutOfBoundsException ex) {
      throw new ZSDXException("Line " + lineNumber + ": Value expected");
    } catch (ZSDXException ex) {
      throw new ZSDXException("Line " + lineNumber + ": " + ex.getMessage());
    } catch (IOException ex) {
      throw new ZSDXException(ex.getMessage());
    }

    setChanged();
    notifyObservers();
  }
Пример #2
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());
    }
  }