public void GenerateLabyrinth() { var random = new Random(); var currentCell = UnvisitedCells.ElementAt(random.Next(0, (Height / 2) * (Width / 2))); InitialPoint = currentCell; Maze.Add(currentCell); UnvisitedCells.Remove(currentCell); do { var currentNeighbours = GetUnvisitedNeighbours(currentCell); if (currentNeighbours.Any() && Maze.Contains(currentCell)) { var a = random.Next(0, currentNeighbours.Length); var nextCell = currentNeighbours[a]; var middleCell = new Cell( Math.Abs(currentCell.X + nextCell.X) / 2, Math.Abs(currentCell.Y + nextCell.Y) / 2, CellTypes.Gold); Maze.Add(middleCell); VisitingOrder.Push(currentCell); currentCell = nextCell; Maze.Add(currentCell); UnvisitedCells.Remove(currentCell); } else { if (EndPoint.Equals(new Cell())) { EndPoint = currentCell; } currentCell = VisitingOrder.Pop(); } } while (UnvisitedCells.Count != 0 || VisitingOrder.Count != 0); }
public static Maze Parse(string pathToMazeFile) { Maze maze = new Maze(); List <int> numbers = new List <int>(); ParsingStage parsingStage = ParsingStage.WidthAndHeight; int rowCount = 0; //Using File.ReadLines to keep memory consumption low if dealing with large files foreach (var line in File.ReadLines(pathToMazeFile)) { switch (parsingStage) { //Parse first line and update the width, height and name of the maze case ParsingStage.WidthAndHeight: maze.Label = pathToMazeFile; numbers = line.Split(' ').Select(Int32.Parse).ToList(); maze.Width = numbers[0]; maze.Height = numbers[1]; //Transtion stage parsingStage = ParsingStage.StartTile; break; //Parse second line and update maze starting position case ParsingStage.StartTile: numbers = line.Split(' ').Select(Int32.Parse).ToList(); maze.Start = new Point() { X = numbers[0], Y = numbers[1] }; //Transtion stage parsingStage = ParsingStage.EndTile; break; //Parse third line and update maze end position case ParsingStage.EndTile: numbers = line.Split(' ').Select(Int32.Parse).ToList(); maze.End = new Point() { X = numbers[0], Y = numbers[1] }; //Transtion stage parsingStage = ParsingStage.Grid; break; //Parse all remaining lines adding each to the maze grid in turn case ParsingStage.Grid: numbers = line.Split(' ').Select(Int32.Parse).ToList(); int colCount = 0; Point tile = new Point(); foreach (var number in numbers) { tile = new Point() { X = colCount, Y = rowCount }; maze.Add( tile, (TileKind)number); colCount++; } rowCount++; break; default: break; } } //Updatet the start and end tiles on the grid maze[maze.Start] = TileKind.PathStart; maze[maze.End] = TileKind.PathEnd; return(maze); }
static void XMLInfoEditor() { GUILayout.BeginHorizontal(); GUILayout.Label("MazeID"); maze.MazeID = GUILayout.TextField(maze.MazeID); GUILayout.Label("MazeName"); maze.MazeName = GUILayout.TextField(maze.MazeName); GUILayout.Label("BattleName"); maze.BattleName = GUILayout.TextField(maze.BattleName); GUILayout.Label("MazeStyle"); maze.MazeStyle = GUILayout.TextField(maze.MazeStyle); GUILayout.Label("MazeNpc"); maze.MazeNpc = GUILayout.TextField(maze.MazeNpc); GUILayout.EndHorizontal(); for (int i = 0; i < maze.zoneList.Count; i++) { GUILayout.BeginVertical(); ZoneScrollPos_ = GUILayout.BeginScrollView(ZoneScrollPos_, GUILayout.Width(200), GUILayout.Height(100)); GUILayout.Label("mfloor"); ((MazeZone)maze.zoneList[i]).Level = GUILayout.TextField(((MazeZone)maze.zoneList[i]).Level); GUILayout.Label("mEntrySceneID"); ((MazeZone)maze.zoneList[i]).EntrySceneID = GUILayout.TextField(((MazeZone)maze.zoneList[i]).EntrySceneID); GUILayout.Label("mEntryID"); ((MazeZone)maze.zoneList[i]).EntryID = GUILayout.TextField(((MazeZone)maze.zoneList[i]).EntryID); GUILayout.Label("MazeMonster"); ((MazeZone)maze.zoneList[i]).MazeMonster = GUILayout.TextField(((MazeZone)maze.zoneList[i]).MazeMonster); GUILayout.Label("MazeMonsterMaxNum"); ((MazeZone)maze.zoneList[i]).MazeMonsterMaxNum = GUILayout.TextField(((MazeZone)maze.zoneList[i]).MazeMonsterMaxNum); GUILayout.Label("MazeMonsterMinNum"); ((MazeZone)maze.zoneList[i]).MazeMonsterMinNum = GUILayout.TextField(((MazeZone)maze.zoneList[i]).MazeMonsterMinNum); GUILayout.Label("Rate"); ((MazeZone)maze.zoneList[i]).Rate = GUILayout.TextField(((MazeZone)maze.zoneList[i]).Rate); if (GUILayout.Button("Delete")) { maze.zoneList.RemoveAt(i); } GUILayout.EndScrollView(); GUILayout.EndVertical(); } GUILayout.BeginVertical(); if (GUILayout.Button("AddMzone")) { MazeZone mzone = new MazeZone(); mzone.EntrySceneID = sEntrySceneId; mzone.EntryID = sEntryId; mzone.Level = sfloor; mzone.MazeMonster = MazeMonster; mzone.MazeMonsterMaxNum = MazeMonsterMaxNum; mzone.MazeMonsterMinNum = MazeMonsterMinNum; mzone.Rate = rate; maze.Add(mzone); } if (GUILayout.Button("Save")) { CreateXml(maze); } GUILayout.EndVertical(); }