コード例 #1
0
        private static void InitializeSearchNodes(TileMap map)
        {
            searchNodes = new SearchNode[levelHeight, levelWidth];

            for (int x = 0; x < levelWidth; x++)
            {
                for (int y = 0; y < levelHeight; y++)
                {
                    SearchNode node = new SearchNode()
                    {
                        Position = new Point(x, y),
                        Walkable = !map.GetIsCollision(x, y)
                    };

                    if (node.Walkable)
                    {
                        node.Neighbors = new SearchNode[4];
                        searchNodes[y, x] = node;
                    }
                }
            }

            for (int x = 0; x < levelWidth; x++)
            {

                for (int y = 0; y < levelHeight; y++)
                {

                    SearchNode node = searchNodes[y, x];

                    // We only want to look at the nodes that

                    // our enemies can walk on.

                    if (node == null || node.Walkable == false)
                    {

                        continue;

                    }

                    // An array of all of the possible neighbors this

                    // node could have. (We will ignore diagonals for now.)

                    Point[] neighbors = new Point[]

            {

            new Point (x, y - 1), // The node above the current node

            new Point (x, y + 1), // The node below the current node.

            new Point (x - 1, y), // The node left of the current node.

            new Point (x + 1, y), // The node right of the current node

            };

                    // We loop through each of the possible neighbors

                    for (int i = 0; i < neighbors.Length; i++)
                    {

                        Point position = neighbors[i];

                        // We need to make sure this neighbour is part of the level.

                        if (position.X < 0 || position.X > levelWidth - 1 ||

                            position.Y < 0 || position.Y > levelHeight - 1)
                        {

                            continue;

                        }

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

                        // We will only bother keeping a reference

                        // to the nodes that can be walked on.

                        if (neighbor == null || neighbor.Walkable == false)
                        {
                            continue;
                        }

                        // Store a reference to the neighbor.
                        node.Neighbors[i] = neighbor;
                    }
                }
            }
        }
コード例 #2
0
        private static List<Vector2> FindFinalPath(SearchNode startNode, SearchNode endNode)
        {
            closedList.Add(endNode);

            SearchNode parentTile = endNode.Parent;

            // Trace back through the nodes using the parent fields
            // to find the best path.
            while (parentTile != startNode)
            {
                closedList.Add(parentTile);
                parentTile = parentTile.Parent;
            }

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

            // Reverse the path and transform into world space.
            for (int i = closedList.Count - 1; i >= 0; i--)
            {
                finalPath.Add(new Vector2(closedList[i].Position.X * 32,
                                          closedList[i].Position.Y * 32));
            }

            return finalPath;
        }