Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.14 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class DungeonGenerator : MonoBehaviour
  7. {
  8.     public int mapWidth;
  9.     public int mapHeight;
  10.  
  11.     public int widthMinRoom;
  12.     public int widthMaxRoom;
  13.     public int heightMinRoom;
  14.     public int heightMaxRoom;
  15.  
  16.     public int minCorridorLength;
  17.     public int maxCorridorLength;
  18.     public int maxFeatures;
  19.     int countFeatures;
  20.  
  21.     public bool isASCII;
  22.  
  23.     public List<Feature> allFeatures;
  24.  
  25.     public void InitializeDungeon()
  26.     {
  27.         MapManager.map = new Tile[mapWidth, mapHeight];      
  28.     }
  29.  
  30.     public void GenerateDungeon()
  31.     {
  32.         GenerateFeature("Room", new Wall(), true);
  33.  
  34.         for (int i = 0; i < 500; i++)
  35.         {
  36.             Feature originFeature;          
  37.  
  38.             if (allFeatures.Count == 1)
  39.             {
  40.                 originFeature = allFeatures[0];
  41.             }
  42.             else
  43.             {
  44.                 originFeature = allFeatures[Random.Range(0, allFeatures.Count - 1)];
  45.             }
  46.  
  47.             Wall wall = ChoseWall(originFeature);
  48.             if (wall == null) continue;
  49.  
  50.             string type;
  51.  
  52.             if (originFeature.type == "Room")
  53.             {
  54.                 type = "Corridor";
  55.             }
  56.             else
  57.             {
  58.                 if (Random.Range(0, 100) < 90)
  59.                 {
  60.                     type = "Room";
  61.                 }
  62.                 else
  63.                 {
  64.                     type = "Corridor";
  65.                 }
  66.             }
  67.  
  68.             GenerateFeature(type, wall);
  69.  
  70.             if (countFeatures >= maxFeatures) break;
  71.         }
  72.  
  73.         DrawMap(isASCII);
  74.     }
  75.  
  76.     void GenerateFeature(string type, Wall wall, bool isFirst = false)
  77.     {
  78.         Feature room = new Feature();
  79.         room.positions = new List<Vector2Int>();
  80.  
  81.         int roomWidth = 0;
  82.         int roomHeight = 0;
  83.  
  84.         if (type == "Room")
  85.         {
  86.             roomWidth = Random.Range(widthMinRoom, widthMaxRoom);
  87.             roomHeight = Random.Range(heightMinRoom, heightMaxRoom);
  88.         }
  89.         else
  90.         {
  91.             switch (wall.direction)
  92.             {
  93.                 case "South":
  94.                     roomWidth = 3;
  95.                     roomHeight = Random.Range(minCorridorLength, maxCorridorLength);
  96.                     break;
  97.                 case "North":
  98.                     roomWidth = 3;
  99.                     roomHeight = Random.Range(minCorridorLength, maxCorridorLength);
  100.                     break;
  101.                 case "West":
  102.                     roomWidth = Random.Range(minCorridorLength, maxCorridorLength);
  103.                     roomHeight = 3;
  104.                     break;
  105.                 case "East":
  106.                     roomWidth = Random.Range(minCorridorLength, maxCorridorLength);
  107.                     roomHeight = 3;
  108.                     break;
  109.  
  110.             }
  111.         }
  112.  
  113.         int xStartingPoint;
  114.         int yStartingPoint;
  115.  
  116.         if (isFirst)
  117.         {
  118.             xStartingPoint = mapWidth / 2;
  119.             yStartingPoint = mapHeight / 2;
  120.         }
  121.         else
  122.         {
  123.             int id;
  124.             if (wall.positions.Count == 3) id = 1;
  125.             else id = Random.Range(1, wall.positions.Count - 2);
  126.  
  127.             xStartingPoint = wall.positions[id].x;
  128.             yStartingPoint = wall.positions[id].y;
  129.         }
  130.  
  131.         Vector2Int lastWallPosition = new Vector2Int(xStartingPoint, yStartingPoint);
  132.  
  133.         if (isFirst)
  134.         {
  135.             xStartingPoint -= Random.Range(1, roomWidth);
  136.             yStartingPoint -= Random.Range(1, roomHeight);
  137.         }
  138.         else
  139.         {
  140.             switch (wall.direction)
  141.             {
  142.                 case "South":
  143.                     if (type == "Room") xStartingPoint -= Random.Range(1, roomWidth - 2);
  144.                     else xStartingPoint--;
  145.                     yStartingPoint -= Random.Range(1, roomHeight - 2);
  146.                     break;
  147.                 case "North":
  148.                     if (type == "Room") xStartingPoint -= Random.Range(1, roomWidth - 2);
  149.                     else xStartingPoint--;
  150.                     yStartingPoint++;
  151.                     break;
  152.                 case "West":
  153.                     xStartingPoint -= roomWidth;
  154.                     if (type == "Room") yStartingPoint -= Random.Range(1, roomHeight - 2);
  155.                     else yStartingPoint--;
  156.                     break;
  157.                 case "East":
  158.                     xStartingPoint++;
  159.                     if (type == "Room") yStartingPoint -= Random.Range(1, roomHeight - 2);
  160.                     else yStartingPoint--;
  161.                     break;
  162.             }
  163.         }
  164.  
  165.         if (!CheckIfHasSpace(new Vector2Int(xStartingPoint, yStartingPoint), new Vector2Int(xStartingPoint + roomWidth - 1, yStartingPoint + roomHeight - 1)))
  166.         {
  167.             return;
  168.         }
  169.  
  170.         room.walls = new Wall[4];
  171.  
  172.         for (int i = 0; i < room.walls.Length; i++)
  173.         {
  174.             room.walls[i] = new Wall();
  175.             room.walls[i].positions = new List<Vector2Int>();
  176.             room.walls[i].length = 0;
  177.  
  178.             switch (i)
  179.             {
  180.                 case 0:
  181.                     room.walls[i].direction = "South";
  182.                     break;
  183.                 case 1:
  184.                     room.walls[i].direction = "North";
  185.                     break;
  186.                 case 2:
  187.                     room.walls[i].direction = "West";
  188.                     break;
  189.                 case 3:
  190.                     room.walls[i].direction = "East";
  191.                     break;
  192.             }
  193.         }
  194.  
  195.         for (int y = 0; y < roomHeight; y++)
  196.         {
  197.             for (int x = 0; x < roomWidth; x++)
  198.             {
  199.                 Vector2Int position = new Vector2Int();
  200.                 position.x = xStartingPoint + x;
  201.                 position.y = yStartingPoint + y;
  202.  
  203.                 room.positions.Add(position);
  204.  
  205.                 MapManager.map[position.x, position.y] = new Tile();
  206.                 MapManager.map[position.x, position.y].xPosition = position.x;
  207.                 MapManager.map[position.x, position.y].yPosition = position.y;
  208.  
  209.                 if (y == 0)
  210.                 {
  211.                     room.walls[0].positions.Add(position);
  212.                     room.walls[0].length++;
  213.                     MapManager.map[position.x, position.y].type = "Wall";
  214.                 }
  215.                 if (y == (roomHeight - 1))
  216.                 {
  217.                     room.walls[1].positions.Add(position);
  218.                     room.walls[1].length++;
  219.                     MapManager.map[position.x, position.y].type = "Wall";
  220.                 }
  221.                 if (x == 0)
  222.                 {
  223.                     room.walls[2].positions.Add(position);
  224.                     room.walls[2].length++;
  225.                     MapManager.map[position.x, position.y].type = "Wall";
  226.                 }
  227.                 if (x == (roomWidth - 1))
  228.                 {
  229.                     room.walls[3].positions.Add(position);
  230.                     room.walls[3].length++;
  231.                     MapManager.map[position.x, position.y].type = "Wall";
  232.                 }
  233.                 if (MapManager.map[position.x, position.y].type != "Wall")
  234.                 {
  235.                     MapManager.map[position.x, position.y].type = "Floor";
  236.                 }
  237.             }
  238.         }
  239.  
  240.         if (!isFirst)
  241.         {
  242.             MapManager.map[lastWallPosition.x, lastWallPosition.y].type = "Floor";
  243.             switch (wall.direction)
  244.             {
  245.                 case "South":
  246.                     MapManager.map[lastWallPosition.x, lastWallPosition.y - 1].type = "Floor";
  247.                     break;
  248.                 case "North":
  249.                     MapManager.map[lastWallPosition.x, lastWallPosition.y + 1].type = "Floor";
  250.                     break;
  251.                 case "West":
  252.                     MapManager.map[lastWallPosition.x - 1, lastWallPosition.y].type = "Floor";
  253.                     break;
  254.                 case "East":
  255.                     MapManager.map[lastWallPosition.x + 1, lastWallPosition.y].type = "Floor";
  256.                     break;
  257.             }
  258.         }
  259.  
  260.         room.width = roomWidth;
  261.         room.height = roomHeight;
  262.         room.type = type;
  263.         allFeatures.Add(room);
  264.         countFeatures++;
  265.     }
  266.  
  267.     bool CheckIfHasSpace(Vector2Int start, Vector2Int end)
  268.     {
  269.         for (int y = start.y; y <= end.y; y++)
  270.         {
  271.             for (int x = start.x; x <= end.x; x++)
  272.             {
  273.                 if (x < 0 || y < 0 || x >= mapWidth || y >= mapHeight) return false;
  274.                 if (MapManager.map != null) return false;
  275.             }
  276.         }
  277.  
  278.         return true;
  279.     }
  280.  
  281.     Wall ChoseWall(Feature feature)
  282.     {
  283.         for (int i = 0; i < 10; i++)
  284.         {
  285.             int id = Random.Range(0, 100) / 25;
  286.             if (!feature.walls[id].hasFeature)
  287.             {
  288.                 return feature.walls[id];
  289.             }
  290.         }
  291.         return null;
  292.     }
  293.  
  294.     void DrawMap(bool isASCII)
  295.     {
  296.         if (isASCII)
  297.         {
  298.             Text screen = GameObject.Find("Console").GetComponent<Text>();
  299.  
  300.             string asciiMap = "";
  301.  
  302.             for (int y = (mapHeight - 1); y >= 0; y--)
  303.             {
  304.                 for (int x = 0; x < mapWidth; x++)
  305.                 {
  306.                     if (MapManager.map[x, y] != null)
  307.                     {
  308.                         switch (MapManager.map[x, y].type)
  309.                         {
  310.                             case "Wall":
  311.                                 asciiMap += "#";
  312.                                 break;
  313.                             case "Floor":
  314.                                 asciiMap += ".";
  315.                                 break;
  316.                         }
  317.                     }
  318.                     else
  319.                     {
  320.                         asciiMap += " ";
  321.                     }
  322.  
  323.                     if (x == (mapWidth - 1))
  324.                     {
  325.                         asciiMap += "\n";
  326.                     }
  327.                 }
  328.             }
  329.  
  330.             screen.text = asciiMap;
  331.         }
  332.     }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement