Пример #1
0
        public List <List <ANode> > UpdateGrid(GameOperator go)
        {
            int                  i, j;
            List <ANode>         listNode;
            List <List <ANode> > grid = new List <List <ANode> >();

            for (i = 0; i < RoomSetting.Instance.MapSize; i++)
            {
                listNode = new List <ANode>();
                for (j = 0; j < RoomSetting.Instance.MapSize; j++)
                {
                    if (!go.IsPossibleMove(i, j))
                    {
                        listNode.Add(new ANode(new Vector2(j, i), true));
                    }
                    else
                    {
                        listNode.Add(new ANode(new Vector2(j, i), false));
                    }
                }
                grid.Add(listNode);
            }

            return(grid);
        }
Пример #2
0
        public ANode FindPath(GameOperator go, Vector2 Start, Vector2 End)
        {
            Grid = UpdateGrid(go);
            ANode start = new ANode(new Vector2((int)(Start.X), (int)(Start.Y)), true);
            ANode end   = new ANode(new Vector2((int)(End.X), (int)(End.Y)), true);

            ANode res = null;

            List <ANode> Path       = new List <ANode>();
            List <ANode> OpenList   = new List <ANode>();
            List <ANode> ClosedList = new List <ANode>();
            List <ANode> neighbors;
            ANode        current = start;

            // add start node to Open List
            OpenList.Add(start);

            while (OpenList.Count != 0 && !ClosedList.Exists(x => x.Position == end.Position))
            {
                current = OpenList[0];
                OpenList.Remove(current);
                ClosedList.Add(current);
                neighbors = GetNeighborNodes(current);


                foreach (ANode aN in neighbors)
                {
                    if (!ClosedList.Contains(aN) && !aN.isWall)
                    {
                        if (!OpenList.Contains(aN))
                        {
                            if (aN.Position.X == end.Position.X && aN.Position.Y == end.Position.Y)
                            {
                                res = current;
                                break;
                            }
                            aN.Parent           = current;
                            aN.DistanceToTarget = Math.Abs(aN.Position.X - end.Position.X) + Math.Abs(aN.Position.Y - end.Position.Y);
                            aN.Cost             = 1 + aN.Parent.Cost;
                            OpenList.Add(aN);
                            OpenList = OpenList.OrderBy(node => node.F).ToList <ANode>();
                        }
                    }
                }
                if (res != null)
                {
                    break;
                }
            }

            // can't find destination
            if (res == null)
            {
                return(null);
            }

            while (res != null && res != start)
            {
                Path.Add(res);
                res = res.Parent;
            }

            if (Path.Count > 0)
            {
                return(Path[Path.Count - 1]);
            }
            return(null);
        }
Пример #3
0
 public Astar(GameOperator go)
 {
     Grid = UpdateGrid(go);
 }