Advertisement
nbarber20

Map Generation Part 1

Dec 17th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.70 KB | None | 0 0
  1.  
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class DungeonGeneration : MonoBehaviour {
  7.     bool[,] map;//true is whitespace;
  8.     public Rect[] Rooms;
  9.     Vector2 CursorPos;
  10.     public int roomPadding;
  11.     public int roomstomake;
  12.     public Vector2 RoomMinMaxWidth;
  13.     public Vector2 RoomMinMaxHeight;
  14.     public GameObject Wall;
  15.     public GameObject PlayerPrefab;
  16.  
  17.     Vector2 RandomPosition;
  18.  
  19.     // Use this for initialization
  20.     void Start() {
  21.         //init the map
  22.         map = new bool[100, 100];
  23.         Rooms = new Rect[roomstomake];
  24.  
  25.         //make some rooms
  26.         for (int i = 0; i< roomstomake;i++)
  27.         {
  28.             Rooms[i] = spawnRoom();                                 //Spawn room
  29.             MakePath(Rooms[i].center, getNearestRoom(Rooms[i]));    //Connect to nearest room
  30.         }
  31.         //Make the player and put it in the center of the first room spawned
  32.         Instantiate(PlayerPrefab, Rooms[0].center, Quaternion.identity);
  33.  
  34.         spawnWorld();
  35.     }
  36.  
  37.     //carves out a room and returns its position and size as a rect
  38.     Rect spawnRoom()
  39.     {
  40.         int iterations = 0;
  41.         bool completed = false;
  42.         while (iterations<100 &&completed==false) {
  43.             RandomPosition = new Vector2(Random.Range(0, 100), Random.Range(0, 100));
  44.             iterations++;
  45.             float RectWidth = Random.Range(RoomMinMaxWidth.x, RoomMinMaxWidth.y);
  46.             float RectHeight = Random.Range(RoomMinMaxHeight.x, RoomMinMaxHeight.y);
  47.             //Check to see if this room overlaps or goes outside of the map
  48.             if (checkrect(new Vector2(RandomPosition.x- roomPadding, RandomPosition.y- roomPadding), new Vector2(RandomPosition.x + RectWidth+ roomPadding, RandomPosition.y + RectHeight+ roomPadding)))
  49.             {
  50.                 //if it does not overlap, carve the room out of the map array
  51.                 clearrect(new Vector2(RandomPosition.x, RandomPosition.y), new Vector2(RandomPosition.x + RectWidth, RandomPosition.y + RectHeight));
  52.                 completed = true;
  53.                 return new Rect(RandomPosition, new Vector2(RectWidth,RectHeight));
  54.             }
  55.         }
  56.         return new Rect(0, 0, 0, 0); //return an erroneous room if 100 randomly generated rooms don't fit.
  57.     }
  58.  
  59.     //Creates the shortest path between two points
  60.     void MakePath(Vector2 From, Vector2 To)
  61.     {
  62.         if(From == new Vector2(0,0)||To == new Vector2(0, 0))
  63.         {
  64.             return;
  65.         }
  66.         map[Mathf.RoundToInt(From.x), Mathf.RoundToInt(From.y)] = true;
  67.         map[Mathf.RoundToInt(To.x), Mathf.RoundToInt(To.y)] = true;
  68.         CursorPos = From;
  69.         bool done = false;
  70.         int iterations = 0;
  71.         while (done == false&&iterations<1000)
  72.         {
  73.             iterations++;
  74.          
  75.             if(CursorPos == To)
  76.             {
  77.                 done = true;
  78.             }
  79.             else
  80.             {
  81.                 float rng = Random.Range(0, 100);
  82.                 if (rng > 50)
  83.                 {
  84.                     CursorPos.x = Mathf.MoveTowards(CursorPos.x, To.x, 1);
  85.  
  86.                 }
  87.                 else
  88.                 {
  89.                     CursorPos.y = Mathf.MoveTowards(CursorPos.y, To.y, 1);
  90.                 }
  91.  
  92.                 map[Mathf.RoundToInt(CursorPos.x), Mathf.RoundToInt(CursorPos.y)] = true; //Set this tile to empty
  93.             }
  94.  
  95.         }
  96.     }
  97.  
  98.  
  99.     //Returns index of nearest room based on a source rect
  100.     Vector2 getNearestRoom(Rect source)
  101.     {
  102.         Vector2 nearestroom = new Vector2(0,0);
  103.         float distance = Mathf.Infinity;
  104.         foreach(Rect r in Rooms)
  105.         {
  106.             float newdist = Vector2.Distance(source.center, r.center);
  107.             if (newdist < distance && newdist > 0)
  108.             {
  109.                 distance = newdist;
  110.                 nearestroom = r.center;
  111.             }
  112.          
  113.         }
  114.         return nearestroom;
  115.     }
  116.  
  117.  
  118.  
  119.     //Runs through the room array and spawns the apropriate tile
  120.     void spawnWorld()
  121.     {
  122.         for (int x = 0; x < 100; x++)
  123.         {
  124.             for (int y = 0; y < 100; y++)
  125.             {
  126.                 if (map[x, y] == false) Instantiate(Wall, new Vector2(x, y), Quaternion.identity);
  127.             }
  128.         }
  129.     }
  130.  
  131.     //Used to carve out an empty space
  132.     void clearrect(Vector2 TopRight, Vector2 BottomLeft)
  133.     {
  134.         for (float x = TopRight.x; x <= BottomLeft.x; x++)
  135.         {
  136.             for (float y = TopRight.y; y <= BottomLeft.y; y++)
  137.             {
  138.                 map[Mathf.RoundToInt(x), Mathf.RoundToInt(y)] = true;
  139.             }
  140.         }
  141.     }
  142.  
  143.     //returns true if there if the provided range does not overlap a previously spawned room
  144.     bool checkrect(Vector2 TopRight, Vector2 BottomLeft)
  145.     {
  146.         if (TopRight.x < 1 || TopRight.y < 2 || BottomLeft.x > 99 || BottomLeft.y > 99)
  147.         {
  148.             return false;
  149.         }
  150.  
  151.         for (float x = TopRight.x; x <= BottomLeft.x; x++)
  152.         {
  153.             for (float y = TopRight.y; y <= BottomLeft.y; y++)
  154.             {
  155.                 if(map[Mathf.RoundToInt(x), Mathf.RoundToInt(y)] == true)// this is a room
  156.                 {
  157.                     return false;
  158.                 }
  159.             }
  160.         }
  161.         return true; // rect is clear
  162.     }
  163.  
  164.     //returns a random empty tile
  165.     Vector2 FindRandomSpot()
  166.     {
  167.         Vector2 spot = new Vector2(0, 0);
  168.         bool complete = false;
  169.         while(complete == false)
  170.         {
  171.             spot = new Vector2(Random.Range(0, 100), Random.Range(0, 100));
  172.             if (map[Mathf.RoundToInt(spot.x),Mathf.RoundToInt(spot.y)] == true)
  173.             {
  174.                 complete = true;              
  175.             }
  176.         }
  177.         return spot;
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement