示例#1
0
        /// <summary>
        /// Constructor
        /// </summary>
        public Pathfinder(int[,] map, EntityManager manager)
        {
            mapWidth = map.GetLength(0);
            mapHeight = map.GetLength(1);

            System.Diagnostics.Debug.WriteLine(mapWidth.ToString() + ", " + mapHeight.ToString());

            InitSearchNodes(map, manager);

            // instead of units, i want to path with source grid and target grid
        }
示例#2
0
        /// <summary>
        /// This function initializes the map's search tiles
        /// Precondition: 
        /// </summary>
        private void InitSearchNodes(int[,] map, EntityManager manager)
        {
            searchNodes = new SearchNode[mapWidth, mapHeight];

            for (int x = 0; x < mapWidth; x++)
            {
                for (int y = 0; y < mapHeight; y++)
                {
                    SearchNode node = new SearchNode();

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

                    // heuristic for "is this node walkable?"

                    Grid grid = (Grid)manager.RetrieveEntity(map[x, y]);

                    if (grid.IsWalkable == true)
                    {
                        node.Walkable = true;
                    }

                    else
                    {
                        node.Walkable = false;
                    }

                    //node.Walkable = true;

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

                        searchNodes[x, y] = node;
                    }

                }

            }

            // Now, connect each search node to its neighbor

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

                    // Only look at the nodes that are walkable

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

                    Vector2[] neighbors = new Vector2[]
                    {
                        new Vector2(x, y - 1), // The node above the current node
                        new Vector2(x, y + 1), // The node below the current node
                        new Vector2(x - 1, y), // The node to the left of the current node
                        new Vector2(x + 1, y), // The node to the right of the current node
                    };

                    // Loop through all the neighbors

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

                        // Make sure this neighbor is part of the level
                        if (position.X < 0
                            || position.X > mapWidth - 1
                            || position.Y < 0
                            || position.Y > mapHeight - 1)
                        {
                            continue;
                        }

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

                        // Again, only care about the nodes that can be walked on

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

                        // And finally, store a reference to the neighbor itself

                        node.Neighbors[i] = neighbor;

                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// This function initializes the map's search tiles
        /// Precondition:
        /// </summary>
        private void InitSearchNodes(int[,] map, EntityManager manager)
        {
            searchNodes = new SearchNode[mapWidth, mapHeight];

            for (int x = 0; x < mapWidth; x++)
            {
                for (int y = 0; y < mapHeight; y++)
                {
                    SearchNode node = new SearchNode();

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

                    // heuristic for "is this node walkable?"

                    Grid grid = (Grid)manager.RetrieveEntity(map[x, y]);

                    if (grid.IsWalkable == true)
                    {
                        node.Walkable = true;
                    }

                    else
                    {
                        node.Walkable = false;
                    }

                    //node.Walkable = true;


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


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


            // Now, connect each search node to its neighbor

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


                    // Only look at the nodes that are walkable

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

                    Vector2[] neighbors = new Vector2[]
                    {
                        new Vector2(x, y - 1), // The node above the current node
                        new Vector2(x, y + 1), // The node below the current node
                        new Vector2(x - 1, y), // The node to the left of the current node
                        new Vector2(x + 1, y), // The node to the right of the current node
                    };

                    // Loop through all the neighbors

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

                        // Make sure this neighbor is part of the level
                        if (position.X < 0 ||
                            position.X > mapWidth - 1 ||
                            position.Y < 0 ||
                            position.Y > mapHeight - 1)
                        {
                            continue;
                        }


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

                        // Again, only care about the nodes that can be walked on

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


                        // And finally, store a reference to the neighbor itself

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