为塔防游戏创建随机路径的问题

时间:2015-12-24 19:14:03

标签: java

我很难弄清楚如何创建一个随机生成的实体路径,使用下面我写过的一组类。

我知道这是一个非常具体的问题,人们可以通过多种方式以编程方式创建适合游戏的解决方案。

所以,说到这里,我可以给你关于我想要创造什么类型的游戏的背景。

GameType:塔防

如果您希望看到我自己尝试解决方案,请参阅以下内容:

generateWorld():

    public void generateWorld() {
        String worldCode = Util.generateCode(8);
        TinyDebug.debug("World", "Generating [gameWorld] w/ [worldCode]: " + worldCode);

        worldWidth = new Random().nextInt(256);
        worldHeight = new Random().nextInt(256);
        xSpawn = new Random().nextInt(10);
        ySpawn = xSpawn;

        /*
        TinyDebug.debug("worldWidth", worldWidth);
        TinyDebug.debug("worldHeight", worldHeight);
        TinyDebug.debug("xSpawn", xSpawn);
        TinyDebug.debug("ySpawn", ySpawn);
        */

        worldMap = new int[worldWidth][worldHeight];
        for (int xPos = 0; xPos < worldWidth; xPos++) {
            for (int yPos = 0; yPos < worldHeight; yPos++) {
                int randomValue = new Random().nextInt(2);
                switch (randomValue) {
                case 0:
                    worldMap[xPos][yPos] = Tile.grassTile.getID();
                    break;
                case 1:
                    worldMap[xPos][yPos] = Tile.dirtTile.getID();
                    break;
                case 2:
                    worldMap[xPos][yPos] = Tile.stoneTile.getID();
                    break;
                default:
                    worldMap[xPos][yPos] = Tile.grassTile.getID();
                    break;
                }
            }
        }

        TinyDebug.debug("World", "[gameWorld] w/ [worldCode]: " +  worldCode + " has been generated successfully.");
    }

正如您所看到的,这个当前代码只是随机地放置了由于生成的随机#而导致的瓦片。

但是,我的问题更加具体和复杂。我不知道如何创建实体流经的路径。请注意,路径是随机生成的。

我知道有更简单的方法可以做到这一点,例如从文件加载,但如果我能找到如何自动执行它会觉得很有趣。

World.java:

    public class World {
    private Game game;

    private int worldWidth, worldHeight, xSpawn, ySpawn;
    private int[][] worldMap;

    public World(Game game) { 
        this.game = game;
    }

    public void generateWorld() {
        //Method used to generate a random world. 
    }

    public void loadWorld(String filePath, String fileName, String fileExtension) {
        String worldFile = TinyFile.loadAppendedFile(filePath + fileName + fileExtension);
        String[] tokens = worldFile.split("\\s+");

        worldWidth = Integer.parseInt(tokens[0]);
        worldHeight = Integer.parseInt(tokens[1]);
        xSpawn = Integer.parseInt(tokens[2]);
        ySpawn = Integer.parseInt(tokens[3]);

        TinyDebug.debug("worldWidth", worldWidth);
        TinyDebug.debug("worldHeight", worldHeight);
        TinyDebug.debug("xSpawn", xSpawn);
        TinyDebug.debug("ySpawn", ySpawn);

        worldMap = new int[worldWidth][worldHeight];
        for (int xPos = 0; xPos < worldWidth; xPos++) {
            for (int yPos = 0; yPos < worldHeight; yPos++) {
                worldMap[xPos][yPos] = Integer.parseInt(tokens[(xPos + yPos * worldWidth) + 4]);
            }
        }
    }

    public void update() {}

    public void render(Graphics g) {
        for (int xPos = 0; xPos < worldWidth; xPos++) {
            for (int yPos = 0; yPos < worldHeight; yPos++) {
                getTile(xPos, yPos).render(g, xPos * Tile.TILEWIDTH, yPos * Tile.TILEHEIGHT);
            }
        }
    }

    private Tile getTile(int xPos, int yPos) {
        return Tile.tilesArray[worldMap[xPos][yPos]];
    }

    public Game getGame() {
        return game;
    }

    public int getxSpawn() {
        return xSpawn;
    }

    public int getySpawn() {
        return ySpawn;
    }
}

Tile.java:

    public class Tile {
    public static Tile[] tilesArray = new Tile[256];
    public static Tile grassTile = new GrassTile(0);
    public static Tile dirtTile = new DirtTile(1);
    public static Tile stoneTile = new StoneTile(2);

    public static final int TILEWIDTH = 64;
    public static final int TILEHEIGHT = 64;

    protected BufferedImage tileTexture;
    protected final int id;

    public Tile(BufferedImage tileTexture, int id) {
        this.tileTexture = tileTexture;
        this.id = id;

        tilesArray[id] = this;
    }

    public void update() {}

    public void render(Graphics g, int xPos, int yPos) {
        g.drawImage(tileTexture, xPos, yPos, TILEWIDTH, TILEHEIGHT, null);
    }

    public boolean isSolid() {
        return false;
    }

    public int getID() {
        return id;
    }
}

GrassTile.java:

public class GrassTile extends Tile {
    public GrassTile(int id) {
        super(Library.grassTile, id);
    }
}

0 个答案:

没有答案