// public List<Pathfinding> FindPath(string destName, int destId, string regionName, PlayerSetup.Player mob, Room.Room room) // { ////starting path? // var list = new List<Pathfinding>(); // var prevStep = mob.AreaId; // var areass = Areas.ListOfRooms().OrderByDescending(i => i.areaId == mob.AreaId).ToList(); // foreach (var searchRoom in areass) // foreach (var exit in searchRoom.exits.OrderBy(x => Helpers.Rand(0, searchRoom.exits.Count)).ToList()) // { // if (prevStep == exit.areaId) // { // continue; // } // if (list.Count(x => x.AreaId == exit.areaId) == 1) // { // continue; // } // prevStep = exit.areaId; // var path = new Pathfinding() // { // Name = exit.name, // AreaId = exit.areaId // }; // list.Add(path); // if (exit.areaId == destId) // { // return list; // } // var anExit = SearchRooms(destId, exit, searchRoom); // foreach (var ae in anExit) // { // if (ae.Key != null) // { // if (prevStep == ae.Value) // { // continue; // } // if (list.Count(x => x.AreaId == ae.Value) == 1) // { // continue; // } // prevStep = ae.Value; // var path1 = new Pathfinding() // { // Name = ae.Key, // AreaId = ae.Value // }; // list.Add(path1); // } // } // } // return list; // } public Dictionary <string, int> SearchRooms(int destId, Room.Exit exit, Room.Room room) { var newExit = new Dictionary <string, int>(); var getNewRoom = Areas.ListOfRooms().FirstOrDefault(x => x.area == exit.area && x.areaId == exit.areaId); if (getNewRoom != null) { foreach (var newRoomExit in getNewRoom.exits.OrderBy(x => Helpers.Rand(0, room.exits.Count)).ToList()) { if (newRoomExit.areaId == destId) { newExit.Add(newRoomExit.name, newRoomExit.areaId); return(newExit); } newExit.Add(newRoomExit.name, newRoomExit.areaId); return(newExit); } } return(null); }
void GenerateRoom(int x, int y, string prevDir, bool entrance = false) { Vector3 position; GameObject room; List <GameObject> possibleRooms; if (x < dungeon.Length && y < dungeon[0].Length && x >= 0 && y >= 0 && dungeon[x][y] == null) { // Just setting variables to place the chunk position.x = x * DefaultRoomWidth; position.y = y * DefaultRoomHeight; position.z = 0; // Checking all rooms that match our previous room possibleRooms = rooms.FindAll(a => a.GetComponent <Room>().exits.Exists(b => b.key == prevDir && ((b.state && b.forceState) || (!b.forceState)))); bool state; if (x == 0) { possibleRooms = possibleRooms.FindAll(a => a.GetComponent <Room>().exits.Exists(b => b.key == "Left" && ((!b.state && b.forceState) || (!b.forceState)))); } else if (x == dungeon.Length - 1) { possibleRooms = possibleRooms.FindAll(a => a.GetComponent <Room>().exits.Exists(b => b.key == "Right" && ((!b.state && b.forceState) || (!b.forceState)))); } else if (dungeon[x + 1][y]) { state = (dungeon[x + 1][y] && dungeon[x + 1][y].exits.Find(a => a.key == "Left").state); possibleRooms = possibleRooms.FindAll(a => a.GetComponent <Room>().exits.Exists(b => b.key == "Right" && ((b.state == state && b.forceState) || (!b.forceState)))); } else if (dungeon[x - 1][y]) { state = (dungeon[x - 1][y] && dungeon[x - 1][y].exits.Find(a => a.key == "Right").state); possibleRooms = possibleRooms.FindAll(a => a.GetComponent <Room>().exits.Exists(b => b.key == "Left" && ((b.state == state && b.forceState) || (!b.forceState)))); } if (y == 0) { possibleRooms = possibleRooms.FindAll(a => a.GetComponent <Room>().exits.Exists(b => b.key == "Bottom" && ((!b.state && b.forceState) || (!b.forceState)))); } else if (y == dungeon[0].Length - 1) { possibleRooms = possibleRooms.FindAll(a => a.GetComponent <Room>().exits.Exists(b => b.key == "Top" && ((!b.state && b.forceState) || (!b.forceState)))); } else if (dungeon[x][y - 1]) { state = (dungeon[x][y - 1] && dungeon[x][y - 1].exits.Find(a => a.key == "Top").state); possibleRooms = possibleRooms.FindAll(a => a.GetComponent <Room>().exits.Exists(b => b.key == "Bottom" && ((b.state == state && b.forceState) || (!b.forceState)))); } else if (dungeon[x][y + 1]) { state = (dungeon[x][y + 1] && dungeon[x][y + 1].exits.Find(a => a.key == "Bottom").state); possibleRooms = possibleRooms.FindAll(a => a.GetComponent <Room>().exits.Exists(b => b.key == "Top" && ((b.state == state && b.forceState) || (!b.forceState)))); } if (!entrance) { room = (GameObject)Instantiate(possibleRooms[Random.Range(0, possibleRooms.Count)], position, Quaternion.Euler(Vector3.zero)); } else { room = (GameObject)Instantiate(Entrance, position, Quaternion.Euler(Vector3.zero)); } if (prevDir != null) { room.GetComponent <Room>().exits.Find(a => a.key == prevDir).state = true; room.GetComponent <Room>().exits.Find(a => a.key == prevDir).forceState = true; } Room.Exit exit = room.GetComponent <Room>().exits.Find(a => a.key == "Left"); bool roomExists = x > 0 && dungeon[x - 1][y]; bool forceState = (x == 0 || roomExists); bool open = (x == 0 ? false : (roomExists ? dungeon[x - 1][y].exits.Find(a => a.key == "Right").state : false)); if (forceState && exit.forceState != true) { exit.state = open; exit.forceState = forceState; } exit = room.GetComponent <Room>().exits.Find(a => a.key == "Right"); roomExists = x < dungeon.Length - 1 && dungeon[x + 1][y]; forceState = (x == dungeon.Length - 1 || roomExists); open = (x == dungeon.Length - 1 ? false : (roomExists ? dungeon[x + 1][y].exits.Find(a => a.key == "Left").state : false)); if (forceState && exit.forceState != true) { exit.state = open; exit.forceState = forceState; } exit = room.GetComponent <Room>().exits.Find(a => a.key == "Bottom"); roomExists = y > 0 && dungeon[x][y - 1]; forceState = (y == 0 || roomExists); open = (y == 0 ? false : (roomExists ? dungeon[x][y - 1].exits.Find(a => a.key == "Top").state : false)); if (forceState && exit.forceState != true) { exit.state = open; exit.forceState = forceState; } exit = room.GetComponent <Room>().exits.Find(a => a.key == "Top"); roomExists = y < dungeon[0].Length - 1 && dungeon[x][y + 1]; forceState = (y == dungeon[0].Length - 1 || roomExists); open = (y == dungeon[0].Length - 1 ? false : (roomExists ? dungeon[x][y + 1].exits.Find(a => a.key == "Bottom").state : false)); if (forceState && exit.forceState != true) { exit.state = open; exit.forceState = forceState; } room.GetComponent <Room>().Init(); dungeon[x][y] = room.GetComponent <Room>(); if (dungeon[x][y].HasExit("Top")) { GenerateRoom(x, y + 1, "Bottom"); } if (dungeon[x][y].HasExit("Bottom")) { GenerateRoom(x, y - 1, "Top"); } if (dungeon[x][y].HasExit("Left")) { GenerateRoom(x - 1, y, "Right"); } if (dungeon[x][y].HasExit("Right")) { GenerateRoom(x + 1, y, "Left"); } } }