Exemplo n.º 1
0
    private void GenerateMazePattern()
    {
        SortedDictionary <int, TileScript> mazePathsTo   = new SortedDictionary <int, TileScript>();
        SortedDictionary <int, TileScript> mazePathsFrom = new SortedDictionary <int, TileScript>();

        TileScript source = grid[0][0];

        source.Discovered = true;
        source.Origin     = source.ID;

        //add north and east connection
        mazePathsFrom.Add(source.connectionValues[0], source);
        mazePathsFrom.Add(source.connectionValues[1], source);
        mazePathsTo.Add(source.connectionValues[0], source.connections[0]);
        mazePathsTo.Add(source.connectionValues[1], source.connections[1]);

        int it = 0;

        while (mazePathsTo.Count != 0)
        {
            foreach (KeyValuePair <int, TileScript> kvp in mazePathsFrom)
            {
                Debug.Log("iteration " + it + "   key-" + kvp.Key + " " + kvp.Value.name + " -> " + mazePathsTo[kvp.Key]);
                it++;
            }
            Debug.Log("---");

            TileScript from  = null;
            TileScript to    = null;
            int        index = 0;

            foreach (KeyValuePair <int, TileScript> kvp in mazePathsFrom)
            {
                from  = kvp.Value;
                index = kvp.Key;
                break;
            }
            foreach (KeyValuePair <int, TileScript> kvp in mazePathsTo)
            {
                to = kvp.Value;
                break;
            }

            Debug.Log("OOOOO key-" + index + " " + from.name.ToString() + " -> " + to.name.ToString());

            mazePathsFrom.Remove(index);
            mazePathsTo.Remove(index);
            if (to.Discovered)
            {
            }
            else
            {
                to.Discovered = true;
                to.Origin     = from.ID;

                for (int i = 0; i < 4; i++)
                {
                    //"neighbour" would go out of bounds or is already closed off
                    if (to.connectionValues[i] == -1)
                    {
                    }
                    else
                    {
                        TileScript nb = to.connections[i];

                        //if not discovered, add the connection to the list
                        if (!nb.Discovered)
                        {
                            //add
                            int newpathvalue = to.connectionValues[i];
                            if (!mazePathsTo.ContainsKey(newpathvalue))
                            {
                                mazePathsTo.Add(newpathvalue, nb);
                                mazePathsFrom.Add(newpathvalue, to);
                            }
                            Debug.Log("discovered-" + to + "    new node-" + nb.name);
                        }
                        //else if discovered AND it is not your predecessor, block the path
                        //else if (nb.connections[i] != null)
                        //{
                        else if (nb.ID != to.Origin)
                        {
                            nb.BlockPath(to);
                            //blockpath
                            to.BlockPath(nb);
                        }
                        //}
                    }
                }
                Debug.Log("//---//");
            }
        }
    }