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 CellularAutomataTest : MonoBehaviour
- {
- public class Cell
- {
- public bool visited = false;
- public bool[] status = new bool[6]; // [North, South, East, West, Up, Down]
- }
- public Vector3 size;
- public int startPos = 0;
- public GameObject room;
- public Vector3 offset;
- List<Cell> board;
- void Start()
- {
- MazeGenerator();
- }
- void GenerateDungeon()
- {
- for (int x = 0; x < size.x; x++)
- {
- for (int y = 0; y < size.y; y++)
- {
- for (int z = 0; z < size.z; z++)
- {
- int index = x + y * (int)size.x + z * (int)(size.x * size.y);
- Cell currentCell = board[index];
- if (currentCell.visited)
- {
- var newRoom = Instantiate(room, new Vector3(x * offset.x, y * offset.y, z * offset.z), Quaternion.identity, transform).GetComponent<RoomBehavior>();
- newRoom.UpdateRoom(currentCell.status);
- newRoom.name = $"Room {x}-{y}-{z}";
- Debug.Log($"Placed Room {x}-{y}-{z} with doors: {string.Join(",", currentCell.status)}");
- }
- }
- }
- }
- }
- void MazeGenerator()
- {
- board = new List<Cell>();
- for (int i = 0; i < size.x * size.y * size.z; i++)
- {
- board.Add(new Cell());
- }
- int currentCell = startPos;
- Stack<int> path = new Stack<int>();
- int k = 0;
- while (k < 1000)
- {
- k++;
- board[currentCell].visited = true;
- if (currentCell == board.Count - 1) break;
- List<int> neighbors = CheckNeighbors(currentCell);
- if (neighbors.Count == 0)
- {
- if (path.Count == 0) break;
- currentCell = path.Pop();
- }
- else
- {
- path.Push(currentCell);
- int newCell = neighbors[Random.Range(0, neighbors.Count)];
- // Ensure bidirectional connections
- int diff = newCell - currentCell;
- if (diff == 1)
- {
- board[currentCell].status[2] = true;
- board[newCell].status[3] = true;
- }
- else if (diff == -1)
- {
- board[currentCell].status[3] = true;
- board[newCell].status[2] = true;
- }
- else if (diff == size.x)
- {
- board[currentCell].status[4] = true;
- board[newCell].status[5] = true;
- }
- else if (diff == -size.x)
- {
- board[currentCell].status[5] = true;
- board[newCell].status[4] = true;
- }
- else if (diff == size.x * size.y)
- {
- board[currentCell].status[1] = true;
- board[newCell].status[0] = true;
- }
- else if (diff == -size.x * size.y)
- {
- board[currentCell].status[0] = true;
- board[newCell].status[1] = true;
- }
- Debug.Log($"Connecting cell {currentCell} to {newCell}, doors: {string.Join(",", board[currentCell].status)}");
- currentCell = newCell;
- }
- }
- Debug.Log("Generating dungeon");
- GenerateDungeon();
- }
- List<int> CheckNeighbors(int cell)
- {
- List<int> neighbors = new List<int>();
- int x = cell % (int)size.x;
- int y = (cell / (int)size.x) % (int)size.y;
- int z = cell / (int)(size.x * size.y);
- if (x + 1 < size.x && !board[cell + 1].visited) neighbors.Add(cell + 1);
- if (x - 1 >= 0 && !board[cell - 1].visited) neighbors.Add(cell - 1);
- if (y + 1 < size.y && !board[cell + (int)size.x].visited) neighbors.Add(cell + (int)size.x);
- if (y - 1 >= 0 && !board[cell - (int)size.x].visited) neighbors.Add(cell - (int)size.x);
- if (z + 1 < size.z && !board[cell + (int)(size.x * size.y)].visited) neighbors.Add(cell + (int)(size.x * size.y));
- if (z - 1 >= 0 && !board[cell - (int)(size.x * size.y)].visited) neighbors.Add(cell - (int)(size.x * size.y));
- return neighbors;
- }
- }
- --- SCRIPT BREAK ---
- using UnityEngine;
- public class RoomBehavior : MonoBehaviour
- {
- public GameObject[] walls; // Order: [North, South, East, West, Up, Down]
- public GameObject[] doors; // Order: [North, South, East, West, Up, Down]
- public void UpdateRoom(bool[] status)
- {
- for (int i = 0; i < status.Length; i++)
- {
- doors[i].SetActive(status[i]); // Activate doors where needed
- walls[i].SetActive(!status[i]); // Deactivate walls if there's a door
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement