示例#1
0
        public List <int> Result(Vector2 start, Vector2 end)
        {
            _start = start;
            _end   = end;

            _startNode = _sourceMap.PointInWhichPoly(start);
            _endNode   = _sourceMap.PointInWhichPoly(end);

            // 优化,如果在同一个多边形中,直接返回当前多边形
            var ret1 = new List <int>();

            if (_startNode == _endNode)
            {
                ret1.Add(_startNode);
                return(ret1);
            }

            // 优化,如果被已经储存的路径包含,从路径中截取
            foreach (var iter in _pathList)
            {
                List <int> wayPoints = new List <int>();
                if (BelongToPath(_startNode, _endNode, iter.Path, wayPoints))
                {
                    iter.Priority++;
                    _pathList.Sort();
                    return(wayPoints);
                }
                else
                {
                    iter.Priority--;
                }
            }


            search();

            Stack <int> path   = new Stack <int>();
            int         parent = _endNode;

            path.Push(parent);
            while (parent != _startNode)
            {
                parent = _route[parent];
                path.Push(parent);
            }

            // 反序列化
            List <int> ret = new List <int>();

            while (path.Count != 0)
            {
                int idx = path.Pop();
                ret.Add(idx);
            }

            // 优化,当前路径存入路径表中
            PriorityPath <int> pp = new PriorityPath <int>();

            pp.Path     = ret;
            pp.Priority = 1;

            if (_pathList.Count >= maxCachePathNum)
            {
                var mpp = _pathList[_pathList.Count - 1];
                if (mpp.Priority < 1)
                {
                    _pathList.RemoveAt(_pathList.Count - 1);
                }
            }
            _pathList.Add(pp);
            _pathList.Sort();

            return(ret);
        }
示例#2
0
        public int CompareTo(object obj)
        {
            PriorityPath <T> other = obj as PriorityPath <T>;

            return(this.Priority > other.Priority ? 1 : -1);
        }