Ejemplo n.º 1
0
  /**
   * Loads a level.
   *
   * @param levelNumber Number of the wanted level.
   * @param resources Resource object for images
   * @return Level object
   * @throws ProtectedContentException
   * @throws IOException
   */
  public static Level load(int levelNumber, Resources resources)
      throws ProtectedContentException, IOException {
    final Image image = Levels.getImage(levelNumber);
    final int w = image.getWidth();
    final int h = image.getHeight();
    final int[] raw = new int[w * h];
    image.getRGB(raw, 0, w, 0, 0, w, h);
    byte[][] level = new byte[w][h];
    Vector enemySpawnPointsVector = new Vector();
    Point playerSpawnPoint = null;
    Point base = null;
    Vector treePointsVector = new Vector();
    byte value;
    int x, y;
    Hashtable unknownColors = new Hashtable();

    // add the level elements set in level image
    for (int i = 0; i < raw.length; i++) {
      value = 0;
      x = i % w;
      y = i / w;
      switch (raw[i] & 0x00F0F0F0) {
        case BRICK_COLOR:
          value = BRICK_WALL;
          break;
        case STEEL_COLOR:
          value = STEEL_WALL;
          break;
        case WATER_COLOR:
          value = WATER;
          break;
        case TREE_COLOR:
          treePointsVector.addElement(new Point(x, y));
          break;
        case ENEMY_SPAWN_POINT_COLOR:
          enemySpawnPointsVector.addElement(new Point(x, y));
          break;
        case PLAYER_SPAWN_POINT_COLOR:
          playerSpawnPoint = new Point(x, y);
          break;
        case BASE_COLOR:
          base = new Point(x, y);
          break;
        default:
          if ((raw[i] & 0x00FFFFFF) != 0x00FFFFFF) {
            Integer key = new Integer(raw[i] & 0x00FFFFFF);
            unknownColors.put(key, key);
          }
      }
      level[x][y] = value;
    }
    Point[] enemySpawnPoints = new Point[enemySpawnPointsVector.size()];
    enemySpawnPointsVector.copyInto(enemySpawnPoints);
    Point[] trees = new Point[treePointsVector.size()];
    treePointsVector.copyInto(trees);
    StringBuffer debug = new StringBuffer();
    Enumeration e = unknownColors.keys();
    while (e.hasMoreElements()) {
      debug.append(Integer.toHexString(((Integer) e.nextElement()).intValue()));
      debug.append(", ");
    }
    if (enemySpawnPoints.length == 0) {
      throw new IllegalArgumentException("no enemy spawn points " + debug.toString());
    }
    if (playerSpawnPoint == null) {
      throw new IllegalArgumentException("no player spawn point " + debug.toString());
    }
    if (base == null) {
      throw new IllegalArgumentException("no base " + debug.toString());
    }
    return new Level(level, enemySpawnPoints, playerSpawnPoint, base, trees, resources);
  }