Advertisement
SebastianLague

PCG 03 MapGenerator

Mar 2nd, 2015
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. public class MapGenerator : MonoBehaviour {
  2.  
  3.     public int width;
  4.     public int height;
  5.  
  6.     public string seed;
  7.     public bool useRandomSeed;
  8.  
  9.     [Range(0,100)]
  10.     public int randomFillPercent;
  11.  
  12.     int[,] map;
  13.  
  14.     void Start() {
  15.         GenerateMap();
  16.     }
  17.  
  18.     void Update() {
  19.         if (Input.GetMouseButtonDown(0)) {
  20.             GenerateMap();
  21.         }
  22.     }
  23.  
  24.     void GenerateMap() {
  25.         map = new int[width,height];
  26.         RandomFillMap();
  27.  
  28.         for (int i = 0; i < 5; i ++) {
  29.             SmoothMap();
  30.         }
  31.  
  32.         MeshGenerator meshGen = GetComponent<MeshGenerator>();
  33.         meshGen.GenerateMesh(map, 1);
  34.     }
  35.  
  36.  
  37.     void RandomFillMap() {
  38.         if (useRandomSeed) {
  39.             seed = Time.time.ToString();
  40.         }
  41.  
  42.         System.Random pseudoRandom = new System.Random(seed.GetHashCode());
  43.  
  44.         for (int x = 0; x < width; x ++) {
  45.             for (int y = 0; y < height; y ++) {
  46.                 if (x == 0 || x == width-1 || y == 0 || y == height -1) {
  47.                     map[x,y] = 1;
  48.                 }
  49.                 else {
  50.                     map[x,y] = (pseudoRandom.Next(0,100) < randomFillPercent)? 1: 0;
  51.                 }
  52.             }
  53.         }
  54.     }
  55.  
  56.     void SmoothMap() {
  57.         for (int x = 0; x < width; x ++) {
  58.             for (int y = 0; y < height; y ++) {
  59.                 int neighbourWallTiles = GetSurroundingWallCount(x,y);
  60.  
  61.                 if (neighbourWallTiles > 4)
  62.                     map[x,y] = 1;
  63.                 else if (neighbourWallTiles < 4)
  64.                     map[x,y] = 0;
  65.  
  66.             }
  67.         }
  68.     }
  69.  
  70.     int GetSurroundingWallCount(int gridX, int gridY) {
  71.         int wallCount = 0;
  72.         for (int neighbourX = gridX - 1; neighbourX <= gridX + 1; neighbourX ++) {
  73.             for (int neighbourY = gridY - 1; neighbourY <= gridY + 1; neighbourY ++) {
  74.                 if (neighbourX >= 0 && neighbourX < width && neighbourY >= 0 && neighbourY < height) {
  75.                     if (neighbourX != gridX || neighbourY != gridY) {
  76.                         wallCount += map[neighbourX,neighbourY];
  77.                     }
  78.                 }
  79.                 else {
  80.                     wallCount ++;
  81.                 }
  82.             }
  83.         }
  84.  
  85.         return wallCount;
  86.     }
  87.  
  88.  
  89.     void OnDrawGizmos() {
  90.         /*
  91.         if (map != null) {
  92.             for (int x = 0; x < width; x ++) {
  93.                 for (int y = 0; y < height; y ++) {
  94.                     Gizmos.color = (map[x,y] == 1)?Color.black:Color.white;
  95.                     Vector3 pos = new Vector3(-width/2 + x + .5f,0, -height/2 + y+.5f);
  96.                     Gizmos.DrawCube(pos,Vector3.one);
  97.                 }
  98.             }
  99.         }
  100.         */
  101.     }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement