コード例 #1
0
  private static void addMachineFromString(String pMachineString, Factory pFactory)
      throws Exception {
    String[] type = pMachineString.split("=");
    if (type[0].equals("Processor")) {
      String[] infos = type[1].split(":");
      String[] subinfos1 = infos[0].split(",");
      String[] subinfos2 = infos[1].split("x");
      String[] subinfos3 = infos[3].split(",");
      List<BlockShape> shapes = new ArrayList<BlockShape>();
      for (String shape : subinfos3) shapes.add(BlockShape.valueOf(shape));

      pFactory.addProcessor(
          Integer.parseInt(subinfos1[0]),
          Integer.parseInt(subinfos1[1]),
          Integer.parseInt(subinfos2[0]),
          Integer.parseInt(subinfos2[1]),
          shapes,
          BlockShape.valueOf(infos[4]),
          BlockColor.valueOf(infos[2]));
    } else if (type[0].equals("Router")) {
      String[] infos = type[1].split(",");
      pFactory.addRouter(Integer.parseInt(infos[0]), Integer.parseInt(infos[1]), 0);
    } else if (type[0].equals("Pipe")) {
      String[] infos = type[1].split(":");
      String[] subinfos = infos[0].split(",");
      pFactory.addPipe(
          Integer.parseInt(subinfos[0]),
          Integer.parseInt(subinfos[1]),
          Integer.parseInt(infos[1]),
          Integer.parseInt(infos[2]));
    }
  }
コード例 #2
0
  public static SavingHelper readWorldFromFile(int pLevel) {
    SavingHelper state = new SavingHelper();

    FileReader inFile = null;
    BufferedReader in = null;
    try {
      File file = new File("saves/level" + pLevel + ".ini");
      file.getParentFile().mkdirs();
      inFile = new FileReader(file);
      in = new BufferedReader(inFile);

      String line = in.readLine(), currentType = "";
      while (line != null) {
        if (line.startsWith("[") && line.endsWith("]")) currentType = line;
        else {
          if (currentType.equals("[WorldInfo]")) {
            String[] rd = line.split("=");
            if (rd[0].equals("Money")) state.money = Integer.parseInt(rd[1]);
            else if (rd[0].equals("Level")) state.level = Integer.parseInt(rd[1]);
            else if (rd[0].equals("FactoryInfo")) {
              String[] rdsub = rd[1].split(",");
              state.factory =
                  new FactoryImpl(
                      Integer.parseInt(rdsub[0]),
                      Integer.parseInt(rdsub[1]),
                      Integer.parseInt(rdsub[2]),
                      Integer.parseInt(rdsub[3]));
            }
          } else if (currentType.equals("[Factory]")) addMachineFromString(line, state.factory);
          else if (currentType.equals("[BuildMenu]")) {
            ProcessorImpl mach = new ProcessorImpl();
            String[] rd = line.split("=")[1].split(":");
            String[] rdsub = rd[0].split(",");
            mach.setTileX(Integer.parseInt(rdsub[0]));
            mach.setTileY(Integer.parseInt(rdsub[1]));
            rdsub = rd[1].split("x");
            mach.setTileWidth(Integer.parseInt(rdsub[0]));
            mach.setTileHeight(Integer.parseInt(rdsub[1]));
            mach.setColor(BlockColor.valueOf(rd[2]));
            rdsub = rd[3].split(",");
            for (String shape : rdsub) mach.getShapeIns().add(BlockShape.valueOf(shape));
            mach.setShapeOut(BlockShape.valueOf(rd[4]));
            mach.setCost(mach.getCostFromSize(mach.getTileWidth(), mach.getTileHeight()));
            mach.setImage(
                new BlockImage(
                    BlockImage.getImage(
                        "Processor_" + mach.getTileWidth() + "x" + mach.getTileHeight() + ".png")));
            mach.setForeGround(
                new BlockImage(
                    BlockImage.getImage(
                        "ProcessorForeground_"
                            + mach.getTileWidth()
                            + "x"
                            + mach.getTileHeight()
                            + ".png")));

            state.buildMenu.add(mach);
          }
        }
        line = in.readLine();
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {
      try {
        if (in != null) in.close();
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }

    return state;
  }