private SplitRect SelectRandomNeighbour(SplitRect source, HashSet <SplitRect> closedList) { var neighbours = source.Neighbours.ToList(); var count = neighbours.Count; var startIndex = UnityEngine.Random.Range(0, count); for (int i = 0; i < neighbours.Count; ++i) { var subject = neighbours[(i + startIndex) % count]; if (!closedList.Contains(subject)) { var intersection = RectUtil.GetIntersection(source._rect, subject._rect); // need at least 3 units to build a door without artifacts (such as unreachable doors) if (Vector2Int.Distance(intersection[0], intersection[1]) >= 3) { return(subject); } } } return(null); }
private TraversalNode CreateNode(TraversalNode parent, SplitRect split, HashSet <SplitRect> closedList) { var result = new TraversalNode() { _split = split, _parent = parent, _children = new List <TraversalNode>(), _parentIntersection = parent == null ? null : RectUtil.GetIntersection(parent._split._rect, split._rect), _pathLength = parent == null ? 0 : parent._pathLength + Vector2.Distance(parent._split._rect.center, split._rect.center) }; if (_longestDistanceNode == null || result._pathLength > _longestDistanceNode._pathLength) { _longestDistanceNode = result; } closedList.Add(split); if (parent != null) { parent._children.Add(result); } return(result); }