Beispiel #1
0
        private List<Point> findFinalPath(SearchNode startNode, SearchNode endNode)
        {
            closedList.Add(endNode);

            SearchNode parentNode = endNode.Parent;

            while (parentNode != startNode)
            {
                closedList.Add(parentNode);
                parentNode = parentNode.Parent;
            }

            List<Point> finalPath = new List<Point>();

            for (int i = closedList.Count - 1; i >= 0; --i)
            {
                finalPath.Add(closedList[i].Position);
            }

            return finalPath;
        }
Beispiel #2
0
        public void InitializeSearchNodes(TileLayer layer)
        {
            searchNodes = new SearchNode[levelWidth, levelHeight];

            for (int x = 0; x < levelWidth; ++x)
            {
                for (int y = 0; y < levelHeight; ++y)
                {
                    SearchNode node = new SearchNode();

                    node.Position = new Point(x, y);

                    node.Passable = layer.IsPassable(x, y);

                    if (node.Passable)
                    {
                        node.Neighbors = new SearchNode[4];

                        searchNodes[x, y] = node;
                    }
                }
            }

            for (int x = 0; x < levelWidth; ++x)
            {
                for (int y = 0; y < levelHeight; ++y)
                {
                    SearchNode node = searchNodes[x, y];

                    if (node == null || !node.Passable)
                        continue;

                    Point[] neighbors = new Point[]
                    {
                        new Point(x, y - 1),
                        new Point(x, y + 1),
                        new Point(x - 1, y),
                        new Point(x + 1, y)
                    };

                    for (int i = 0; i < neighbors.Length; ++i)
                    {
                        Point position = neighbors[i];

                        if (position.X < 0 || position.X >= levelWidth || position.Y < 0 || position.Y >= levelHeight)
                            continue;

                        SearchNode neighbor = searchNodes[position.X, position.Y];

                        if (neighbor == null || !neighbor.Passable)
                            continue;

                        node.Neighbors[i] = neighbor;
                    }
                }
            }
        }