Ejemplo n.º 1
0
  /**
   * Converts the given CLUE game data XML Tags.
   *
   * @param gameData CLUE game data to convert
   * @return the root XML Tag
   */
  private static XMLTag convertToXML(ClueGameData gameData) {
    XMLTag rootTag = new XMLTag();
    XMLTag clueGameTag = new XMLTag("ClueGame");

    XMLTag roomsTag = new XMLTag("Rooms");
    roomsTag.addAttribute(
        "displayRoomPicturesOnBoard", Boolean.toString(gameData.getDisplayRoomPicturesOnBoard()));
    for (Room room : gameData.getRooms()) {
      XMLTag roomTag = new XMLTag("Room");
      roomTag.addAttribute("id", Integer.toString(room.getId()));
      roomTag.addAttribute("name", room.getName());
      String pictureName = room.getPictureName();
      if (pictureName != null && !pictureName.isEmpty())
        roomTag.addAttribute("pictureName", pictureName);
      if (room.getTransparentPictureColor() != null)
        roomTag.addAttribute(
            "transparentPictureColor",
            Integer.toString(room.getTransparentPictureColor().getRGB()));
      roomsTag.addSubTag(roomTag);
    }
    clueGameTag.addSubTag(roomsTag);

    XMLTag suspectsTag = new XMLTag("Suspects");
    for (Suspect suspect : gameData.getSuspects()) {
      XMLTag suspectTag = new XMLTag("Suspect");
      suspectTag.addAttribute("id", Integer.toString(suspect.getId()));
      suspectTag.addAttribute("name", suspect.getName());
      suspectTag.addAttribute("color", Integer.toString(suspect.getColor().getRGB()));
      String pictureName = suspect.getPictureName();
      if (pictureName != null && !pictureName.isEmpty())
        suspectTag.addAttribute("pictureName", pictureName);
      suspectsTag.addSubTag(suspectTag);
    }
    clueGameTag.addSubTag(suspectsTag);

    XMLTag weaponsTag = new XMLTag("Weapons");
    for (Weapon weapon : gameData.getWeapons()) {
      XMLTag weaponTag = new XMLTag("Weapon");
      weaponTag.addAttribute("id", Integer.toString(weapon.getId()));
      weaponTag.addAttribute("name", weapon.getName());
      String pictureName = weapon.getPictureName();
      if (pictureName != null && !pictureName.isEmpty())
        weaponTag.addAttribute("pictureName", pictureName);
      weaponsTag.addSubTag(weaponTag);
    }
    clueGameTag.addSubTag(weaponsTag);

    Board board = gameData.getBoard();
    XMLTag boardTag = new XMLTag("ClueBoard");
    boardTag.addAttribute("height", Integer.toString(board.getHeight()));
    boardTag.addAttribute("width", Integer.toString(board.getWidth()));
    if (gameData.getBackgroundImageFilename() != null)
      boardTag.addAttribute("backgroundImageFilename", gameData.getBackgroundImageFilename());
    boardTag.addAttribute(
        "backgroundColor", Integer.toString(gameData.getBackgroundColor().getRGB()));
    for (int row = 0; row < board.getHeight(); row++) {
      for (int col = 0; col < board.getWidth(); col++) {
        DisplayTile tile = board.getTile(row, col);
        if (tile.isFreeTile()) continue;

        XMLTag tileTag = new XMLTag("Tile");
        tileTag.addAttribute("row", Integer.toString(row));
        tileTag.addAttribute("col", Integer.toString(col));

        if (tile.isRoomTile())
          tileTag.addAttribute("roomId", Integer.toString(tile.getRoom().getId()));
        for (DisplayTile.Direction direction : DisplayTile.Direction.values()) {
          if (tile.hasDoor(direction)) {
            XMLTag doorTag = new XMLTag("Door");
            doorTag.addContent(direction.toString());
            tileTag.addSubTag(doorTag);
          }
        }

        if (tile.isRemovedTile()) tileTag.addAttribute("removed", "true");
        else if (tile.isPassage()) {
          XMLTag passageConnectionTag = new XMLTag("PassageConnection");
          Board.TilePosition tilePosition = board.getTilePosition(tile.getPassageConnection());
          passageConnectionTag.addAttribute("row", Integer.toString(tilePosition.row));
          passageConnectionTag.addAttribute("col", Integer.toString(tilePosition.col));
          tileTag.addSubTag(passageConnectionTag);
        } else if (tile.hasSuspect()) {
          tileTag.addAttribute("suspectId", Integer.toString(tile.getSuspect().getId()));
          tileTag.addAttribute("suspectDirection", tile.getSuspectDirection().toString());
        }

        boardTag.addSubTag(tileTag);
      }
    }
    clueGameTag.addSubTag(boardTag);

    XMLTag storyTag = new XMLTag("Story");
    storyTag.addContent(gameData.getStory());
    clueGameTag.addSubTag(storyTag);

    rootTag.addSubTag(clueGameTag);
    return rootTag;
  }