Ejemplo n.º 1
0
 private void RegisterEvents()
 {
     PathContext.RegisterButtonReaction(0.05f);
     PathContext.RegisterOnClick(OnClick);
     TreeRightArrow.RegisterButtonReaction(0.1f);
     TreeRightArrow.RegisterOnClick(TreeRightArrow_MouseDown);
 }
Ejemplo n.º 2
0
    //收集从起点到终点经过的所有层级路点,一旦遇见最近层级的终点就结束,用于计算最短路径.
    void CollectPathLayer(PathContext context, int start, int end, int layer = 1)
    {
        Dictionary <int, WayLength> ways = Main.Ins.CombatData.wayPoints[start].link;

        foreach (var each in ways)
        {
            if (!PathLayerExist(context, each.Key))
            {
                //之前的所有层次中并不包含此节点.
                PathNode no = context.Container[each.Key];
                no.wayPointIdx = each.Key;
                no.parent      = start;
                if (context.PathInfo.ContainsKey(layer))
                {
                    context.PathInfo[layer].Add(no);
                }
                else
                {
                    context.PathInfo.Add(layer, new List <PathNode> {
                        no
                    });
                }
            }
        }
    }
Ejemplo n.º 3
0
 //查看之前层级是否已统计过该节点信息
 bool PathLayerExist(PathContext context, int wayPoint)
 {
     foreach (var each in context.PathInfo)
     {
         for (int i = 0; i < each.Value.Count; i++)
         {
             if (each.Value[i].wayPointIdx == wayPoint)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Ejemplo n.º 4
0
    public void FindPath(PathContext context, Vector3 source, Vector3 target, List <WayPoint> waypoint)
    {
        int startPathIndex = GetWayIndex(source);
        int endPathIndex   = GetWayIndex(target);

        if (startPathIndex == -1 || endPathIndex == -1)
        {
            Debug.LogError("无法得到该位置所属路点");
            return;
        }
        context.looked.Clear();
        waypoint.Clear();
        FindPathCore(context, startPathIndex, endPathIndex, waypoint);
    }
Ejemplo n.º 5
0
    public List <Vector2> PathfindAstar(Vector2 start, Vector2 end, out double time)
    {
        System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
        Vector2Int startpos = ClampToGrid(start);
        Vector2Int endpos   = ClampToGrid(end);

        if (
            //!pathGrid.cells[startpos.x, startpos.y].traversable ||
            !pathGrid.cells[endpos.x, endpos.y].traversable)
        {
            Debug.LogWarning("End cell is invalid");
            time = 0.001d;
            return(new List <Vector2>()
            {
                end
            });
        }
        timer.Start();
        List <Vector2Int> gridPath = PathContext.FindPath(startpos, endpos);

        timer.Stop();
        if (curr.debugPaths)
        {
            Debug.Log(string.Format("Path found after {0} milliseconds with {1} nodes", timer.Elapsed.TotalMilliseconds, gridPath.Count));
        }
        List <Vector2> path      = new List <Vector2>();
        Vector2        direction = Vector2.zero;

        for (int i = 0; i < gridPath.Count - 1; i++)
        {
            Vector2Int intPos       = gridPath[i];
            Vector2Int intPosPrev   = gridPath[i + 1];
            Vector2    newDirection = pathGrid.cells[intPosPrev.x, intPosPrev.y].globalPos - pathGrid.cells[intPos.x, intPos.y].globalPos;
            if (direction != newDirection)
            {
                direction = newDirection;
                path.Add(pathGrid.cells[intPos.x, intPos.y].globalPos);
            }
        }
        path.Add(pathGrid.cells[gridPath[gridPath.Count - 1].x, gridPath[gridPath.Count - 1].y].globalPos);
        time = timer.Elapsed.TotalMilliseconds;
        return(path);
    }
Ejemplo n.º 6
0
 //从起点开始 构造寻路树.
 void CollectPathInfo(PathContext context, int start, int end, int layer = 1)
 {
     CollectPathLayer(context, start, end, layer);
     if (PathLayerExist(context, end))
     {
         return;
     }
     while (context.PathInfo.ContainsKey(layer))
     {
         int nextLayer = layer + 1;
         for (int i = 0; i < context.PathInfo[layer].Count; i++)
         {
             CollectPathLayer(context, context.PathInfo[layer][i].wayPointIdx, end, nextLayer);
             if (PathLayerExist(context, end))
             {
                 return;
             }
         }
         layer = nextLayer;
     }
 }
Ejemplo n.º 7
0
        public void ShouldUpdateMonitoredPathAndNotifyApplicationStartedWhenInitialized()
        {
            //GIVEN
            var    messages    = Substitute.For <DiagnosticMessages>();
            var    watchers    = Substitute.For <FileSystemWatcher>();
            var    presenter   = Substitute.For <ApplicationEventsPresenter>();
            var    states      = Any.Instance <PathStates>();
            string description = Any.String();

            var context = new PathContext(messages, watchers, presenter, states);

            watchers.Description().Returns(description);

            //WHEN
            context.Initialize();

            //THEN
            Received.InOrder(() =>
            {
                presenter.UpdateMonitoredPath(watchers.Description());
                messages.NotifyApplicationStarted();
            });
        }
Ejemplo n.º 8
0
        public static List <Vector2Int> FindPath(Vector2Int start, Vector2Int end)
        {
            List <Vector2Int> path = new List <Vector2Int>();

            PathContext context = new PathContext(curr.pathGrid, end);
            ActiveCell  c       = new ActiveCell(curr.pathGrid.cells[start.x, start.y]);

            c.gCost = 0;
            c.hCost = context.Distance(c.gridPos, end);
            context.Open.Add(c);

            while (context.Open.Count != 0)
            {
                // find lowest f-cost
                ActiveCell current = context.Open.RemoveFirst();
                context.Closed.Add(current);
                if (current.gridPos == end)
                {
                    if (curr.debugPaths)
                    {
                        Debug.Log(string.Format("Path from {0},{1} to {2},{3} complete", start.x, start.y, end.x, end.y));
                        Debug.Log("Path finished after " + context.Closed.Count + " finished cells and " + context.Open.Count + " started cells");
                    }
                    //thats the path
                    current.PathTrain(ref context.Closed, ref path);
                    return(path);
                }

                context.GetNeighbors(current);
            }
            //throw new Exception( "Impossible Path" );
            return(new List <Vector2Int>()
            {
                ClampToGrid(PlayerBaseClass.current.transform.position)
            });
        }
Ejemplo n.º 9
0
 public CountriesController(PathContext context)
 {
     _context = context;
 }
Ejemplo n.º 10
0
    public void FindPathCore(PathContext context, int start, int end, List <WayPoint> wp)
    {
        if (context.looked.Contains(start))
        {
            return;
        }

        context.looked.Add(start);
        if (start == -1 || end == -1)
        {
            return;
        }

        //路点相同,可直接走向终点
        if (start == end)
        {
            return;
        }

        //收集路径信息 层次
        context.ResetPath();
        PathNode no = context.Container[start];

        no.wayPointIdx = start;
        no.parent      = -1;
        context.PathInfo[0].Add(no);
        CollectPathInfo(context, start, end);
        int scan = 0;

        foreach (var each in context.PathInfo)
        {
            scan += each.Value.Count;
        }
        Debug.LogError("层信息:" + context.PathInfo.Count + " 总结点:" + scan);
        //计算最短路径.
        int  target = end;
        bool find   = false;

        foreach (var each in context.PathInfo)
        {
            for (int i = 0; i < each.Value.Count; i++)
            {
                if (each.Value[i].wayPointIdx == end)
                {
                    find = true;
                    //Debug.LogError("找到目标点:" + end);
                    if (wp.Count == 0)
                    {
                        wp.Add(Main.Ins.CombatData.wayPoints[target]);
                    }
                    else
                    {
                        wp.Insert(0, Main.Ins.CombatData.wayPoints[target]);
                    }
                    PathNode p = each.Value[i];
                    while (p.parent != start)
                    {
                        target = p.parent;
                        p      = context.Container[target];
                        if (wp.Count == 0)
                        {
                            wp.Add(Main.Ins.CombatData.wayPoints[target]);
                        }
                        else
                        {
                            wp.Insert(0, Main.Ins.CombatData.wayPoints[target]);
                        }
                        if (wp.Count >= 100)
                        {
                            Debug.LogError("寻路链路超过100!!!");
                            break;
                        }
                    }
                    break;
                }
            }
            if (find)
            {
                break;
            }
        }

        if (!find)
        {
            Debug.LogError(string.Format("孤立的寻路点:{0},没有点可以走向目标", target));
            if (wp.Count == 0)
            {
                wp.Add(Main.Ins.CombatData.wayPoints[target]);
            }
            else
            {
                wp.Insert(0, Main.Ins.CombatData.wayPoints[target]);
            }
        }
        wp.Insert(0, Main.Ins.CombatData.wayPoints[start]);
    }
Ejemplo n.º 11
0
 public void FindPath(PathContext context, int start, int end, List <WayPoint> wp)
 {
     context.looked.Clear();
     wp.Clear();
     FindPathCore(context, start, end, wp);
 }
 public PathContextTests()
 {
     _PathContext = new PathContext("root/p1/p2/p3/p4");
 }
Ejemplo n.º 13
0
 public PSMPath()
 {
     Context = new PathContext();
 }
 private static ApplicationUseCases ApplicationUseCases(DiagnosticMessages windowsDiagnosticMessages, PathContext pathContext)
 {
     return(new ApplicationUseCases(windowsDiagnosticMessages, pathContext));
 }