Advertisement
Guest User

Generator

a guest
Oct 14th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Level : MonoBehaviour
  6. {
  7.     public int height;
  8.     public int width;
  9.     Room[,] maze;
  10.  
  11.     private void Start()
  12.     {
  13.         CreateLevel();
  14.     }
  15.  
  16.     private void CreateLevel()
  17.     {
  18.         maze = new Room[height, width];
  19.         for (int i = 0; i < height; i++)
  20.         {
  21.             for (int j = 0; j < width; j++)
  22.             {
  23.                 if ((i % 2 != 0 && j % 2 != 0) && //если ячейка нечетная по x и y,
  24.                    (i < height - 1 && j < width - 1)) //и при этом находится в пределах стен лабиринта
  25.                 {
  26.                     Room room = new Room();
  27.                     room.x = i;
  28.                     room.y = j;
  29.                     room.visited = false;
  30.                     room.wall = false;
  31.                     maze[i, j] = room;
  32.                 }
  33.                 else
  34.                 {
  35.                     Room wall = new Room();
  36.                     wall.x = i;
  37.                     wall.y = j;
  38.                     wall.visited = false;
  39.                     wall.wall = true;
  40.                     maze[i, j] = wall;
  41.                 }
  42.             }
  43.         }
  44.  
  45.         Room startRoom = new Room();
  46.         startRoom.x = 1;
  47.         startRoom.y = 1;
  48.         startRoom.visited = true;
  49.         Room currentRoom = startRoom;
  50.         Room neighbourRoom;
  51.         List<Room> neighboursNoVisited = new List<Room>();
  52.  
  53.         do
  54.         {
  55.             neighboursNoVisited = NeighboursNoVisited(currentRoom.x, currentRoom.y);
  56.             neighbourRoom = neighboursNoVisited[Random.Range(0, neighboursNoVisited.Count)];
  57.             RemoveWall(currentRoom, neighbourRoom);
  58.             neighbourRoom.visited = true;
  59.             currentRoom = neighbourRoom;
  60.  
  61.         } while (neighboursNoVisited.Count > 0);
  62.  
  63.  
  64.  
  65.         string map = "";
  66.         for(int i = 0; i < height; i++)
  67.         {
  68.             for(int j = 0; j < width; j++)
  69.             {
  70.                 if (maze[i, j].wall)
  71.                 {
  72.                     map += "*";
  73.                 }
  74.                 else
  75.                 {
  76.                     map += "_";
  77.                 }
  78.                 if(j + 1 == width)
  79.                 {
  80.                     map += "\n";
  81.                 }
  82.             }
  83.         }
  84.  
  85.         Debug.Log(map);
  86.     }
  87.  
  88.     struct Room
  89.     {
  90.         public int x;
  91.         public int y;
  92.         public bool visited;
  93.         public bool wall;
  94.     }
  95.  
  96.     private List<Room> NeighboursNoVisited(int x, int y)
  97.     {
  98.         List<Room> rooms = new List<Room>();
  99.  
  100.         if( x - 2 > 0 && !maze[x - 2, y].visited)
  101.         {
  102.             rooms.Add(maze[x - 2, y]);
  103.         }
  104.         if (x + 2 < height && !maze[x + 2, y].visited)
  105.         {
  106.             rooms.Add(maze[x + 2, y]);
  107.         }
  108.         if (y - 2 > 0 && !maze[x, y - 2].visited)
  109.         {
  110.             rooms.Add(maze[x, y - 2]);
  111.         }
  112.         if (y + 2 < width && !maze[x, y + 2].visited)
  113.         {
  114.             rooms.Add(maze[x, y + 2]);
  115.         }
  116.  
  117.         return rooms;
  118.     }
  119.  
  120.     private void RemoveWall(Room currentRoom, Room neighbourRoom)
  121.     {
  122.         if(currentRoom.x < neighbourRoom.x)
  123.         {
  124.             maze[currentRoom.x + 1, currentRoom.y].wall = false;
  125.         }
  126.         else if(currentRoom.x > neighbourRoom.x)
  127.         {
  128.             maze[currentRoom.x - 1, currentRoom.y].wall = false;
  129.         }
  130.         else if (currentRoom.y < neighbourRoom.y)
  131.         {
  132.             maze[currentRoom.x, currentRoom.y + 1].wall = false;
  133.         }
  134.         else if (currentRoom.y > neighbourRoom.y)
  135.         {
  136.             maze[currentRoom.x, currentRoom.y + 1].wall = false;
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement