0

カメラのビューポート内にある一連のタイル (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;
        }

    });
}

これは素晴らしいことですが、私が望むものではありません。レイヤー全体をコピーする代わりに、コピー時にカメラのビューポート内にあるものだけをコピーしたい. 次に、それを画面の上部に貼り付けて、その貼り付けが目立たないようにカメラのビューポートをリセットします。(私は本質的に画面の下部を取り、それを上部に配置して、その下に新しい値を生成します)

これどうやってするの?

ありがとう!

4

1 に答える 1