-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.cs
97 lines (82 loc) · 2.99 KB
/
Maze.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Maze : MonoBehaviour {
public IntVector2 size;
public MazeCell cellPrefab;
public float generationStepDelay;
public MazePassage passagePrefab;
public MazeWall wallPrefab;
private MazeCell[,] cells;
public IntVector2 RandomCoordinates {
get {
return new IntVector2(Random.Range(0, size.x), Random.Range(0, size.z));
}
}
public bool ContainsCoordinates (IntVector2 coordinate) {
return coordinate.x >= 0 && coordinate.x < size.x && coordinate.z >= 0 && coordinate.z < size.z;
}
public MazeCell GetCell (IntVector2 coordinates) {
return cells[coordinates.x, coordinates.z];
}
public IEnumerator Generate () {
WaitForSeconds delay = new WaitForSeconds(generationStepDelay);
cells = new MazeCell[size.x, size.z];
List<MazeCell> activeCells = new List<MazeCell>();
DoFirstGenerationStep(activeCells);
while (activeCells.Count > 0) {
yield return delay;
DoNextGenerationStep(activeCells);
}
}
private void DoFirstGenerationStep (List<MazeCell> activeCells) {
activeCells.Add(CreateCell(RandomCoordinates));
}
private void DoNextGenerationStep (List<MazeCell> activeCells) {
int currentIndex = activeCells.Count - 1;
MazeCell currentCell = activeCells[currentIndex];
if (currentCell.IsFullyInitialized) {
activeCells.RemoveAt(currentIndex);
return;
}
MazeDirection direction = currentCell.RandomUninitializedDirection;
IntVector2 coordinates = currentCell.coordinates + direction.ToIntVector2();
if (ContainsCoordinates(coordinates)) {
MazeCell neighbor = GetCell(coordinates);
if (neighbor == null) {
neighbor = CreateCell(coordinates);
CreatePassage(currentCell, neighbor, direction);
activeCells.Add(neighbor);
}
else {
CreateWall(currentCell, neighbor, direction);
}
}
else {
CreateWall(currentCell, null, direction);
}
}
private MazeCell CreateCell (IntVector2 coordinates) {
MazeCell newCell = Instantiate(cellPrefab) as MazeCell;
cells[coordinates.x, coordinates.z] = newCell;
newCell.coordinates = coordinates;
newCell.name = "Maze Cell " + coordinates.x + ", " + coordinates.z;
newCell.transform.parent = transform;
newCell.transform.localPosition = new Vector3(coordinates.x - size.x * 0.5f + 0.5f, 0f, coordinates.z - size.z * 0.5f + 0.5f);
return newCell;
}
private void CreatePassage (MazeCell cell, MazeCell otherCell, MazeDirection direction) {
MazePassage passage = Instantiate(passagePrefab) as MazePassage;
passage.Initialize(cell, otherCell, direction);
passage = Instantiate(passagePrefab) as MazePassage;
passage.Initialize(otherCell, cell, direction.GetOpposite());
}
private void CreateWall (MazeCell cell, MazeCell otherCell, MazeDirection direction) {
MazeWall wall = Instantiate(wallPrefab) as MazeWall;
wall.Initialize(cell, otherCell, direction);
if (otherCell != null) {
wall = Instantiate(wallPrefab) as MazeWall;
wall.Initialize(otherCell, cell, direction.GetOpposite());
}
}
}