gpt4 book ai didi

java - 二维高架瓦片生成

转载 作者:行者123 更新时间:2023-11-30 11:37:50 25 4
gpt4 key购买 nike

我现在有这段代码:

private void generateLevel() {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
tiles[x + y * width] = random.nextInt(4);
}
}
}

允许此方法工作的原因:

public Tile getTile(int x, int y) {
if (x < 0 || y < 0 || x >= width || y >= height) return Tile.voidTile;
if (tiles[x + y * width] == 0) return Tile.grass;
if (tiles[x + y * width] == 1) return Tile.stone;
else return Tile.water;
//return Tile.voidTile;
}

返回这个结果: 2D Tile Generation

我想要的是光滑、圆形的岛屿,到处都是随机的石头沉积物。 Perlin 噪声是否会为此过度杀伤力?我想我可以只生成一个图 block ,检查它旁边的图 block ID,如果相邻的图 block 属于同一类型,则将其放下。但这会创造出无穷无尽的同一 block 瓷砖。有帮助吗?

最佳答案

我要采取的第一步是从您域级别的任何内容创建对象:

public class Island {
private Point2D center = null;
private int radius = 0;

private List<Deposit> deposits = new ArrayList<Deposit>();

public Island(Point2D center, int radius) {
this.center = center;
this.radius = radius;
}

public void generateDeposits(int numDeposits) {
for (int i = 0; i < numDeposits; i++) {
// TODO: I leave it to you to find an x and y inside the island's
// boundary.
int x = getIntInsideCircle(center, radius);
int y = getIntInsideCircle(center, radius);
if (!depositInLocation(x, y)) {
deposits.add(new StoneDeposit(x, y));
} else {
i--; // TODO: This code could potentially go on forever,
// if we keep generating locations that have been used,
// but I'll leave this for you to handle.
}
}
}
}

public abstract class Deposit {
private Point2D location = null;

public Deposit(Point2D location) {
this.location = location;
}
}

public class StoneDeposit extends Deposit {
// TODO: You can fill this with StoneDeposit specifics.
}

现在我们有了代码,可以生成一个带有所有随机存款的岛屿。唯一剩下要做的就是实际放置这些岛屿。我将保持简单,只向 map 添加一个,但我相信您可以弄清楚如何添加多个(我会留下一些评论和我的想法):

public class Map {
private final int WIDTH = 1000;
private final int HEIGHT = 1000;

private List<Island> islands = new ArrayList<Island>();

public void generate() {
// TODO: If you want to make more, make a for loop.
int radius = 100;
Island island = new Island(new Point2D(WIDTH / 2, HEIGHT / 2), radius);
// TODO: If you are going to add more, then you can't simply add them
// all willy-nilly. You are going to have to check if the islands collide
// and, if they do, find a way to handle that.
// You could let them collide and create a mountain range where they do, or,
// you could try to place the next island in a different position (similar
// to what we used above placing deposits, but both situations require
// code a bit better than what I've included).
islands.add(island);
}
}

好的,现在我们已经获得了所需的所有数据。这将我们带到了使用磁贴实际将其绘制到屏幕上的最后一点。我对这个主题不太熟悉,所以这可能效率低下,但它应该作为一个起点。

我概括了一些函数(例如 drawTile(int x, int y, TileType type),因为我不知道您是如何将图 block 绘制到屏幕上的)。

// Generate our data.
Map map = new Map();
map.generate();
// Draw to the screen.

// 1. Fill the entire screen with water.
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
drawTile(x, y, Type.WATER);
}
}

// 2. Draw the islands.
// We're going to use this algorithm to draw the circle:
// http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
for (Island island : map.getIslands()) {
int f = 1 - island.getRadius();
int ddF_x = 1;
int ddF_y = -2 * island.getRadius();
int x = 0;
int y = 0;

Point2D center = island.getCenter();
int radius = island.getRadius();
drawTile(center.getX(), center.getY() + radius, TileType.LAND);
drawTile(center.getX(), center.getY() - radius, TileType.LAND);
drawTile(center.getX() + radius, center.getY(), TileType.LAND);
drawTile(center.getX() - radius, center.getY(), TileType.LAND);

while(x < y) {
if(f >= 0) {
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
drawTile(center.getX() + x, center.getY() + y, TileType.LAND);
drawTile(center.getX() - x, center.getY() + y, TileType.LAND);
drawTile(center.getX() + x, center.getY() - y, TileType.LAND);
drawTile(center.getX() - x, center.getY() - y, TileType.LAND);
drawTile(center.getX() + y, center.getY() + x, TileType.LAND);
drawTile(center.getX() - y, center.getY() + x, TileType.LAND);
drawTile(center.getX() + y, center.getY() - x, TileType.LAND);
drawTile(center.getX() - y, center.getY() - x, TileType.LAND);
}

// TODO: Now you have to figure out how to fill in the guts of the island.
}

// 3. Draw the deposits.
// TODO: I'll leave this one for you.

基本上就是这样 - 还不错。

您总是可以更进一步,添加大部分是水但有一些海岸线的瓷砖。为此,您必须检查您正在绘制的图 block 是边还是角:

+-+-+-+
|1|2|3|
+-+-+-+
+-+-+-+-+
|4|5|6|7|
+-+-+-+-+
+-+-+-+
|8|9|0|
+-+-+-+

这里你可以看到2,9,4都是边缘瓦片。 1、3、8、0都是角砖,5是内砖。当您将一个瓷砖识别为角时,您必须选择所有附着的水瓷砖并将它们绘制为“海岸”瓷砖:

  +-+-+
| |x|
+-+-+-+-+
|x|1|2|3|
+-+-+-+-+
+-+-+-+-+
|4|5|6|7|
+-+-+-+-+
+-+-+-+
|8|9|0|
+-+-+-+

所有的 x 都是沿海板 block 。

希望对您有所帮助。

关于java - 二维高架瓦片生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13897951/

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