LegitTeddyBears

Cellular Automata - MapGenerator 1

Apr 22nd, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //Here we called the system function so we could use some of the words that aren't used by Unity defultly, such as System.Random
  5. using System;
  6.  
  7. public class MapGenerator : MonoBehaviour
  8. {
  9.     //The max size of our cave
  10.     public int width;
  11.     public int height;
  12.  
  13.     //A string of characters that is used when generoating our cave
  14.     //Using this seed always creates the same cave so long as no varriables change
  15.     public string seed;
  16.  
  17.     //Randomly creates a new seed
  18.     public bool useRandomSeed;
  19.  
  20.     //Having this range parameter forces the int below to be within the range we gave.
  21.     //This is a percent so we want it to be between 0% and 100%
  22.     [Range(0,100)]
  23.     public int randomFillPercent; //How of the cave is filled before we smooth it out
  24.  
  25.     /*
  26.      * This is an array
  27.      * An array is a list, in this case a list of int's
  28.      * This list stores values to certin locations within the array
  29.      * The comma denotes that we have two coordinates location.
  30.      * Very similar to a 2D grid.
  31.     */
  32.     int[,] map;
  33.  
  34.     //When we launch the game we want to generate a cave (or map as its not yet 3D)
  35.     public void Start()
  36.     {
  37.  
  38.         //This line calls the gererate map function
  39.         GenerateMap();
  40.     }
  41.  
  42.     void Update()
  43.     {
  44.         if (Input.GetMouseButtonDown(0))
  45.         {
  46.             GenerateMap();
  47.         }
  48.     }
  49.     //This is the generate map function
  50.     void GenerateMap()
  51.     {
  52.         //First we establish the size of our map using two varriables named height and width
  53.         map = new int[width,height];
  54.         //Now we call the random fill map function to fill that map
  55.         RandomFillMap();
  56.  
  57.         //Here we run the smooth map funciton 5 times, this can be changed based on prefrence
  58.         for (int i = 0; i < 5; i++)
  59.         {
  60.             //Calls smooth map function
  61.             SmoothMap();
  62.         }
  63.     }
  64.  
  65.     //Here is where the map is actually filled.
  66.     void RandomFillMap()
  67.     {
  68.         //If we have are using the random seed options we exucute this code
  69.         if (useRandomSeed)
  70.         {
  71.             //This line uses the amount of time the game has been running for as the seed for our map
  72.             seed = Time.time.ToString();
  73.         }
  74.  
  75.  
  76.         //We are using the seed to create a new System.Random
  77.         System.Random pseudoRandom = new System.Random(seed.GetHashCode());
  78.         //For each point on the X axis
  79.         for (int x = 0; x < width; x++)
  80.         {
  81.             //For each point on the Y axis
  82.             for (int y = 0; y < height; y++)
  83.             {
  84.                 //If the point is an edge point
  85.                 if (x == 0 || x == width-1 || y == 0 || y == height - 1)
  86.                 {
  87.                     //Set the coordinate to 1 (or a wall in this case)
  88.                     map[x, y] = 1;
  89.                 }
  90.                 else
  91.                 {
  92.                     //Set the coordinate to a random number between 0 and 100 if the number is smaller than the fill percent than make it a wall
  93.                     map[x, y] = (pseudoRandom.Next(0, 100) < randomFillPercent) ? 1 : 0; //What is this queastion mark?
  94.                                                                                          //It acts like an if else statement
  95.                    //In this case the when the number is less than randomFillPercent the it becomes a 1 and when greater than randomFillPercent it becomes a 0
  96.                 }
  97.                
  98.             }
  99.         }
  100.     }
  101.  
  102.     //Here is where the map smooths itself to create the cave
  103.     void SmoothMap()
  104.     {
  105.         //For each point on the X axis
  106.         for (int x = 0; x < width; x++)
  107.         {
  108.             //For each point on the Y axis
  109.             for (int y = 0; y < height; y++)
  110.             {
  111.                 //Set the value of neighbourWallTiles to be equal to the amount of wall tiles there are next to it in both the X and Y axis
  112.                 int neighbourWallTiles = GetSurroundingWallCount(x, y);
  113.  
  114.                 //If that number is less the 4 (arbitrary number)
  115.                 if (neighbourWallTiles > 4)
  116.                 {
  117.                     //Make it a wall
  118.                     map[x, y] = 1;
  119.                 }
  120.                 //If that number is greater than 4 (same arbitrary number)
  121.                 else if (neighbourWallTiles < 4)
  122.                 {
  123.                     //Make it open
  124.                     map[x, y] = 0;
  125.                 }
  126.             }
  127.         }
  128.     }
  129.    
  130.  
  131.     //How we know how many walls are touching each wall
  132.     //Here we have another array like before but here we have specified the names of the points
  133.     //By using the int at the begining we are declaring that the function GetSurroundingWallCount will have an output of type int
  134.     int GetSurroundingWallCount(int gridX, int gridY)
  135.     {
  136.  
  137.         //To begin we set the wall count to zero
  138.         int wallcount = 0;
  139.         //For each point on the X axis within a 3x3 grid of that tile
  140.         for (int neighbourX = gridX - 1;  neighbourX <= gridX + 1; neighbourX++)
  141.         {
  142.             //For each point on the Y axis within a 3x3 grid of that tile
  143.             for (int neighbourY = gridY - 1; neighbourY <= gridY + 1; neighbourY++)
  144.             {
  145.                 //This line checks to see if we are checking a space on the edge of the grid or off the grid
  146.                 if (neighbourX >= 0 && neighbourX < width && neighbourY >= 0 && neighbourY < height)
  147.                 {
  148.                     //If the space we are checking doesn't exsist than just pretend its a wall
  149.                     if (neighbourX != gridX || neighbourY != gridY)
  150.                     {
  151.                         wallcount += map[neighbourX, neighbourY];
  152.                     }
  153.                 }
  154.                 else
  155.                 {
  156.                     //Add to the count of walls touhcing this wall
  157.                     wallcount++;
  158.                 }
  159.             }
  160.         }
  161.        
  162.         //This is our output
  163.         return wallcount;
  164.     }
  165.  
  166.     //This function draws within the Unity editor and is useful for visualizing our code
  167.     private void OnDrawGizmos()
  168.     {
  169.         //If the map is not blank
  170.         if(map != null )
  171.         {
  172.             //For every X coordinate
  173.             for (int x = 0; x < width; x++)
  174.             {
  175.                 //For every Y coordinate
  176.                 for (int y = 0; y < height; y++)
  177.                 {
  178.                     //Select the tile with the and set the color to black when the number is 1 and white when it is 0
  179.                     Gizmos.color = (map[x, y] == 1) ? Color.black : Color.white;
  180.                     //Center the object based on the width and height varriables we set at the begining
  181.                     Vector3 pos = new Vector3(-width / 2 + x + .5f, 0, -height / 2 + y + .5f);
  182.                     //Draw the tile with the correct color in the correct space
  183.                     Gizmos.DrawCube(pos, Vector3.one);
  184.                 }
  185.             }
  186.         }
  187.     }
  188. }
Add Comment
Please, Sign In to add comment