在Java中,创建可以放置和移动对象的2D地图

Bra*_*roy 4 java arrays multidimensional-array

我是Java的新手,我正在尝试制作一个基于2D文本的游戏,该游戏内部包含一个世界地图,类的实例可以在该世界地图上进行交互和移动。但是,一开始我已经很困惑。假设我要一张3x3的地图。然后,每个图块是多维数组中的值(例如,坐标[0] [2]上的图块)。但是,每个图块(即地图上的坐标)必须是类的实例化数组(例如,图块对象(森林,地面,沙漠)或实体(人,岩石,鸭)。至少包含一个元素(砖块对象(森林,地面,沙漠),但理论上可以包含无限多个实体)。

我在这里尝试并结合了一些答案。当前的想法是制造一个只有坐标的超类实体。创建实体(给定一些坐标)后,该对象将在该位置链接到世界地图(似乎无效)。世界地图是可以容纳实体的多维数组。

如您所见,该实体尚未链接到世界地图,我不知道为什么不这样做。在Entity.java和World.java文件中肯定是错的,但是我仍然不确定如何在世界地图上设置位置。请注意,稍后我需要设置,更改实体在地图上的位置,或从整体上删除实体(包括实体在地图上的位置)。

mat*_*att 5

我认为您的图块不应该是一个实体。我认为tile应该封装实体。

public class Tile {
    final int xPos,yPos;
    private String type;
    List<Entity> entities = new ArrayList<>();
    public Tile(int xPosition, int yPosition) {
        xPos = xPosition;
        yPos = yPosition;
    }

    public void setType(String type){
        this.type = type;
    }
    public String getType() {
        return type;
    }
    public void addEntity(Entity e){
        entities.add(e);
    }
    public void removeEntity(Entity e){
        entities.remove(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,世界级将包含实体:

public class World {
    Tile[][] coordinates;
    List<Tile> tiles = new ArrayList<>();

    // Creates an instance of a map with a user-defined size
    public World(int width, int height) {
        coordinates = new Tile[width][height];
        for(int i = 0; i<width; i++){
            for(int j = 0; j<height; j++){
                coordinates[i][j] = new Tile(i,j);
                tiles.add(coordinates[i][j]);
            }
        }
    }

    public void addEntity(Entity e){
        int x = e.getX();
        int y = e.getY();
        coordinates[x][y].addEntity(e);
    }
    public void setTileType(int x, int y, String type){
        coordinates[x][y].setType(type);
    }
    public List<Tile> getTiles(){
        return new List<>(tiles);
    }

    public List<Entity> getEntities(){
       List<Entity> entities = new ArrayList<>();
       for(Tile[] row: coordinates){
           for(Tile tile: row){
               entities.addAll(tile.entities);
           }
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您的main方法将类似于:

public static void main(String[] args) {
    // Creators
    World worldMap = new World(mapWidth, mapHeight);
    Character john = new Character("John", 0, 0);
    Character mary = new Character("Mary", 1, 4);
    worldMap.add(john);
    worldMap.add(mary);

    for (int x = 0; x < mapWidth; x++) {
        worldMap.setTileType(x, 5, "forest");
    }

    // Printing out info about character(s)
    //I think the world map should  have a list of characters.

    for (Character character : charList) {
        System.out.print(character+": " + character.getName() + "\n");
    }
    System.out.print("\n"+charList.size() + " characters in play\n\n");
    List<Tile> tileList = worldMap.getTiles(); 
    // Printing out info about tile(s)
    for (Tile tile : tileList) {
        System.out.print(tile + " type: " + tile.getType() + "\n");
    }
    System.out.print("\n"+tileList.size() + " tiles in play");
}
Run Code Online (Sandbox Code Playgroud)

有两个#addEntity()的原因是因为认为如何回答以下困境。

“我想创建一个角色john并将其添加到x,y位置的世界中。”

当然会有

Character john = new Character("john", x, y);
Run Code Online (Sandbox Code Playgroud)

然后,我们如何约翰到世界?我说,我们只是将他添加到世界上,然后让世界知道如何使用磁贴等。

world.addEntity(john);
Run Code Online (Sandbox Code Playgroud)

约翰有一个位置,世界可以找到瓷砖,并将约翰放在瓷砖上。相反,与您尝试过的相反,它将找到磁贴并直接添加john。

world.coordinates[john.getX()][john.getY()].addEntity(john);
Run Code Online (Sandbox Code Playgroud)

如您的示例所示,这存在索引超出范围异常的一些问题,您应该处理这些异常。所以问题是,您需要了解许多有关添加实体的图块的结构。取而代之的是让世界在实体上,因为那样一来它就可以处理所有警告,并将实体放置在正确的图块上。