Exemple #1
0
  public Level(String filename, PixelPie pie) {
    XML file = pie.app.loadXML(filename);

    // Get level properties, if any.
    if (file.getChild("properties") != null) {
      properties = new StringDict();
      for (XML child : file.getChild("properties").getChildren("property")) {
        properties.set(child.getString("name"), child.getString("value"));
      }
    }

    // Get tile dimensions.
    tileWidth = file.getInt("tilewidth");
    tileHeight = file.getInt("tileheight");

    // Get Row and Column count.
    levelColumns = file.getInt("width");
    levelRows = file.getInt("height");

    // Get level dimensions.
    levelWidth = levelColumns * tileWidth;
    levelHeight = levelRows * tileHeight;

    // Get tileSets.
    tileSets = file.getChildren("tileset");

    // Get objects.
    objects = file.getChildren("objectgroup");

    // Get scripts.
    scripts = new HashMap<String, Script>();
    for (XML objectType : objects) {
      String objectName = objectType.getString("name");

      // Check type of object.
      if (objectName.substring(0, 1).equals("(")) {

        // Break up name into parts.
        String[] nameParts = PApplet.split(objectName, " ");

        // If script.
        if (nameParts[0].equals("(script)")) {

          // Grab script name.
          String name = nameParts[1];

          // Initialize coordinates array (if any).
          IntDict scriptCoords = new IntDict();

          // Check for objects in script layer. Get coordinates from these objects.
          if (objectType.getChild("object") != null) {
            for (XML coordObj : objectType.getChildren("object")) {

              // Check if object has a name, if not, skip it.
              if (coordObj.getString("name") == null) {
                pie.log.printlg("Script coordinate container does not have a name. Ignoring.");
                continue;
              }

              // Figure out object type.
              int coordType = 0;
              if (coordObj.hasChildren()) {
                coordType = (coordObj.getChild("polyline") != null) ? 2 : 1;
              }

              // Process coordinates depending on object type.
              switch (coordType) {

                  // If rectangle, take top-left corner.
                case 0:
                  scriptCoords.set(
                      coordObj.getString("name"),
                      PixelPie.coordToArray(coordObj.getInt("x"), coordObj.getInt("y"), this));
                  break;

                  // If ellipse, take center.
                case 1:
                  scriptCoords.set(
                      coordObj.getString("name"),
                      PixelPie.coordToArray(
                          coordObj.getInt("x") + coordObj.getInt("width") / 2,
                          coordObj.getInt("y") + coordObj.getInt("height") / 2,
                          this));
                  break;

                  // If poly line, record each point.
                case 2:
                  String[] coordsArray =
                      PApplet.split(coordObj.getChild("polyline").getString("points"), " ");
                  int startX = Integer.parseInt(objectType.getChild("object").getString("x"));
                  int startY = Integer.parseInt(objectType.getChild("object").getString("y"));
                  for (int i = 0; i < coordsArray.length; i++) {
                    scriptCoords.set(
                        coordObj.getString("name") + "_" + PApplet.nf(i, 2),
                        PixelPie.coordToArray(
                            startX + Integer.parseInt(PApplet.split(coordsArray[i], ",")[0]),
                            startY + Integer.parseInt(PApplet.split(coordsArray[i], ",")[1]),
                            this));
                  }
                  break;
              }
            }
          } else {
            scriptCoords = null;
          }

          // Get the total amount of frames.
          int endFrame = 0;
          for (XML obj : objectType.getChild("properties").getChildren("property")) {
            endFrame = PApplet.max(endFrame, PixelPie.getScriptFrame(obj.getString("name")));
          }

          // Create the scriptAction container "children".
          HashMap<String, ScriptAction[]> children = new HashMap<String, ScriptAction[]>();

          // Create the script object.
          Script Script = new Script(endFrame, scriptCoords, children, pie);
          scripts.put(name, Script);

          // Create scriptAction temporary container. This one uses a HashMap so we can dynamically
          // add scriptActions to it.
          HashMap<String, ArrayList<ScriptAction>> scriptTemp =
              new HashMap<String, ArrayList<ScriptAction>>();

          // Add scriptActions to the temporary container.
          for (XML obj : objectType.getChild("properties").getChildren("property")) {
            String frame = PApplet.str(PixelPie.getScriptFrame(obj.getString("name")));

            // Create scriptAction object from parameters.
            String[] actionArray = PApplet.split(obj.getString("value"), "(");
            String params = actionArray[1].substring(0, actionArray[1].length() - 1);
            ScriptAction action = null;
            // try {action = (scriptAction) Class.forName(app.getClass().getName() + "$script_" +
            // actionArray[0]).getDeclaredConstructors()[0].newInstance(new Object[]{app, params,
            // Script});}
            try {
              action =
                  (ScriptAction)
                      Class.forName(pie.app.getClass().getName() + "$script_" + actionArray[0])
                          .getDeclaredConstructors()[0]
                          .newInstance(new Object[] {pie.app});
              action.setup(params, Script);
            } catch (Exception e) {
              pie.log.printlg(e);
            }

            // Check whether there is an entry in the scriptTemp array.
            if (scriptTemp.get(frame) != null) {
              scriptTemp.get(frame).add(action);
            } else {
              // Initiate Arraylist.
              scriptTemp.put(frame, new ArrayList<ScriptAction>());
              scriptTemp.get(frame).add(action);
            }
          }

          // Turn over contents of temporary container to "children" array.
          for (@SuppressWarnings("rawtypes") Map.Entry entry : scriptTemp.entrySet()) {
            ScriptAction[] tempActions = new ScriptAction[scriptTemp.get(entry.getKey()).size()];
            for (int i = 0; i < scriptTemp.get(entry.getKey()).size(); i++) {
              tempActions[i] = scriptTemp.get(entry.getKey()).get(i);
            }
            children.put(entry.getKey().toString(), tempActions);
          }
        }
      }
    }

    // Load level layers.
    XML[] layers = file.getChildren("layer");

    // Count number of background and level layers.
    for (int i = 0; i < layers.length; i++) {
      if (layers[i].getString("name").substring(0, 4).equals("(bg)")) {
        backgroundLayers++;
      } else {
        levelLayers++;
      }
    }

    // Initiate arrays.
    background = new int[backgroundLayers][levelColumns * levelRows];
    levelMap = new int[levelLayers][levelColumns * levelRows];
    bgScroll = new float[backgroundLayers];

    // Process each layer.
    int currentLayer = 0;
    int bgCurrentLayer = 0;
    for (int i = 0; i < layers.length; i++) {

      // If it's a background layer...
      if (layers[i].getString("name").substring(0, 4).equals("(bg)")) {

        // Get properties.
        HashMap<String, String> prop = new HashMap<String, String>();
        for (XML property : layers[i].getChild("properties").getChildren("property")) {
          prop.put(property.getString("name"), property.getString("value"));
        }

        // Process the scroll rate.
        bgScroll[bgCurrentLayer] =
            PApplet.constrain(Float.parseFloat(PixelPie.getProp(prop, "scroll")), 0, 1);

        // Process each background tile.
        XML[] tiles = layers[i].getChild("data").getChildren("tile");
        for (int k = 0; k < tiles.length; k++) {
          background[bgCurrentLayer][k] = tiles[k].getInt("gid");
        }

        // Increase background layer counter.
        bgCurrentLayer++;
      }

      // Else, load layer as normal.
      else {
        // Load each tile.
        XML[] tiles = layers[i].getChild("data").getChildren("tile");
        for (int k = 0; k < tiles.length; k++) {
          levelMap[currentLayer][k] = tiles[k].getInt("gid");
        }
        currentLayer++;
      }
    }
  }
  private Library(File folder, String groupName) {
    super(folder);
    this.group = groupName;

    libraryFolder = new File(folder, "library");
    examplesFolder = new File(folder, "examples");
    referenceFile = new File(folder, "reference/index.html");

    File exportSettings = new File(libraryFolder, "export.txt");
    StringDict exportTable =
        exportSettings.exists() ? Util.readSettings(exportSettings) : new StringDict();

    exportList = new HashMap<String, String[]>();

    // get the list of files just in the library root
    String[] baseList = libraryFolder.list(standardFilter);
    //    System.out.println("Loading " + name + "...");
    //    PApplet.println(baseList);

    String appletExportStr = exportTable.get("applet");
    if (appletExportStr != null) {
      appletExportList = PApplet.splitTokens(appletExportStr, ", ");
    } else {
      appletExportList = baseList;
    }

    String androidExportStr = exportTable.get("android");
    if (androidExportStr != null) {
      androidExportList = PApplet.splitTokens(androidExportStr, ", ");
    } else {
      androidExportList = baseList;
    }

    // for the host platform, need to figure out what's available
    File nativeLibraryFolder = libraryFolder;
    String hostPlatform = Platform.getName();
    //    System.out.println("1 native lib folder now " + nativeLibraryFolder);
    // see if there's a 'windows', 'macosx', or 'linux' folder
    File hostLibrary = new File(libraryFolder, hostPlatform);
    if (hostLibrary.exists()) {
      nativeLibraryFolder = hostLibrary;
    }
    //    System.out.println("2 native lib folder now " + nativeLibraryFolder);
    // check for bit-specific version, e.g. on windows, check if there
    // is a window32 or windows64 folder (on windows)
    hostLibrary = new File(libraryFolder, hostPlatform + Platform.getNativeBits());
    if (hostLibrary.exists()) {
      nativeLibraryFolder = hostLibrary;
    }
    //    System.out.println("3 native lib folder now " + nativeLibraryFolder);

    if (hostPlatform.equals("linux") && System.getProperty("os.arch").equals("arm")) {
      hostLibrary = new File(libraryFolder, "linux-armv6hf");
      if (hostLibrary.exists()) {
        nativeLibraryFolder = hostLibrary;
      }
    }

    // save that folder for later use
    nativeLibraryPath = nativeLibraryFolder.getAbsolutePath();

    // for each individual platform that this library supports, figure out what's around
    for (int i = 1; i < platformNames.length; i++) {
      String platformName = platformNames[i];
      String platformName32 = platformName + "32";
      String platformName64 = platformName + "64";
      String platformNameArmv6hh = platformName + "-armv6hf";

      // First check for things like 'application.macosx=' or 'application.windows32' in the
      // export.txt file.
      // These will override anything in the platform-specific subfolders.
      String platformAll = exportTable.get("application." + platformName);
      String[] platformList = platformAll == null ? null : PApplet.splitTokens(platformAll, ", ");
      String platform32 = exportTable.get("application." + platformName + "32");
      String[] platformList32 = platform32 == null ? null : PApplet.splitTokens(platform32, ", ");
      String platform64 = exportTable.get("application." + platformName + "64");
      String[] platformList64 = platform64 == null ? null : PApplet.splitTokens(platform64, ", ");
      String platformArmv6hf = exportTable.get("application." + platformName + "-armv6hf");
      String[] platformListArmv6hf =
          platformArmv6hf == null ? null : PApplet.splitTokens(platformArmv6hf, ", ");

      // If nothing specified in the export.txt entries, look for the platform-specific folders.
      if (platformAll == null) {
        platformList = listPlatformEntries(libraryFolder, platformName, baseList);
      }
      if (platform32 == null) {
        platformList32 = listPlatformEntries(libraryFolder, platformName32, baseList);
      }
      if (platform64 == null) {
        platformList64 = listPlatformEntries(libraryFolder, platformName64, baseList);
      }
      if (platformListArmv6hf == null) {
        platformListArmv6hf = listPlatformEntries(libraryFolder, platformNameArmv6hh, baseList);
      }

      if (platformList32 != null || platformList64 != null || platformListArmv6hf != null) {
        multipleArch[i] = true;
      }

      // if there aren't any relevant imports specified or in their own folders,
      // then use the baseList (root of the library folder) as the default.
      if (platformList == null
          && platformList32 == null
          && platformList64 == null
          && platformListArmv6hf == null) {
        exportList.put(platformName, baseList);

      } else {
        // once we've figured out which side our bread is buttered on, save it.
        // (also concatenate the list of files in the root folder as well
        if (platformList != null) {
          exportList.put(platformName, platformList);
        }
        if (platformList32 != null) {
          exportList.put(platformName32, platformList32);
        }
        if (platformList64 != null) {
          exportList.put(platformName64, platformList64);
        }
        if (platformListArmv6hf != null) {
          exportList.put(platformNameArmv6hh, platformListArmv6hf);
        }
      }
    }
    //    for (String p : exportList.keySet()) {
    //      System.out.println(p + " -> ");
    //      PApplet.println(exportList.get(p));
    //    }

    // get the path for all .jar files in this code folder
    packageList = Util.packageListFromClassPath(getClassPath());
  }