Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- //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
- using System;
- public class MapGenerator : MonoBehaviour
- {
- //The max size of our cave
- public int width;
- public int height;
- //A string of characters that is used when generoating our cave
- //Using this seed always creates the same cave so long as no varriables change
- public string seed;
- //Randomly creates a new seed
- public bool useRandomSeed;
- //Having this range parameter forces the int below to be within the range we gave.
- //This is a percent so we want it to be between 0% and 100%
- [Range(0,100)]
- public int randomFillPercent; //How of the cave is filled before we smooth it out
- /*
- * This is an array
- * An array is a list, in this case a list of int's
- * This list stores values to certin locations within the array
- * The comma denotes that we have two coordinates location.
- * Very similar to a 2D grid.
- */
- int[,] map;
- //When we launch the game we want to generate a cave (or map as its not yet 3D)
- public void Start()
- {
- //This line calls the gererate map function
- GenerateMap();
- }
- void Update()
- {
- if (Input.GetMouseButtonDown(0))
- {
- GenerateMap();
- }
- }
- //This is the generate map function
- void GenerateMap()
- {
- //First we establish the size of our map using two varriables named height and width
- map = new int[width,height];
- //Now we call the random fill map function to fill that map
- RandomFillMap();
- //Here we run the smooth map funciton 5 times, this can be changed based on prefrence
- for (int i = 0; i < 5; i++)
- {
- //Calls smooth map function
- SmoothMap();
- }
- }
- //Here is where the map is actually filled.
- void RandomFillMap()
- {
- //If we have are using the random seed options we exucute this code
- if (useRandomSeed)
- {
- //This line uses the amount of time the game has been running for as the seed for our map
- seed = Time.time.ToString();
- }
- //We are using the seed to create a new System.Random
- System.Random pseudoRandom = new System.Random(seed.GetHashCode());
- //For each point on the X axis
- for (int x = 0; x < width; x++)
- {
- //For each point on the Y axis
- for (int y = 0; y < height; y++)
- {
- //If the point is an edge point
- if (x == 0 || x == width-1 || y == 0 || y == height - 1)
- {
- //Set the coordinate to 1 (or a wall in this case)
- map[x, y] = 1;
- }
- else
- {
- //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
- map[x, y] = (pseudoRandom.Next(0, 100) < randomFillPercent) ? 1 : 0; //What is this queastion mark?
- //It acts like an if else statement
- //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
- }
- }
- }
- }
- //Here is where the map smooths itself to create the cave
- void SmoothMap()
- {
- //For each point on the X axis
- for (int x = 0; x < width; x++)
- {
- //For each point on the Y axis
- for (int y = 0; y < height; y++)
- {
- //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
- int neighbourWallTiles = GetSurroundingWallCount(x, y);
- //If that number is less the 4 (arbitrary number)
- if (neighbourWallTiles > 4)
- {
- //Make it a wall
- map[x, y] = 1;
- }
- //If that number is greater than 4 (same arbitrary number)
- else if (neighbourWallTiles < 4)
- {
- //Make it open
- map[x, y] = 0;
- }
- }
- }
- }
- //How we know how many walls are touching each wall
- //Here we have another array like before but here we have specified the names of the points
- //By using the int at the begining we are declaring that the function GetSurroundingWallCount will have an output of type int
- int GetSurroundingWallCount(int gridX, int gridY)
- {
- //To begin we set the wall count to zero
- int wallcount = 0;
- //For each point on the X axis within a 3x3 grid of that tile
- for (int neighbourX = gridX - 1; neighbourX <= gridX + 1; neighbourX++)
- {
- //For each point on the Y axis within a 3x3 grid of that tile
- for (int neighbourY = gridY - 1; neighbourY <= gridY + 1; neighbourY++)
- {
- //This line checks to see if we are checking a space on the edge of the grid or off the grid
- if (neighbourX >= 0 && neighbourX < width && neighbourY >= 0 && neighbourY < height)
- {
- //If the space we are checking doesn't exsist than just pretend its a wall
- if (neighbourX != gridX || neighbourY != gridY)
- {
- wallcount += map[neighbourX, neighbourY];
- }
- }
- else
- {
- //Add to the count of walls touhcing this wall
- wallcount++;
- }
- }
- }
- //This is our output
- return wallcount;
- }
- //This function draws within the Unity editor and is useful for visualizing our code
- private void OnDrawGizmos()
- {
- //If the map is not blank
- if(map != null )
- {
- //For every X coordinate
- for (int x = 0; x < width; x++)
- {
- //For every Y coordinate
- for (int y = 0; y < height; y++)
- {
- //Select the tile with the and set the color to black when the number is 1 and white when it is 0
- Gizmos.color = (map[x, y] == 1) ? Color.black : Color.white;
- //Center the object based on the width and height varriables we set at the begining
- Vector3 pos = new Vector3(-width / 2 + x + .5f, 0, -height / 2 + y + .5f);
- //Draw the tile with the correct color in the correct space
- Gizmos.DrawCube(pos, Vector3.one);
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment