Beispiel #1
0
  /**
   * 渲染当前层画面到LGraphics之上
   *
   * @param g
   * @param x
   * @param y
   * @param sx
   * @param sy
   * @param width
   * @param ty
   * @param isLine
   * @param mapTileWidth
   * @param mapTileHeight
   */
  public void draw(
      LGraphics g,
      int x,
      int y,
      int sx,
      int sy,
      int width,
      int ty,
      boolean isLine,
      int mapTileWidth,
      int mapTileHeight) {

    int tileCount = map.getTileSetCount();

    int nx, ny, sheetX, sheetY, tileOffsetY;

    for (int tileset = 0; tileset < tileCount; tileset++) {

      TMXTileSet set = null;

      for (int tx = 0; tx < width; tx++) {

        nx = sx + tx;
        ny = sy + ty;

        if ((nx < 0) || (ny < 0)) {
          continue;
        }
        if ((nx >= this.width) || (ny >= this.height)) {
          continue;
        }

        if (data[nx][ny][0] == tileset) {
          if (set == null) {
            set = map.getTileSet(tileset);
          }

          sheetX = set.getTileX(data[nx][ny][1]);
          sheetY = set.getTileY(data[nx][ny][1]);

          tileOffsetY = set.tileHeight - mapTileHeight;

          set.tiles.draw(
              g, x + (tx * mapTileWidth), y + (ty * mapTileHeight) - tileOffsetY, sheetX, sheetY);
        }
      }

      if (isLine) {
        if (set != null) {
          set = null;
        }
        map.rendered(ty, ty + sy, index);
      }
    }
  }
Beispiel #2
0
  /**
   * 设置指定位置的瓦片ID
   *
   * @param x
   * @param y
   * @param tile
   */
  public void setTileID(int x, int y, int tile) {
    if (tile == 0) {
      data[x][y][0] = -1;
      data[x][y][1] = 0;
      data[x][y][2] = 0;
    } else {
      TMXTileSet set = map.findTileSet(tile);

      data[x][y][0] = set.index;
      data[x][y][1] = tile - set.firstGID;
      data[x][y][2] = tile;
    }
  }
Beispiel #3
0
  /**
   * 根据TMX地图描述创建一个新层
   *
   * @param map
   * @param element
   * @throws RuntimeException
   */
  public TMXLayer(TMXTiledMap map, Element element) throws RuntimeException {
    this.map = map;
    name = element.getAttribute("name");
    width = Integer.parseInt(element.getAttribute("width"));
    height = Integer.parseInt(element.getAttribute("height"));
    data = new int[width][height][3];

    // 获得当前图层属性
    Element propsElement = (Element) element.getElementsByTagName("properties").item(0);
    if (propsElement != null) {
      NodeList properties = propsElement.getElementsByTagName("property");
      if (properties != null) {
        props = new TMXProperty();
        for (int p = 0; p < properties.getLength(); p++) {
          Element propElement = (Element) properties.item(p);

          String name = propElement.getAttribute("name");
          String value = propElement.getAttribute("value");
          props.setProperty(name, value);
        }
      }
    }

    Element dataNode = (Element) element.getElementsByTagName("data").item(0);
    String encoding = dataNode.getAttribute("encoding");
    String compression = dataNode.getAttribute("compression");

    // 进行base64的压缩解码
    if ("base64".equals(encoding) && "gzip".equals(compression)) {
      try {
        Node cdata = dataNode.getFirstChild();
        char[] enc = cdata.getNodeValue().trim().toCharArray();
        byte[] dec = decodeBase64(enc);
        GZIPInputStream is = new GZIPInputStream(new ByteArrayInputStream(dec));

        for (int y = 0; y < height; y++) {
          for (int x = 0; x < width; x++) {
            int tileId = 0;
            tileId |= is.read();
            tileId |= is.read() << 8;
            tileId |= is.read() << 16;
            tileId |= is.read() << 24;

            if (tileId == 0) {
              data[x][y][0] = -1;
              data[x][y][1] = 0;
              data[x][y][2] = 0;
            } else {
              TMXTileSet set = map.findTileSet(tileId);

              if (set != null) {
                data[x][y][0] = set.index;
                data[x][y][1] = tileId - set.firstGID;
              }
              data[x][y][2] = tileId;
            }
          }
        }
      } catch (IOException e) {
        throw new RuntimeException("Unable to decode base64 !");
      }
    } else {
      throw new RuntimeException(
          "Unsupport tiled map type "
              + encoding
              + ","
              + compression
              + " only gzip base64 Support !");
    }
  }