Ejemplo n.º 1
0
  private void addTileByGlobalTileID(
      final int pGlobalTileID, final ITMXTilePropertiesListener pTMXTilePropertyListener) {
    final TMXTiledMap tmxTiledMap = this.mTMXTiledMap;

    final int tilesHorizontal = this.mTileColumns;

    final int column = this.mTilesAdded % tilesHorizontal;
    final int row = this.mTilesAdded / tilesHorizontal;

    final TMXTile[][] tmxTiles = this.mTMXTiles;

    final ITextureRegion tmxTileTextureRegion;
    if (pGlobalTileID == 0) {
      tmxTileTextureRegion = null;
    } else {
      tmxTileTextureRegion = tmxTiledMap.getTextureRegionFromGlobalTileID(pGlobalTileID);
    }
    final int tileHeight = this.mTMXTiledMap.getTileHeight();
    final int tileWidth = this.mTMXTiledMap.getTileWidth();

    if (this.mTexture == null) {
      this.mTexture = tmxTileTextureRegion.getTexture();
      super.initBlendFunction(this.mTexture);
    } else {
      if (this.mTexture != tmxTileTextureRegion.getTexture()) {
        throw new AndEngineRuntimeException(
            "All TMXTiles in a TMXLayer need to be in the same TMXTileSet.");
      }
    }
    final TMXTile tmxTile =
        new TMXTile(pGlobalTileID, column, row, tileWidth, tileHeight, tmxTileTextureRegion);
    tmxTiles[row][column] = tmxTile;

    this.setIndex(this.getSpriteBatchIndex(column, row));
    this.drawWithoutChecks(
        tmxTileTextureRegion,
        tmxTile.getTileX(),
        tmxTile.getTileY(),
        tileWidth,
        tileHeight,
        Color.WHITE_ABGR_PACKED_FLOAT);
    this
        .submit(); // TODO Doesn't need to be called here, but should rather be called in a "init"
                   // step, when parsing the XML is complete.

    if (pGlobalTileID != 0) {
      /* Notify the ITMXTilePropertiesListener if it exists. */
      if (pTMXTilePropertyListener != null) {
        final TMXProperties<TMXTileProperty> tmxTileProperties =
            tmxTiledMap.getTMXTileProperties(pGlobalTileID);
        if (tmxTileProperties != null) {
          pTMXTilePropertyListener.onTMXTileWithPropertiesCreated(
              tmxTiledMap, this, tmxTile, tmxTileProperties);
        }
      }
    }

    this.mTilesAdded++;
  }
  private void processIsometricTMXObject() {
    /*
     * Implemented by - Paul Robinson
     * Also in TMXLayer.java
     * Referenced work - athanazio - "Working with Isometric Maps"
     * http://www.athanazio.com/2008/02/21/working-with-isometric-maps/
     * http://www.athanazio.com/wp-content/uploads/2008/02/isomapjava.txt
     */
    final int tileHeight = this.mTMXTiledMap.getTileHeight();
    final int tileWidth = this.mTMXTiledMap.getTileWidth();
    final int maxColumnPixel = tileHeight * this.getTileColumns();
    final int maxRowPixel = tileHeight * this.getTileRows();
    ArrayList<TMXObject> objects = this.mTMXObjectGroup.getTMXObjects();
    for (TMXObject tmxObject : objects) {
      final int columnPixel = tmxObject.getX();
      final int rowPixel = tmxObject.getY();
      // Check if its pixel can be divided by tile height
      int columnRemainder = columnPixel % tileHeight;
      int rowRemainder = rowPixel % tileHeight;
      if (columnPixel <= 0
          || rowPixel <= 0
          || columnPixel > maxColumnPixel
          || rowPixel > maxRowPixel) {
        // We've got a tile outside the map, we won't draw it so skip it.
        continue;
      }

      if (columnRemainder != 0 || rowRemainder != 0) {
        /*
         * Since there is a remainder that means we're not on a tile
         * This means someone had held ctrl when placing the object tile.
         * Drawing this tile is currently not supported.
         */
        continue;
      }

      /*
       * Work out what row and column this object should exist on.
       * -1 is magic! This is because our row and column start at 0(Thanks java?!?)
       * So taking away 1 accounts for starting at 0;
       */
      final int column = (columnPixel / tileHeight) - 1;
      final int row = (rowPixel / tileHeight) - 1;
      final int width = this.mTMXTiledMap.getTileColumns();
      final ITextureRegion tmxTileTextureRegion;
      final int pGlobalTileID = tmxObject.getGID();
      // If -1 it means its not a tile object, just a normal object
      if (pGlobalTileID == 0) {
        tmxTileTextureRegion = null;
      } else if (pGlobalTileID == -1) {
        Log.w(TAG, "Global Tile ID is -1 this means it is not a tile object that can be draw");
        continue;
      } else if (tmxObject.getPolygonPoints() != null) {
        Log.w(TAG, "This object has polygon points, cannot draw these");
        continue;
      } else if (tmxObject.getPolylinePoints() != null) {
        Log.w(TAG, "This object has polyline points, cannot draw these");
        continue;
      } else {
        tmxTileTextureRegion = this.mTMXTiledMap.getTextureRegionFromGlobalTileID(pGlobalTileID);
      }

      if (tmxTileTextureRegion != null) {
        // Unless this is a transparent tile, setup the texture
        if (this.mTexture == null) {
          this.mTexture = tmxTileTextureRegion.getTexture();
          super.initBlendFunction(this.mTexture);
        } else {
          if (this.mTexture != tmxTileTextureRegion.getTexture()) {
            throw new AndEngineRuntimeException(
                "All TMXTiles in a TMXObjetTileLayer ("
                    + this.getName()
                    + ") need to be in the same TMXTileSet.");
          }
        }
      }
      /*
       * Since objects layers aren't structured like normal layers,
       * we have to work out the z index based on the tile row and column
       */
      final int pZindex = (row * width) + column;
      TMXTile tmxTile =
          new TMXTile(
              this.mTMXTiledMap.getOrientation(),
              pGlobalTileID,
              pZindex,
              column,
              row,
              tileWidth,
              tileHeight,
              tmxTileTextureRegion);
      tmxTile.setTMXObject(tmxObject);

      // Get the offset for the tileset and the tileset size
      /*
       * element[0] is the X offset.
       * element[1] is the Y offset.
       * element[2] is the tile width.
       * element[3] is the tile height.
       */
      int[] offset_tilesize = {0, 0, tileWidth, tileHeight};
      offset_tilesize = this.mTMXTiledMap.checkTileSetOffsetAndSize(pGlobalTileID);

      float xRealIsoPos = (column * this.mIsoHalfTileWidth);
      xRealIsoPos = xRealIsoPos - (row * this.mIsoHalfTileWidth);
      float yRealIsoPos = (column * this.mIsoHalfTileHeight);
      yRealIsoPos = yRealIsoPos + (row * this.mIsoHalfTileHeight);

      /*
       * We need to apply the offset different here, in TMXLayer we wanted
       * to drop the offset down, here we need to bring it up
       * the X offset shouldn't really matter
       */
      float xOffsetPos = xRealIsoPos - Math.abs(offset_tilesize[0]);
      float yOffsetPos = yRealIsoPos - offset_tilesize[1];

      tmxTile.setTileXIso(xOffsetPos);
      tmxTile.setTileYIso(yOffsetPos);
      /*
       * Suppose we could skip this
       * If you want to touch this object then click on the tile space it
       * occupies
      int xCentre = xRealIsoPos + this.mIsoHalfTileWidth;
      int yCentre = yRealIsoPos + this.mIsoHalfTileHeight;
      tmxTile.setTileXIsoCentre(xCentre);
      tmxTile.setTileYIsoCentre(yCentre);
       */
      this.mTMXTiles[row][column] = tmxTile;
      this.count++;
      if (pGlobalTileID != 0) {
        this.setIndex(this.getSpriteBatchIndex(column, row));
        // Before we were drawing to the map tile size, not the tileset size
        this.drawWithoutChecks(
            tmxTileTextureRegion,
            tmxTile.getTileX(),
            tmxTile.getTileY(),
            offset_tilesize[2],
            offset_tilesize[3],
            Color.WHITE_ABGR_PACKED_FLOAT);
        this
            .submit(); // TODO Doesn't need to be called here, but should rather be called in a
                       // "init" step, when parsing the XML is complete.
        // Notify the ITMXTilePropertiesListener if it exists.
        /*
        Not supporting this, would require a slight change or passing null for TMXLayer argument.
        if(pTMXTilePropertyListener != null) {
        	final TMXProperties<TMXTileProperty> tmxTileProperties = this.mTMXTiledMap.getTMXTileProperties(pGlobalTileID);
        	if(tmxTileProperties != null) {
        		pTMXTilePropertyListener.onTMXTileWithPropertiesCreated(this.mTMXTiledMap, this, tmxTile, tmxTileProperties);
        		//Log.i(TAG, "tmxTileProperties created, size " + tmxTileProperties.size());
        	}
        }
         */
      }
    }
  }