-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapFactory.cs
More file actions
52 lines (45 loc) · 1.8 KB
/
MapFactory.cs
File metadata and controls
52 lines (45 loc) · 1.8 KB
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
using System.Collections.Immutable;
using ThreeDSpectreMaze.Algorithms;
namespace ThreeDSpectreMaze;
public enum Block { Solid, Empty }
public static class MapFactory
{
const int Width = 17;
const int Height = 17;
public static ImmutableArray<ImmutableArray<Block>> Create(
Func<int, int, Action<Direction[,]>?, Direction[,]> algorithm,
Action<Direction[,]>? observer = null)
{
var directionalMap = algorithm(Width, Height, observer);
var gridMap = TransformToGrid(directionalMap);
return gridMap;
}
public static ImmutableArray<ImmutableArray<Block>> TransformToGrid(Direction[,] map)
{
var topRow = Enumerable.Repeat(Block.Solid, Width * 2 + 1).ToList();
var rows = new List<List<Block>>();
rows.Add(topRow);
for (var y = 0; y < Height; y++)
{
var row1 = new List<Block>();
var row2 = new List<Block>();
// our rows always start with a wall otherwise the maze would be open
// because we are checking eastwards we don't need to terminate with a
// wall, the maze generation algorithm itself terminates and all last columns
// will have no eastward openings
row1.Add(Block.Solid);
row2.Add(Block.Solid);
for (var x = 0; x < Width; x++)
{
row1.Add(Block.Empty);
row1.Add((map[y, x] & Direction.E) > 0 ? Block.Empty : Block.Solid);
row2.Add((map[y, x] & Direction.S) > 0 ? Block.Empty : Block.Solid);
row2.Add(Block.Solid);
}
rows.Add(row1);
rows.Add(row2);
}
var result = rows.Select(row => row.ToImmutableArray()).ToImmutableArray();
return result;
}
}