Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DungeonGeneration : MonoBehaviour {
- bool[,] map;//true is whitespace;
- public Rect[] Rooms;
- Vector2 CursorPos;
- public int roomPadding;
- public int roomstomake;
- public Vector2 RoomMinMaxWidth;
- public Vector2 RoomMinMaxHeight;
- public GameObject Wall;
- public GameObject PlayerPrefab;
- Vector2 RandomPosition;
- // Use this for initialization
- void Start() {
- //init the map
- map = new bool[100, 100];
- Rooms = new Rect[roomstomake];
- //make some rooms
- for (int i = 0; i< roomstomake;i++)
- {
- Rooms[i] = spawnRoom(); //Spawn room
- MakePath(Rooms[i].center, getNearestRoom(Rooms[i])); //Connect to nearest room
- }
- //Make the player and put it in the center of the first room spawned
- Instantiate(PlayerPrefab, Rooms[0].center, Quaternion.identity);
- spawnWorld();
- }
- //carves out a room and returns its position and size as a rect
- Rect spawnRoom()
- {
- int iterations = 0;
- bool completed = false;
- while (iterations<100 &&completed==false) {
- RandomPosition = new Vector2(Random.Range(0, 100), Random.Range(0, 100));
- iterations++;
- float RectWidth = Random.Range(RoomMinMaxWidth.x, RoomMinMaxWidth.y);
- float RectHeight = Random.Range(RoomMinMaxHeight.x, RoomMinMaxHeight.y);
- //Check to see if this room overlaps or goes outside of the map
- if (checkrect(new Vector2(RandomPosition.x- roomPadding, RandomPosition.y- roomPadding), new Vector2(RandomPosition.x + RectWidth+ roomPadding, RandomPosition.y + RectHeight+ roomPadding)))
- {
- //if it does not overlap, carve the room out of the map array
- clearrect(new Vector2(RandomPosition.x, RandomPosition.y), new Vector2(RandomPosition.x + RectWidth, RandomPosition.y + RectHeight));
- completed = true;
- return new Rect(RandomPosition, new Vector2(RectWidth,RectHeight));
- }
- }
- return new Rect(0, 0, 0, 0); //return an erroneous room if 100 randomly generated rooms don't fit.
- }
- //Creates the shortest path between two points
- void MakePath(Vector2 From, Vector2 To)
- {
- if(From == new Vector2(0,0)||To == new Vector2(0, 0))
- {
- return;
- }
- map[Mathf.RoundToInt(From.x), Mathf.RoundToInt(From.y)] = true;
- map[Mathf.RoundToInt(To.x), Mathf.RoundToInt(To.y)] = true;
- CursorPos = From;
- bool done = false;
- int iterations = 0;
- while (done == false&&iterations<1000)
- {
- iterations++;
- if(CursorPos == To)
- {
- done = true;
- }
- else
- {
- float rng = Random.Range(0, 100);
- if (rng > 50)
- {
- CursorPos.x = Mathf.MoveTowards(CursorPos.x, To.x, 1);
- }
- else
- {
- CursorPos.y = Mathf.MoveTowards(CursorPos.y, To.y, 1);
- }
- map[Mathf.RoundToInt(CursorPos.x), Mathf.RoundToInt(CursorPos.y)] = true; //Set this tile to empty
- }
- }
- }
- //Returns index of nearest room based on a source rect
- Vector2 getNearestRoom(Rect source)
- {
- Vector2 nearestroom = new Vector2(0,0);
- float distance = Mathf.Infinity;
- foreach(Rect r in Rooms)
- {
- float newdist = Vector2.Distance(source.center, r.center);
- if (newdist < distance && newdist > 0)
- {
- distance = newdist;
- nearestroom = r.center;
- }
- }
- return nearestroom;
- }
- //Runs through the room array and spawns the apropriate tile
- void spawnWorld()
- {
- for (int x = 0; x < 100; x++)
- {
- for (int y = 0; y < 100; y++)
- {
- if (map[x, y] == false) Instantiate(Wall, new Vector2(x, y), Quaternion.identity);
- }
- }
- }
- //Used to carve out an empty space
- void clearrect(Vector2 TopRight, Vector2 BottomLeft)
- {
- for (float x = TopRight.x; x <= BottomLeft.x; x++)
- {
- for (float y = TopRight.y; y <= BottomLeft.y; y++)
- {
- map[Mathf.RoundToInt(x), Mathf.RoundToInt(y)] = true;
- }
- }
- }
- //returns true if there if the provided range does not overlap a previously spawned room
- bool checkrect(Vector2 TopRight, Vector2 BottomLeft)
- {
- if (TopRight.x < 1 || TopRight.y < 2 || BottomLeft.x > 99 || BottomLeft.y > 99)
- {
- return false;
- }
- for (float x = TopRight.x; x <= BottomLeft.x; x++)
- {
- for (float y = TopRight.y; y <= BottomLeft.y; y++)
- {
- if(map[Mathf.RoundToInt(x), Mathf.RoundToInt(y)] == true)// this is a room
- {
- return false;
- }
- }
- }
- return true; // rect is clear
- }
- //returns a random empty tile
- Vector2 FindRandomSpot()
- {
- Vector2 spot = new Vector2(0, 0);
- bool complete = false;
- while(complete == false)
- {
- spot = new Vector2(Random.Range(0, 100), Random.Range(0, 100));
- if (map[Mathf.RoundToInt(spot.x),Mathf.RoundToInt(spot.y)] == true)
- {
- complete = true;
- }
- }
- return spot;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement