我现在有这个代码:
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;
}
返回此结果:
我想要的是光滑,圆形的岛屿,随处可见石块沉积物。 perlin的噪音是否会过度杀伤?我想我可以生成一个图块,检查它旁边的图块ID,然后如果相邻的图块是相同类型,则将其放下。但这将创造无尽的相同瓷砖。有什么帮助吗?
答案 0 :(得分:1)
我要采取的第一步是在域级别的任何内容中创建对象:
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.
}
现在我们的代码将生成一个包含所有随机存款的岛屿。剩下要做的唯一事情就是放置这些岛屿。我将保持简单,只添加一个到地图,但我相信你可以弄清楚如何添加多个(我会留下一些评论与我的想法):
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),因为我不知道你是如何将图块绘制到屏幕上的。)
// 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.
所以基本上就是这样 - 这不是太糟糕。
你总是可以更进一步,添加主要是水但有一些海岸线的瓷砖。为此,您必须检查您正在绘制的图块是边缘还是边角:
+-+-+-+
|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都是沿海瓷砖。
我希望这有点帮助。