When last we met we had set up a 2 dimensional array and filled it with 1s and 0s. Now we’re going to look at getting a visual representation for our noise. For this we will use the in-built method OnDrawGizmos (). To begin with let’s use our old friend the nested for loops to iterate through our array.
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
We need to check each coordinate. If it contains a 1 we will draw a black square and if it contains a 0 we will draw a white square.
Gizmos.color = (map[x,y] == 1)?Color.black : Color.white;
Vector3 pos = new Vector3(-width/2 + x + 0.5f, 0, -height/2 + y + 0.5f);
Gizmos.DrawCube(pos, Vector3.one);
Now if you press play and switch from game view to scene view you will have your noise map. Try it with different strings to make sure that it is properly using them as a seed.
The final thing we need to do is bring order from chaos, create a map from the noise. To do this we are going to create a method called SmoothMap (). Okay so this method is really important since it will define how our map looks. So we want to go through all our squares so... nested for loops? Nested for loops.
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
So the method we are going to use is to check how many squares around each coordinate are filled. To do so we’ll create a method that takes two values, our current x and y positions, as arguments and checks everything around it.
int neighbourWallTiles = GetSurroundingWallCount(x,y);
So because we want this method to be called from every square it needs an internal counter which will always start at 0.
int GetSurroundingWallCount(int gridX, int gridY) {
int wallCount = 0;
We’ll use the x and y values as a starting point and check what’s around them, incrementing wallCount when we find something. Remember we want to keep this within the bounds of our grid and not get an error from trying to access coordinates outside the boundary.
for (int neighbourX = gridX - 1; neighbourX <= gridX + 1; neighbourX ++) {
for (int neighbourY = gridY - 1; neighbourY <= gridY + 1; neighbourY ++) {
if (neighbourX >= 0 && neighbourX < width && neighbourY >= 0 && neighbourY < height) {
if (neighbourX != gridX || neighbourY != gridY) {
wallCount += map[neighbourX,neighbourY];
}
}
else {
wallCount ++;
}
}
}
return wallCount;
}
Going back into SmoothMap we can use the returned integer. The idea we’re going for is that the more isolated squares become white while the more dense areas are filled in.
if (neighbourWallTiles < 3){
map[x,y] = 0;
}
else if (neighbourWallTiles > 5){
map[x,y] = 1;
}
else if (neighbourWallTiles == 2){
map[x,y] = 1;
}
This should generate clear areas of white and black, kind of like continents in an ocean. Then believe it or not that is that. The shapes we get are the outline for how our map is going to look. Later we will look at how to turn each of the squares into a navigable room.
Speaking of rooms next week we’ll be looking at how to generate rooms with random layouts. In the mean time I hope this has been informative and if you have any questions feel free to reach out.