-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinaryTreeMaze.cs
74 lines (65 loc) · 1.88 KB
/
BinaryTreeMaze.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BinaryTreeMaze : MonoBehaviour, RandomMaze
{
private int rows;
private int cols;
private Cell2D.direction ver;
private Cell2D.direction hor;
private Grid2D grid;
public BinaryTreeMaze(int rows, int cols, Cell2D.direction ver, Cell2D.direction hor)
{
this.rows = rows;
this.cols = cols;
this.ver = ver;
this.hor = hor;
this.grid = new Grid2D(rows, cols);
genMaze();
}
public BinaryTreeMaze(Grid2D grid, Cell2D.direction ver, Cell2D.direction hor)
{
this.rows = grid.getRows();
this.cols = grid.getCols();
this.ver = ver;
this.hor = hor;
this.grid = grid;
genMaze();
}
private void genMaze()
{
grid.genActiveCells();
ArrayList blueprint = grid.getActiveCells();
foreach (Cell2D cell in blueprint)
{
int row = cell.GetRow();
int col = cell.GetColumn();
Cell2D horVal = cell.getNeighbor(hor);
Cell2D verVal = cell.getNeighbor(ver);
List<Cell2D> neighbors = new List<Cell2D>();
int count = 0;
if (!(horVal is null))
{
if (horVal.IsActive()) { neighbors.Add(horVal); count++; }
}
if (!(verVal is null))
{
if (verVal.IsActive()) { neighbors.Add(verVal); count++; }
}
if (!(count == 0))
{
Cell2D neighbor = neighbors[Random.Range(0, count)];
cell.Link(neighbor);
}
}
}
public Grid getGrid() => this.grid;
public RenderMaze getRenderer()
{
return new RenderMaze2D(grid);
}
public RenderMaze getRenderer(Vector3 pos)
{
return new RenderMaze2D(grid, pos);
}
}