public static Vector3Int GetEnterPosition(this RoomConnectorBehavior door, Vector3 destination) { Vector3 p = door.transform.localPosition + destination; p += door.Direction == Direction.N ? new Vector3(0, -0.5f, 0) : door.Direction == Direction.E ? new Vector3(-0.5f, 0, 0) : door.Direction == Direction.S ? new Vector3(0, 0.5f, 0) : door.Direction == Direction.W ? new Vector3(0.5f, 0, 0) : new Vector3(0, 0, 0); return(Vector3Int.RoundToInt(p)); }
public bool GrowDungeon(RoomConnectorBehavior door, IEnumerable <GameObject> rooms, int maxRoomConnections = int.MaxValue) { var matchingConnectors = rooms.SelectMany(r => { return(r .GetComponentsInChildren <RoomConnectorBehavior>() .Where(c => c.Direction == (Direction)(((int)door.Direction + 2) % 4)) .Where(c => c.Tag == door.Tag)); }).ToList(); var originalMatchingConnetors = matchingConnectors.ToArray(); GameObject nextRoom = null; while (nextRoom == null && matchingConnectors.Any()) { var selectedConnector = matchingConnectors[this.rand.Next(matchingConnectors.Count())]; var s = selectedConnector.transform.parent.gameObject; var d = door.transform.position - selectedConnector.transform.localPosition; if (this.CheckOccupancy(s, d, maxRoomConnections)) { nextRoom = GameObject.Instantiate(s); nextRoom.SetActive(true); nextRoom.transform.position = d; nextRoom.transform.parent = this.Level.transform; var newDoors = nextRoom.GetComponentsInChildren <RoomConnectorBehavior>().ToList(); this.UpdateOpenDoors(newDoors); this.Occupy(s, d); } else { matchingConnectors.Remove(selectedConnector); } } if (nextRoom == null) { door.transform.localScale = new Vector3(2, 2, 2); foreach (var missedConnector in originalMatchingConnetors) { var d = door.transform.position - missedConnector.transform.localPosition; var p = missedConnector.GetExitPosition(d); this.misses.Add(p); } return(false); } return(true); }