Ejemplo n.º 1
0
    IEnumerator Generate()
    {
        generatedAreas = new List <AreaData>();

        // Select a random area to start with.
        int      randomAreaIndex = Random.Range(0, areaDatas.Length);
        AreaData randomArea      = areaDatas[randomAreaIndex];

        // Create a copy of the AreaData.
        AreaData newArea = new AreaData(randomArea.name, randomArea.availableTransitions);

        for (int i = 0; i < iterations; i++)
        {
            // Iterate through this area's transitions and add connections
            // however, we'll also need to check for existing areas blocking the way or that should be connected
            for (int j = 0; j < newArea.availableTransitions.Length; j++)
            {
                Direction transition = newArea.availableTransitions[j];

                if (transition == Direction.None)
                {
                    continue;
                }

                Direction opposite = transition.GetOpposite();

                Coordinates adjacentAreaCoordinate = newArea.coordinates.GetAdjacentCoordinate(transition);
                AreaData    adjacentArea           = GetGeneratedAreaByCoordinate(adjacentAreaCoordinate);

                // if there's an area in the way check if it has an available transition opposite of this transition.
                if (adjacentArea != null)
                {
                    if (!adjacentArea.GetIsTransitionAvailable(opposite))
                    {
                        // The adjecent area cannot be transitioned to from this area.
                        adjacentArea = null;

                        // We should actually now flag this direction as no longer viable.
                        newArea.availableTransitions[j] = Direction.None;
                    }
                }
                // otherwise create a new area
                else
                {
                    adjacentArea = CreateRandomAreaWithTransition(opposite);

                    if (adjacentArea == null)
                    {
                        Debug.LogErrorFormat(
                            "Could not GetRandomAreaWithTransition({0}). " +
                            "Please ensure areaDatas has available transitions on all sides",
                            opposite);
                    }
                    else
                    {
                        adjacentArea.coordinates = adjacentAreaCoordinate;
                        generatedAreas.Add(adjacentArea);
                    }
                }



                if (adjacentArea != null)
                {
                    // assign the connection between the two areas.
                    newArea.SetTransitionUsed(transition, adjacentArea, opposite);
                    adjacentArea.SetTransitionUsed(opposite, newArea, transition);

                    // check to see if we assigned any transitions to this new area, if so add it to the generatedAreas list.
                    if (newArea.GetTransitionCount() > 0)
                    {
                        if (!generatedAreas.Contains(newArea))
                        {
                            generatedAreas.Add(newArea);
                        }
                    }
                    // otherwise did something go wrong?
                    else
                    {
                        Debug.LogWarning("No transitions assigned to area: " + newArea.ToString());
                    }

                    yield return(CreateCubeRepresentation());
                }
            }



            // Now we need to get the next area to work on.
            newArea = null;
            foreach (var item in generatedAreas)
            {
                if (item.HasAnyAvailableTransition())
                {
                    newArea = item;
                    break;
                }
            }

            if (newArea == null)
            {
                Debug.Log("Can't find any generated areas with avilable transitions. Quitting.");
                break;
            }
        }
    }