Ejemplo n.º 1
0
        /// <summary>
        /// Creates a graph from the starting tile
        /// </summary>
        /// <param name="tile">The starting tile</param>
        private void CreateGraph(BaseTile tile)
        {
            if (tile == null)
            {
                return;
            }

            // A queue to house the tiles yet to be created
            Util.Datastructures.Queue <BaseTile> q = new Util.Datastructures.Queue <BaseTile>();

            q.Enqueue(tile);

            float diag = (float)Math.Sqrt(2);

            // While we have unvisited tiles
            while (!q.isEmpty)
            {
                // Get a tile
                BaseTile current = q.Dequeue();

                // Create a vertex
                current.CreateTileVertex();

                // Get all neighbours
                List <BaseTile> neighbours = GameWorld.instance.tiles.GetWalkableNeighbours(current);

                // For every tile, check if there already is an edge connecting the 2, if not, create it and add the neighbour to the queue
                foreach (BaseTile t in neighbours)
                {
                    if (current.HasAdjacent(t) || (current is TileRiver && t is TileRiver))
                    {
                        continue;
                    }
                    t.CreateTileVertex();
                    AddEdge(current, t, 1);
                    q.Enqueue(t);
                }

                if (current.allowDiagonal)
                {
                    neighbours = GameWorld.instance.tiles.GetWalkableDiagonalNeighbours(current);
                    foreach (BaseTile t in neighbours)
                    {
                        if (current.HasAdjacent(t) || !t.allowDiagonal)
                        {
                            continue;
                        }
                        t.CreateTileVertex();
                        AddEdge(current, t, diag);
                        q.Equals(t);
                    }
                }
            }
        }