private PathNode getPathNodeOfNearest(int startX, int startY, IGridObjectType gridObjectType) { PathNode startNode = grid.GetGridObject(startX, startY); if (startNode == null) { // Invalid Path return(null); } openList = new List <PathNode> { startNode }; closedList = new List <PathNode>(); while (openList.Count > 0) { PathNode currentNode = GetLowestFCostNode(openList); openList.Remove(currentNode); closedList.Add(currentNode); if (currentNode.referenceToObjects.Exists(referenceToObject => referenceToObject.gridObjectType.Equals(gridObjectType))) { return(currentNode); } foreach (PathNode neighbourNode in GetNeighbourList(currentNode)) { if (closedList.Contains(neighbourNode)) { continue; } if (!neighbourNode.isWalkable && !neighbourNode.referenceToObjects.Exists(referenceToObject => referenceToObject.gridObjectType.Equals(gridObjectType))) { closedList.Add(neighbourNode); continue; } if (!openList.Contains(neighbourNode)) { openList.Add(neighbourNode); } } } // Out of nodes on the openList return(null); }
public static PathInProgress setPathToNearest(int2 start, IGridObjectType gridObjectType, Pathfinding pathfinding) { List <Vector3> pathVectorList; MonoBehaviour objectController; pathfinding.FindPathToNearest(start.x, start.y, gridObjectType, out pathVectorList, out objectController); if (pathVectorList != null && pathVectorList.Count > 1) { pathVectorList.RemoveAt(0); } return(new PathInProgress(0, pathVectorList, objectController)); }
public void FindPathToNearest(int startX, int startY, IGridObjectType gridObjectType, out List <Vector3> vectorPath, out MonoBehaviour monoBehaviour) { var pathNodeOfNearest = getPathNodeOfNearest(startX, startY, gridObjectType); if (pathNodeOfNearest != null) { vectorPath = FindPath(startX, startY, pathNodeOfNearest.x, pathNodeOfNearest.y); monoBehaviour = pathNodeOfNearest.referenceToObjects .Find(referenceToObject => referenceToObject.gridObjectType.Equals(gridObjectType)).objectController; } else { vectorPath = null; monoBehaviour = null; } }
public PathInProgress calculatePathToNearest(int2 start, IGridObjectType gridObjectType) { var pathfinding = new Pathfinding(width, height, cellSize, grid); return(PathInProgress.setPathToNearest(start, gridObjectType, pathfinding)); }
public MonoBehaviour getIfInRange(IGridObjectType gridObject) { var pathInProgress = goToNearest(gridObject); return(isInRange(pathInProgress) ? pathInProgress.destinationobj : null); }
public PathInProgress goToNearest(IGridObjectType gridObjectType) { var start = getPosition(); return(gameSceneController.grid.calculatePathToNearest(start, gridObjectType)); }
public ReferenceToObject(IGridObjectType gridObjectType, MonoBehaviour objectController) { this.gridObjectType = gridObjectType; this.objectController = objectController; }