我正在尝试复制相机视口内的一组图块(使用 Tiled 和 libGDX)。现在我有一个复制和粘贴代码:
package com.divergent.tapdown;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.maps.tiled.TiledMapTile;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
public abstract class TileMapCopier {
public static 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;
}
public static void pasteRegion(TiledMapTileLayer layer, TiledMapTile[][] region, int x, int y) {
for (int ix = x; ix < x + region.length; ix++)
for (int iy = y; iy < y + region[ix].length; iy++) {
Cell cell = layer.getCell(ix, iy);
if (cell == null) {
Gdx.app.debug(TileMapCopier.class.getSimpleName(), "pasteRegion: skipped [" + ix + ";" + iy + "]");
continue;
}
cell.setTile(region[ix - x][iy - y]);
}
}
}
这将获取图层上的所有单元格,并在我想要的时候将其粘贴到屏幕上:
public void show() {
final TiledMapTileLayer layer = ((TiledMapTileLayer) map.getLayers().get(0));
camera.position.x = layer.getWidth() * layer.getTileWidth() / 2;
camera.position.y = layer.getHeight() * layer.getTileHeight() / 2;
camera.zoom = 3;
Gdx.input.setInputProcessor(new InputAdapter() {
TiledMapTile[][] clipboard;
@Override
public boolean keyDown(int keycode) {
if(keycode == Keys.C) // copy
clipboard = TileMapCopier.copyRegion(layer, 0, 0, layer.getWidth(), layer.getHeight() / 2);
if(keycode == Keys.P) // paste
TileMapCopier.pasteRegion(layer, clipboard, 0, layer.getHeight() / 2);
return true;
}
});
}
这很棒,但这不是我想要的。而不是复制整个图层,我只想在复制时复制相机视口内的内容。然后我想将它粘贴到屏幕顶部并以一种使粘贴不明显的方式重置相机的视口。(我本质上是将屏幕的下部放在顶部以在下方生成新值)
我怎样才能做到这一点?
谢谢!