gpt4 book ai didi

java - libGDX - 如何在相机视口(viewport)中复制图 block 而不是按层复制?

转载 作者:行者123 更新时间:2023-12-01 12:57:30 26 4
gpt4 key购买 nike

我正在尝试复制相机视口(viewport)内的一组图 block (使用 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;
}

});
}

这很棒,但这不是我想要的。我不想复制整个图层,只想复制复制时相机视口(viewport)内的内容。然后,我想将其粘贴到屏幕顶部,并以一种使粘贴不明显的方式重置相机的视口(viewport)。 (我本质上是将屏幕的下部放在顶部以在下面生成新值)

我该怎么做?

谢谢!

最佳答案

您可以浏览所有图 block ,如果它们位于相机视口(viewport)的范围内,则将其添加到某个数组中(或者只是对它们执行某些操作)。您可以这样检查边界:

for(int i = 0; i < allTiles.size; i++) {
Tile t = allTiles.get(i);
MapProperties props = t.getProperties();
float x = (float) props.get("x");
float y = (float) props.get("y");
if(x > camera.position.x && x < camera.position.x + camera.vieportWidth
y > camera.position.y && y < camera.position.y + camera.vieportHeight)
//do something with the tile, because it IS inside the camera sight
}

关于java - libGDX - 如何在相机视口(viewport)中复制图 block 而不是按层复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23761204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com