使用文本文件加载基于图块的地图(2D Java 引擎)标准库

Loading tile-based maps using text files (2D Java Engine) Standard Libraries

所以我可以使用文本文件使用图块加载 2D 地图,这非常棒。但是,我使用此方法遇到的一个问题是我无法将 objects/actors 添加到我的地图中,因为该文件是二维网格。 (该游戏类似于 zelda 和 pokemon 等游戏。)我尝试创建一个对象层以便我可以重叠图像,但它似乎对我不起作用。举一个我想要的例子,让诸如树木之类的对象成为实体并位于背景草地上。 如果您想向我提出一些想法,我也在寻找更好的方法来创建这些基于图块的地图。

**注意:我大约 beginner/intermediate 在 Java。

这是调用地图的 GameState class 的构造函数。

public GameState(Game game) {
    super(game);
    player = new Player(game, 0, 0, 64, 64);
    map = new Map(game, "res/saves/save1.txt");
}

这是地图 class(有效),它也调用对象(第二)层。

private int width, height;
public static int spawnX, spawnY;
private int[][] mapTiles;
MapObjects mapObjects;
Game game;

public Map(Game game, String path) {
    this.game = game;
    mapObjects = new MapObjects(game, "res/saves/save1_obj.txt", width, height);
    loadMap(path);
}

private void loadMap(String path) {
    String file = Utils.loadFileAsString(path);
    //Token is which number it is out of the total
    String[] tokens = file.split("\s+");
    //Sets what is what
    width = Utils.parseInt(tokens[0]);
    height = Utils.parseInt(tokens[1]);
    spawnX = Utils.parseInt(tokens[2]);
    spawnY = Utils.parseInt(tokens[3]);

    mapTiles = new int[width][height];
    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {
            // (x+y*width) : calculates the nth token (+4) : The 4 prior tokens before the graph
            mapTiles[x][y] = Utils.parseInt(tokens[(x + y *width) + 4]);
        }
    }

}

public void render(Graphics g) {
    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {
            //Only renders what is seen.
            getMapTile(x, y).render(g, (int)(x*Tile.TILE_WIDTH-game.getCamera().getxOffset()), (int)(y*Tile.TILE_HEIGHT-game.getCamera().getyOffset()));
        }
    }
}

public void tick() {

}

//Gets the specific tile at specific coordinates.
private Tile getMapTile(int x, int y) {
    Tile t = Tile.tiles[mapTiles[x][y]];
    if(t == null) {
        return Tile.grassTile;
    }
    return t;
}

最后,对象层不起作用。它不会给出错误,只是重叠的对象不可见。我已经确保在地图层之前加载对象层,但这似乎不是问题。

private int width, height;
private int[][] objTiles;
Game game;

public MapObjects(Game game, String path, int width, int height) {
    this.game = game;
    loadObjects(path, width, height);
}

public void loadObjects(String path, int width, int height) {
    this.width = width;
    this.height = height;

    String file = Utils.loadFileAsString(path);
    String[] tokens = file.split("\s+");

    objTiles = new int[width][height];
    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {
            objTiles[x][y] = Utils.parseInt(tokens[(x + y *width)]);
        }
    }
}

public void render(Graphics g) {
    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {
            //Only renders what is seen.
            getObjTile(x, y).render(g, (int)(x*Tile.TILE_WIDTH-game.getCamera().getxOffset()), (int)(y*Tile.TILE_HEIGHT-game.getCamera().getyOffset()));
        }
    }
}

public void tick() {

}

//Gets the specific object tile at specific coordinates.
private Tile getObjTile(int x, int y) {
    Tile t = Tile.tiles[objTiles[x][y]];
    if(t == null) {
        return Tile.nothingTile;
    }
    return t;
}

我们可能需要您提供更多信息。

您使用与绘制对象不同的 "container/component" 来绘制地图图块吗?因为如果您先渲染对象,那么它们会在您渲染地图后立即消失。你应该先画地图,然后再做对象:

public Map(Game game, String path) {
    this.game = game;
    //swapped the order of the lines below so the map loads first:
    loadMap(path);
    mapObjects = new MapObjects(game, "res/saves/save1_obj.txt", width, height);
}

根据您所说,这也不起作用,但是如果您使用相同的组件来绘制地图和对象,那么一个组件将始终覆盖另一个组件,并且总会丢失某些内容。要解决此问题,您需要折叠两个单独的窗格,一个用于地图,另一个位于地图顶部的透明窗格可用于渲染对象。

以这张图为例:

您基本上需要添加一个新的透明 "content plane" 类似于上面显示的玻璃窗格的方式。