Advertisement
SebastianLague

Procedural Cave Generation 01

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