/// <summary> /// 使用二叉堆加速型的find /// </summary> public void findPath(Wrapper start, Wrapper end, TezBinaryHeap <Wrapper> openSet) { // Stopwatch stopwatch = new Stopwatch(); // stopwatch.Start(); this.saveWrapper(start); this.saveWrapper(end); if (end.isBlocked()) { this.onPathFindComplete(); onPathNotFound.Invoke(); return; } HashSet <Wrapper> close_set = createHashSet_Wrapper(); openSet.push(start); while (openSet.count > 0) { var current_node = openSet.pop(); if (current_node.Equals(end)) { // stopwatch.Stop(); // UnityEngine.Debug.Log(stopwatch.ElapsedMilliseconds + "ms"); this.retracePath(start, current_node); this.onPathFindComplete(openSet, close_set); return; } close_set.Add(current_node); var neighbours = this.calculateNeighbours(current_node); foreach (var neighbour in neighbours) { if (neighbour.isBlocked() || close_set.Contains(neighbour)) { continue; } var not_in_open = !openSet.contains(neighbour); int new_cost = current_node.gCost + this.calculateGPreCost(current_node, neighbour); if (new_cost < neighbour.gCost || not_in_open) { neighbour.gCost = new_cost; neighbour.hCost = this.calculateHCost(neighbour, end); neighbour.parent = current_node; if (not_in_open) { openSet.push(neighbour); } } } this.recycleNeighbourList(neighbours); } onPathNotFound.Invoke(); this.onPathFindComplete(openSet, close_set); }
protected virtual void onPathFindComplete(TezBinaryHeap <Wrapper> openSet, HashSet <Wrapper> closeSet) { this.onPathFindComplete(); openSet.clear(); closeSet.Clear(); recycleHashSet_Wrapper(closeSet); }