Пример #1
0
        /// <summary>
        /// Rebuilds the List of neighbors for each node. Useful in case the map changes after it was originally loaded.
        /// </summary>
        /// <param name="map">TiledMap that you wish to pathfind on</param>
        public void RebuildNeighbors(TiledMap map)
        {
            // Remove any existing neighbors on each node
            for (int y = 0; y < map.txHeight; y++)
            {
                for (int x = 0; x < map.txWidth; x++)
                {
                    Nodes[x, y].Neighbors.Clear();
                }
            }


            // Setup new neighbors
            for (int y = 0; y < map.txHeight; y++)
            {
                for (int x = 0; x < map.txWidth; x++)
                {
                    Tile tile = map.GetTxTopMostTile(x, y);
                    Tile neighbor;

                    // If the tile at this location is impassable don't add any neighbors
                    if (tile == null || tile.HasProperty("Impassable"))
                    {
                        continue;
                    }

                    // Check each entry point to build a list of neighbors
                    List <string> entryPoints = new List <string>(tile.GetProperty("Entry", "Top Bottom Left Right").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
                    bool          left, right, up, down = false;

                    left  = entryPoints.Contains("Left");
                    right = entryPoints.Contains("Right");
                    up    = entryPoints.Contains("Top");
                    down  = entryPoints.Contains("Bottom");

                    // Ortho neighbors
                    if (left)
                    {
                        if (ValidPos(map, x - 1, y))
                        {
                            // Check to see if the neighbor is a valid link
                            neighbor = map.GetTxTopMostTile(x - 1, y);

                            if (!neighbor.HasProperty("Impassable") && HasEntry(neighbor, "Right"))
                            {
                                Nodes[x, y].Neighbors.Add(Nodes[x - 1, y]);
                            }
                            else
                            {
                                left = false; // left is not a valid neighbor, this rules out some diagonals
                            }
                        }
                    }
                    if (right)
                    {
                        if (ValidPos(map, x + 1, y))
                        {
                            neighbor = map.GetTxTopMostTile(x + 1, y);

                            if (!neighbor.HasProperty("Impassable") && HasEntry(neighbor, "Left"))
                            {
                                Nodes[x, y].Neighbors.Add(Nodes[x + 1, y]);
                            }
                            else
                            {
                                right = false;
                            }
                        }
                    }
                    if (up)
                    {
                        if (ValidPos(map, x, y - 1))
                        {
                            neighbor = map.GetTxTopMostTile(x, y - 1);

                            if (!neighbor.HasProperty("Impassable") && HasEntry(neighbor, "Bottom"))
                            {
                                Nodes[x, y].Neighbors.Add(Nodes[x, y - 1]);
                            }
                            else
                            {
                                up = false;
                            }
                        }
                    }
                    if (down)
                    {
                        if (ValidPos(map, x, y + 1))
                        {
                            neighbor = map.GetTxTopMostTile(x, y + 1);

                            if (!neighbor.HasProperty("Impassable") && HasEntry(neighbor, "Top"))
                            {
                                Nodes[x, y].Neighbors.Add(Nodes[x, y + 1]);
                            }
                            else
                            {
                                down = false;
                            }
                        }
                    }

                    // Diagonal neighbors
                    if (left && up)
                    {
                        if (ValidPos(map, x - 1, y - 1))
                        {
                            neighbor = map.GetTxTopMostTile(x - 1, y - 1);

                            if (!neighbor.HasProperty("Impassable") && HasEntry(neighbor, "Bottom") && HasEntry(neighbor, "Right"))
                            {
                                Nodes[x, y].Neighbors.Add(Nodes[x - 1, y - 1]);
                            }
                        }
                    }
                    if (right && up)
                    {
                        if (ValidPos(map, x + 1, y - 1))
                        {
                            neighbor = map.GetTxTopMostTile(x + 1, y - 1);

                            if (!neighbor.HasProperty("Impassable") && HasEntry(neighbor, "Bottom") && HasEntry(neighbor, "Left"))
                            {
                                Nodes[x, y].Neighbors.Add(Nodes[x + 1, y - 1]);
                            }
                        }
                    }
                    if (right && down)
                    {
                        if (ValidPos(map, x + 1, y + 1))
                        {
                            neighbor = map.GetTxTopMostTile(x + 1, y + 1);

                            if (!neighbor.HasProperty("Impassable") && HasEntry(neighbor, "Top") && HasEntry(neighbor, "Left"))
                            {
                                Nodes[x, y].Neighbors.Add(Nodes[x + 1, y + 1]);
                            }
                        }
                    }
                    if (left && down)
                    {
                        if (ValidPos(map, x - 1, y + 1))
                        {
                            neighbor = map.GetTxTopMostTile(x - 1, y + 1);

                            if (!neighbor.HasProperty("Impassable") && HasEntry(neighbor, "Top") && HasEntry(neighbor, "Right"))
                            {
                                Nodes[x, y].Neighbors.Add(Nodes[x - 1, y + 1]);
                            }
                        }
                    }
                }
            }
        }