I have this code right now:
private void generateLevel() {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
tiles[x + y * width] = random.nextInt(4);
}
}
}
Which allows this method to work:
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;
}
Which returns this result:
What I want is smooth, rounded islands with random stone deposits here and there. Would perlin noise be overkill for this? I figured I could just generate a tile, check the tile id next to it and then place it down if the tile adjacent is of the same type. But that would create endless expanses of the same tile. Any help?
The first step I would take is to create objects out of anything on your domain level:
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.
}
Now we have code that will generate an island with all it's random deposits. The only thing that is left to do is actually place these islands. I'm going to keep it simple and only add one to the map but I'm sure you can figure out how to add more than one (I'll leave some comments with my ideas):
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);
}
}
Ok, now we've got all the data we need. This brings us to the final point of actually drawing it onto the screen using tiles. I'm not too experienced with this subject, so this might be inefficient, but it should serve as a launching point.
Some of the functions I've generalized (like drawTile(int x, int y, TileType type) because I don't know how you are drawing the tiles to the screen).
// 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.
So that's basically it - it's not too bad.
You could always go a step further and add tiles that are mostly water but with some shore line. In order to do that you would have to check if the tile you are drawing is an edge or a corner:
+-+-+-+
|1|2|3|
+-+-+-+
+-+-+-+-+
|4|5|6|7|
+-+-+-+-+
+-+-+-+
|8|9|0|
+-+-+-+
Here you can see 2,9, and 4 are all edge tiles. 1, 3, 8, 0 are all corner tiles, and 5 is an interior tile. When you recognize a tile as being a corner then you have to pick all the attached water tiles and draw them as "coast" tiles:
+-+-+
| |x|
+-+-+-+-+
|x|1|2|3|
+-+-+-+-+
+-+-+-+-+
|4|5|6|7|
+-+-+-+-+
+-+-+-+
|8|9|0|
+-+-+-+
There all the x's would be coastal tiles.
I hope this helps a bit.