private void CreateAdjacentNodesConnections(Nav2dCell cell) { var cells = new Dictionary <string, Nav2dCell> { { "leftDown", FindNavCellAtPosition(cell.gridPos + new Vector3Int(-1, -1, 0)) }, { "leftTop", FindNavCellAtPosition(cell.gridPos + new Vector3Int(-1, 1, 0)) }, // left down { "left", FindNavCellAtPosition(cell.gridPos + new Vector3Int(-1, 0, 0)) }, // left { "top", FindNavCellAtPosition(cell.gridPos + new Vector3Int(0, 1, 0)) }, // top { "down", FindNavCellAtPosition(cell.gridPos + new Vector3Int(0, -1, 0)) }, // down { "right", FindNavCellAtPosition(cell.gridPos + new Vector3Int(1, 0, 0)) }, { "rightTop", FindNavCellAtPosition(cell.gridPos + new Vector3Int(1, 1, 0)) }, { "rightDown", FindNavCellAtPosition(cell.gridPos + new Vector3Int(1, -1, 0)) } }; // table of nodes to test connection Nav2dNode[] centerNodeNeighbors = new Nav2dNode[9] { cell.getNode(-1, -1), cells["left"]?.getNode(0, 0), cells["top"]?.getNode(-1, -1), cells["down"]?.getNode(0, 0), null, cells["top"]?.getNode(0, 0), cells["right"]?.getNode(-1, -1), cells["right"]?.getNode(0, 0), cells["rightTop"]?.getNode(-1, -1) }; //sets connection two ways cell.getNode(0, 0).setAccessibility(centerNodeNeighbors, LayerMask.GetMask(collisionLayers)); Nav2dNode[] northWestNodeNeighbors = new Nav2dNode[9] { cells["leftDown"]?.getNode(0, 0), cells["left"]?.getNode(-1, -1), cells["left"]?.getNode(0, 0), cells["down"]?.getNode(-1, -1), null, cells["top"]?.getNode(-1, -1), cells["down"]?.getNode(0, 0), cells["right"]?.getNode(-1, -1), cell.getNode(0, 0) }; //sets connection two ways cell.getNode(-1, -1).setAccessibility(northWestNodeNeighbors, LayerMask.GetMask(collisionLayers)); }
public Nav2dNode findClosestAccessibleNodeInGrid(Vector3Int cellPos, Vector3 targetPos) { Nav2dNode node = null; C5.HashSet <Vector3Int> explored = new C5.HashSet <Vector3Int>(); Queue <Vector3Int> opened = new Queue <Vector3Int>(); opened.Enqueue(cellPos); // dont try to long for (int i = 0; i < 20; i++) { Vector3Int current = opened.Dequeue(); Nav2dCell cell = FindNavCellAtPosition(current); node = cell?.findClosestAccessibleNodeInCell(targetPos); if (node != null) { break; } explored.Add(current); Vector3Int[] neighbors = { current + new Vector3Int(0, 1, 0), current + new Vector3Int(0, -1, 0), current + new Vector3Int(-1, 0, 0), current + new Vector3Int(1, 0, 0) }; foreach (var n in neighbors) { if (!explored.Contains(n)) { opened.Enqueue(n); } } } return(node); }