private void BuildPuzzle() { const int size = 5; tileGrid = RectGrid <SpriteCell> .Rectangle(size, size); blocksGrid = (RectGrid <Block>)tileGrid.CloneStructure <Block>(); map = new RectMap(new Vector2(200, 200)) .AnchorCellMiddleCenter() .WithWindow(ExampleUtils.ScreenRect) .AlignMiddleCenter(tileGrid) .To3DXY(); foreach (RectPoint point in tileGrid) { var cell = MakeCell(point); tileGrid[point] = cell; blocksGrid[point] = null; } blocks = new List <Block>(); AddFirstBlock(); AddOtherBlocks(); winPosition = new RectPoint(4, 2); }
public static IEnumerable <RectPoint> GenerateMazeWalls <TCell>(RectGrid <TCell> grid) { var walls = grid.CloneStructure <bool>(); //true indicates passable foreach (var point in walls) { walls[point] = point.GetColor3() == 0; //Debug.Log(point); } var wallList = new PointList <RectPoint>(); var newMaizePoint = grid.Where(p => p.GetColor3() == 0).RandomItem(); var inMaze = new PointList <RectPoint> { newMaizePoint }; var edges = grid.GetNeighbors(newMaizePoint); wallList.AddRange(edges); while (wallList.Any()) { var randomWall = wallList.RandomItem(); var faces = GetEdgeFaces(randomWall).Where(grid.Contains); //At least one of the two faces must be in the maze if (faces.Any(point => !inMaze.Contains(point))) { newMaizePoint = faces.First(point => !inMaze.Contains(point)); inMaze.Add(newMaizePoint); walls[randomWall] = true; yield return(randomWall); // Add all edges that are not passages edges = grid.GetNeighbors(newMaizePoint).Where(edge => !(walls[edge])); wallList.AddRange(edges); } else { wallList.Remove(randomWall); } } }
private void BuildPuzzle() { const int size = 5; tileGrid = RectGrid<SpriteCell>.Rectangle(size, size); blocksGrid = (RectGrid<Block>) tileGrid.CloneStructure<Block>(); map = new RectMap(new Vector2(152, 152)) .AnchorCellMiddleCenter() .WithWindow(ExampleUtils.ScreenRect) .AlignMiddleCenter(tileGrid) .To3DXY(); foreach (RectPoint point in tileGrid) { var cell = MakeCell(point); tileGrid[point] = cell; blocksGrid[point] = null; } blocks = new List<Block>(); AddFirstBlock(); AddOtherBlocks(); winPosition = new RectPoint(4, 2); }