1

所以现在我正在复制相机视口内的图块,并使用以下函数将它们粘贴到 TiledMap 的顶部:

    public TiledMapTile[][] copyRegion(TiledMapTileLayer layer, int x, int y, int width, int height) {
    TiledMapTile[][] region = new TiledMapTile[width][height];

    for (int ix = x; ix < x + width; ix++)
        for (int iy = y; iy < y + height; iy++) {
            Cell cell = layer.getCell(ix, iy);
            if(cell == null)
                continue;

            region[ix - x][iy - y] = cell.getTile();

        }

    return region;
}

我的问题是这个双 for 循环,它搜索视口的每个 x 和 y 值,这是数千个循环,这显然是我的问题。在此之前,我将宽度和高度设置为 TiledMapTileLayer 的 layer.getTileHeight 和 layer.getTileWidth 并且运行顺利。这是因为层 x 和 y 的值是由 Tiles 决定的,所以它实际上只检查了大约 100 个切片,这要快得多。我将如何将 2“合并”在一起以仅复制视口内的图块,而不是视口内的 x 和 y 值?

我使用以下方法调用此函数:

clipboard = TileMapCopier.copyRegion(layer, 0, 0, (int) camera.viewportWidth, (int) camera.viewportHeight);
TileMapCopier.pasteRegion(layer, clipboard, 0, layer.getHeight() / 2);

谢谢!

编辑:有人告诉我查看正交平铺地图渲染器如何基于视口进行渲染。我真的不知道该怎么做:P 我还是新手。

    @Override
public void renderTileLayer (TiledMapTileLayer layer) {
    final Color batchColor = spriteBatch.getColor();
    final float color = Color.toFloatBits(batchColor.r, batchColor.g, batchColor.b, batchColor.a * layer.getOpacity());

    final int layerWidth = layer.getWidth();
    final int layerHeight = layer.getHeight();

    final float layerTileWidth = layer.getTileWidth() * unitScale;
    final float layerTileHeight = layer.getTileHeight() * unitScale;

    final int col1 = Math.max(0, (int)(viewBounds.x / layerTileWidth));
    final int col2 = Math.min(layerWidth, (int)((viewBounds.x + viewBounds.width + layerTileWidth) / layerTileWidth));

    final int row1 = Math.max(0, (int)(viewBounds.y / layerTileHeight));
    final int row2 = Math.min(layerHeight, (int)((viewBounds.y + viewBounds.height + layerTileHeight) / layerTileHeight));

    float y = row1 * layerTileHeight;
    float xStart = col1 * layerTileWidth;
    final float[] vertices = this.vertices;

    for (int row = row1; row < row2; row++) {
        float x = xStart;
        for (int col = col1; col < col2; col++) {
            final TiledMapTileLayer.Cell cell = layer.getCell(col, row);
            if (cell == null) {
                x += layerTileWidth;
                continue;
            }
            final TiledMapTile tile = cell.getTile();

            if (tile != null) {
                final boolean flipX = cell.getFlipHorizontally();
                final boolean flipY = cell.getFlipVertically();
                final int rotations = cell.getRotation();

                TextureRegion region = tile.getTextureRegion();

                float x1 = x;
                float y1 = y;
                float x2 = x1 + region.getRegionWidth() * unitScale;
                float y2 = y1 + region.getRegionHeight() * unitScale;

                float u1 = region.getU();
                float v1 = region.getV2();
                float u2 = region.getU2();
                float v2 = region.getV();

                vertices[X1] = x1;
                vertices[Y1] = y1;
                vertices[C1] = color;
                vertices[U1] = u1;
                vertices[V1] = v1;

                vertices[X2] = x1;
                vertices[Y2] = y2;
                vertices[C2] = color;
                vertices[U2] = u1;
                vertices[V2] = v2;

                vertices[X3] = x2;
                vertices[Y3] = y2;
                vertices[C3] = color;
                vertices[U3] = u2;
                vertices[V3] = v2;

                vertices[X4] = x2;
                vertices[Y4] = y1;
                vertices[C4] = color;
                vertices[U4] = u2;
                vertices[V4] = v1;

                if (flipX) {
                    float temp = vertices[U1];
                    vertices[U1] = vertices[U3];
                    vertices[U3] = temp;
                    temp = vertices[U2];
                    vertices[U2] = vertices[U4];
                    vertices[U4] = temp;
                }
                if (flipY) {
                    float temp = vertices[V1];
                    vertices[V1] = vertices[V3];
                    vertices[V3] = temp;
                    temp = vertices[V2];
                    vertices[V2] = vertices[V4];
                    vertices[V4] = temp;
                }
                if (rotations != 0) {
                    switch (rotations) {
                    case Cell.ROTATE_90: {
                        float tempV = vertices[V1];
                        vertices[V1] = vertices[V2];
                        vertices[V2] = vertices[V3];
                        vertices[V3] = vertices[V4];
                        vertices[V4] = tempV;

                        float tempU = vertices[U1];
                        vertices[U1] = vertices[U2];
                        vertices[U2] = vertices[U3];
                        vertices[U3] = vertices[U4];
                        vertices[U4] = tempU;
                        break;
                    }
                    case Cell.ROTATE_180: {
                        float tempU = vertices[U1];
                        vertices[U1] = vertices[U3];
                        vertices[U3] = tempU;
                        tempU = vertices[U2];
                        vertices[U2] = vertices[U4];
                        vertices[U4] = tempU;
                        float tempV = vertices[V1];
                        vertices[V1] = vertices[V3];
                        vertices[V3] = tempV;
                        tempV = vertices[V2];
                        vertices[V2] = vertices[V4];
                        vertices[V4] = tempV;
                        break;
                    }
                    case Cell.ROTATE_270: {
                        float tempV = vertices[V1];
                        vertices[V1] = vertices[V4];
                        vertices[V4] = vertices[V3];
                        vertices[V3] = vertices[V2];
                        vertices[V2] = tempV;

                        float tempU = vertices[U1];
                        vertices[U1] = vertices[U4];
                        vertices[U4] = vertices[U3];
                        vertices[U3] = vertices[U2];
                        vertices[U2] = tempU;
                        break;
                    }
                    }
                }
                spriteBatch.draw(region.getTexture(), vertices, 0, 20);
            }
            x += layerTileWidth;
        }
        y += layerTileHeight;
    }
}
}
4

0 回答 0