Search Unity

Procedural map and auto-tiling

Discussion in '2D' started by Pearbook, Mar 11, 2015.

  1. Pearbook

    Pearbook

    Joined:
    Feb 3, 2013
    Posts:
    7
    I'm working on a 2D top down game. The map is randomly generated using a perlin noise. This is what the code looks like:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MapGeneration : MonoBehaviour
    5. {
    6.     public int MapHeight;
    7.     public int MapWidth;
    8.     private int seed;
    9.  
    10.     int[,] map;
    11.  
    12.     public float TileSize;
    13.     public float noiseStrength = 10.0f;
    14.  
    15.     void Start()
    16.     {
    17.         Generate();
    18.     }
    19.  
    20.     void Generate()
    21.     {
    22.         seed = Random.Range(0, 100000);
    23.         int treeSeed = Random.Range(0, 100000);
    24.  
    25.         map = new int[MapWidth, MapHeight];
    26.  
    27.         for(int x = 0; x < MapWidth; ++x)
    28.         {
    29.             for(int y = 0; y < MapHeight; ++y)
    30.             {
    31.                 float noise = PerlinNoise(x,y, seed, noiseStrength) + CircleGradient(x, y);
    32.  
    33.                 float treeNoise = PerlinNoise(x,y, treeSeed, 10f);
    34.  
    35.                 if (noise < 0.5f)
    36.                     map[x,y] = 3; // Mountain
    37.                 else if (noise < 0.8f)
    38.                     map[x,y] = 2; // Land
    39.                 else
    40.                     map[x,y] = 0; // Water
    41.  
    42.                 if(map[x,y] == 2 && treeNoise < 0.4f)
    43.                     map[x,y] = 1; // Trees
    44.             }
    45.         }
    46.  
    47.         BuildMap();
    48.     }
    49.  
    50.     void BuildMap()
    51.     {
    52.         for(int x = 0; x < MapWidth; ++x)
    53.         {
    54.             for(int y = 0; y < MapHeight; ++y)
    55.             {
    56.                 GameObject Tile = (GameObject)Instantiate(Resources.Load("Tiles/Tile" + map[x,y]), transform.position, Quaternion.identity);
    57.                 Tile.transform.position = new Vector2((x * TileSize - (MapWidth / 2) * TileSize) + TileSize / 2, -(y * TileSize - ((MapHeight / 2) * TileSize) - TileSize / 2));
    58.                 Tile.transform.parent = transform;
    59.             }
    60.         }
    61.     }
    62.  
    63.     float CircleGradient(float x, float y)
    64.     {
    65.         int centerX = MapWidth / 2;
    66.         int centerY = MapHeight / 2;
    67.  
    68.         float distanceX = (centerX - x) * (centerX - x);
    69.         float distanceY = (centerY - y) * (centerY - y);
    70.        
    71.         float distanceToCenter = Mathf.Sqrt(distanceX + distanceY);
    72.         distanceToCenter = distanceToCenter / MapWidth;
    73.  
    74.         return distanceToCenter;
    75.     }
    76.  
    77.     float PerlinNoise(float x, float y, float seedToUse, float strength)
    78.     {
    79.         float noise = Mathf.PerlinNoise(seedToUse + x / strength, seedToUse + y / strength);
    80.  
    81.         return noise;
    82.     }
    83.  
    84. }
    85.  
    And this is the result:



    Blue is water,
    Light green is grass,
    Dark green are trees,
    and the grey is stone.

    The entire map is made of tiles so the next thing I would like to do is add auto-tiling. I've never done this before and have no idea how to get started. Someone told me it was a real annoying thing to do because you have to check every tile in every direction and tell what tile needs to have which visual. But I remember reading somewhere you can calculate a number which will be linked to the visual of that tile.

    So I was hoping someone could help me with this and if you see something in my code that I could do better I'd love to hear because I'm still learning.
     
  2. JymWythawhy

    JymWythawhy

    Joined:
    Jan 18, 2015
    Posts:
    25
    Pearbook,

    I haven't had time to play with this myself, but I was recently looking into something similar and found this resource: http://www.oldschoolpixels.com/?p=252 . It claims to be a C# script that can be used to match a tiles sprite based on the tiles around it, which I think is what you are looking for. Like I said, I haven't been able to try it out yet, but it looked promising! Good luck.