/// <summary> /// Check wether the dungeon can be completed. /// </summary> /// <param name="start">StartPoint gameobject.</param> /// <param name="end">EndPoint gameobject.</param> private void CheckPath(GameObject start, GameObject end) { StartingPoint startPoint = start.GetComponent <StartingPoint>(); EndingPoint endPoint = end.GetComponent <EndingPoint>(); RoomNode startRoom = startPoint.room; RoomNode endRoom = endPoint.room; int connections = 0; //Search for connections between the start and end point //Due to my lack of knowledge of AI in unity as well as issues with the visualisation I was unable to use //AI agnets to check if the path is able to be completed like I wanted foreach (Hallway hallway in hallwayList) { if (hallway.ConnectedRooms.Contains(startRoom) && hallway.ConnectedRooms.Contains(endRoom)) { connections++; Debug.Log("Connection found"); } } }
public override string ToString() { return($"Start: {StartingPoint.ToString()}, End: {EndingPoint.ToString()}"); }
/// <summary> /// Parses the dungeon.lua file in the current project directory /// </summary> /// <returns> A Map object representing the current dungeon.lua file contents </returns> public static Map ParseMapFile() { Char[] delimiters = { '=', ' ', ',', '"', '{', '}', '\r', '\n', '\t', ')', '(' }; Char[] delimitersAttributes = { '.', ':', ',', '"', '(', ')', '\r', '\n', '\t' }; Char[] delimitersAttributes2 = { '.', ':', ',', '"', '(', ')', '\r', '\n', '\t', ' ' }; Dictionary <string, MapElement> elements = new Dictionary <string, MapElement>(); Monster.MonsterType tmpMonstertype; Text.TextType tmpTextType; Door.DoorType tmpDoorType; Lever.LeverType tmpLeverType; Lock.LockType tmpLockType; ButtonE.ButtonType tmpButtonType; Alcove.AlcoveType tmpAlcoveType; PressurePlate.PressurePlateType tmpPressurePlateType; TrapDoor.TrapDoorType tmpTrapDoorType; TorchHolder.TorchHolderType tmpTorchHolderType; Lantern.LanternType tmpLanternType; Altar.AltarType tmpAltarType; WallEffect.WallEffectType tmpWallEffectType; Weapon.WeaponType tmpWeaponType; Armor.ArmorType tmpArmorType; Food.FoodType tmpFoodType; Potion.PotionType tmpPotionType; string fileText = System.IO.File.ReadAllText(DirectoryManager.DungeonFilePath); string patternSpawn = @"spawn\(.*\)"; string patternAttributes = @"\w+\.\w+\:\w+\(.*\)"; string patternLoadLayer = @"loadLayer\(([^)]+)\)"; string patternParameters = @"\w+ = .*,"; string patternTiles = @"tiles = {(\n|\t|[^{])*(?=})"; MatchCollection matchsSpawn = Regex.Matches(fileText, patternSpawn, RegexOptions.IgnoreCase); MatchCollection matchsAttribute = Regex.Matches(fileText, patternAttributes, RegexOptions.IgnoreCase); MatchCollection matchsParameters = Regex.Matches(fileText, patternParameters, RegexOptions.IgnoreCase); Match matchLayer = Regex.Match(fileText, patternLoadLayer, RegexOptions.IgnoreCase); Match matchTiles = Regex.Match(fileText, patternTiles, RegexOptions.IgnoreCase); string name = "", ambientTrack = ""; int width = 0, height = 0; int[] levelCoord = null; List <string> tiles = new List <string>(); List <Cell> cells = new List <Cell>(); List <EndingPoint> endingPoints = new List <EndingPoint>(); StartingPoint startingPoint = null; foreach (Match m in matchsParameters) { string[] split = m.Value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); if (split[0].Contains("name")) { name = split[1]; } else if (split[0].Contains("width")) { width = Convert.ToInt32(split[1]); } else if (split[0].Contains("height")) { height = Convert.ToInt32(split[1]); } else if (split[0].Contains("levelCoord")) { levelCoord = new int[] { Convert.ToInt32(split[1]), Convert.ToInt32(split[2]), Convert.ToInt32(split[3]) }; } else if (split[0].Contains("ambientTrack")) { ambientTrack = split[1]; } } string[] splitTiles = matchTiles.Value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); for (int i = 1; i < splitTiles.Length; i++) { tiles.Add(splitTiles[i]); } string[] splitLayer = matchLayer.Value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); for (int i = 2; i < splitLayer.Length; i++) { int x = (i - 2) % width; int y = (i - 2) / width; int type = Convert.ToInt32(splitLayer[i]); Cell c = new Cell(x, y, type); if (type == 1) { c.IsWalkable = true; } else if (type == 2) { c.IsWalkable = false; //FIXME: Se houverem mais tipos esta comparação pode não chegar. } cells.Add(c); } foreach (Match m in matchsSpawn) { string[] splitString = m.Value.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); string id = splitString[1]; int x = Convert.ToInt32(splitString[2]); int y = Convert.ToInt32(splitString[3]); int o = Convert.ToInt32(splitString[4]); int h = Convert.ToInt32(splitString[5]); string uniqueId = splitString[6]; Cell tmpCell = cells.Where(c => c.X == x && c.Y == y).First(); MapElement tmpElement = null; if (id.Contains("starting_location")) { startingPoint = new StartingPoint(id, x, y, o, h, uniqueId); tmpCell.IsStartingPoint = true; tmpCell.StartPoint = startingPoint; elements.Add(uniqueId, startingPoint); } else if (id.Contains("exit") || id.Contains("stairs") || id.Contains("healing_crystal")) { var newEndingPoint = new EndingPoint(id, x, y, o, h, uniqueId); tmpCell.IsEndingPoint = true; tmpCell.EndPoint = newEndingPoint; endingPoints.Add(newEndingPoint); elements.Add(uniqueId, newEndingPoint); } else if (Enum.TryParse(id, true, out tmpTorchHolderType)) { tmpElement = new TorchHolder(id, x, y, o, h, uniqueId); } else if (id.Equals("scroll")) { tmpElement = new Scroll(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpAlcoveType)) { tmpElement = new Alcove(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpButtonType)) { tmpElement = new ButtonE(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpDoorType)) { tmpElement = new Door(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpLeverType)) { tmpElement = new Lever(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpLockType)) { tmpElement = new Lock(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpMonstertype)) { var newMonster = new Monster(id, x, y, o, h, uniqueId); tmpCell.Monster = newMonster; elements.Add(uniqueId, newMonster); } else if (Enum.TryParse(id, true, out tmpPressurePlateType)) { tmpElement = new PressurePlate(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpTextType)) { tmpElement = new Text(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpTrapDoorType)) { tmpElement = new TrapDoor(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpLanternType)) { tmpElement = new Lantern(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpAltarType)) { tmpElement = new Altar(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpWallEffectType)) { tmpElement = new WallEffect(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpWeaponType)) { tmpElement = new Weapon(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpArmorType)) { tmpElement = new Armor(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpFoodType)) { tmpElement = new Food(id, x, y, o, h, uniqueId); } else if (Enum.TryParse(id, true, out tmpPotionType)) { tmpElement = new Potion(id, x, y, o, h, uniqueId); } else { tmpElement = new Item(id, x, y, o, h, uniqueId); } if (tmpElement != null) { tmpCell.AddElement(tmpElement); elements.Add(uniqueId, tmpElement); } } foreach (Match m in matchsAttribute) { Char[] delimit = delimitersAttributes; if (m.Value.Contains("addConnector")) { delimit = delimitersAttributes2; } string[] split = m.Value.Split(delimit, StringSplitOptions.RemoveEmptyEntries); MapElement tmpElement = null; if (elements.TryGetValue(split[0], out tmpElement)) { if (split[2].Contains("addConnector")) { tmpElement.addConnector(split[3], split[4], split[5]); } else if (split[2].Contains("addItem") || split[2].Contains("setWallText") || split[2].Contains("setOpenedBy") || split[2].Contains("setState") || split[2].Contains("setDoorState") || split[2].Contains("setScrollText")) { tmpElement.setAttribute(split[2], split[3]); } else if (split[2].Contains("disable")) { tmpElement.setAttribute(split[2], ""); } else // True or false { tmpElement.setAttribute(split[2], split[3].Contains("true") ? true : false); } } } Map map = new Map(name, width, height) { StartPoint = startingPoint, EndPointList = endingPoints, AmbientTrack = ambientTrack, LevelCoord = levelCoord, Cells = cells, Tiles = new ArrayList(tiles), //FIXME Elements = elements, }; CurrentMap = map; return(map); }