Example #1
0
        public override bool StartFindWay(Map map, out WayNode path)
        {
            _openList.Clear();
            _closeList.Clear();

            _map = map;

            var startNode = map.FindWayData.Entrance;
            var endNode   = map.FindWayData.Destination;

            _startPos = startNode.Pos;
            _endPos   = endNode.Pos;
            //起点终点不可达
            if (!startNode.Walkable || !endNode.Walkable)
            {
                return(base.StartFindWay(map, out path));
            }

            var node = new AStarWayNode(_startPos, new int2(-1, -1));

            node.CalcF(map);
            _openList.Add(node);

            while (_openList.Count > 0)
            {
                //获得F最小的点
                var minFNode = GetMinF();
                //从列表中移除
                _openList.Remove(minFNode);
                //加入关闭列表
                _closeList.Add(minFNode);

                var surroundPoints = GetSurround(map, minFNode);

                foreach (var surroundPoint in surroundPoints)
                {
                    var surround = (AStarWayNode)surroundPoint;
                    //在关闭列表中 丢弃
                    if (!_closeList.Contains(surround) && !_openList.Contains(surround))
                    {
                        surround.CalcF(map);
                        surround.Parent = minFNode.Current;
                        if (surround.Current.Equals(map.FindWayData.Destination.Pos))
                        {
                            path = surround;
                            return(true);
                        }
                        else
                        {
                            _openList.Add(surround);
                        }
                    }
                }
            }

            return(base.StartFindWay(map, out path));
        }