Exemplo n.º 1
0
        /// <summary>
        /// Primary generation method. Summary:
        ///
        /// 1. Gather feature list (stubbed for now)
        /// 2. Generate starting room
        /// 3. If ending condition is met, goto 10. Else continue.
        /// 4. Select next Feature.
        /// 5. Select next position.
        /// 6. Compute all valid configurations.
        /// 7. Select next configuration.
        /// 8. Add all the Sections of the configured Feature.
        /// 9. Goto 3.
        /// 10. Done.
        /// </summary>
        private void Generate()
        {
            // Temporary: feature list
            Feature basicFeature = new Feature();

            // TODO I don't think this works properly with connections specified.
            basicFeature.Add(new Vector2Int(0, 0), Connection.All, externalConnections: Connection.All);

            Feature[] featureList = { basicFeature };

            // Initialize with a single starting room
            // Later, this can be e.g. a specific starting chamber.
            grid[0, 0] = Section.Default();

            // Pick some ending condition. For now it's just "try 100 times".
            int count = 0;

            while (count++ < 100)
            {
                Feature nextFeature = featureSelector.Select(featureList);
                if (nextFeature == null)
                {
                    Debug.Log("Failed to get valid Feature.");
                    continue;
                }

                Vector2Int nextPosition = positionSelector.Select(new ReadOnlyGrid(grid));

                // Get all valid placements
                List <Feature.Configuration> configurations = nextFeature.CanConnect(new ReadOnlyGrid(grid), nextPosition);

                if (configurations.Count == 0)
                {
                    Debug.Log("Failed to find valid configuration at position: " + nextPosition);
                    continue;
                }

                // Choose one of those placements
                Feature.Configuration configuration = configurationSelector.Select(configurations.ToArray());

                // Add that Feature in
                Dictionary <Vector2Int, Section> toAdd = nextFeature.GetConfiguration(configuration);
                foreach (KeyValuePair <Vector2Int, Section> pair in toAdd)
                {
                    grid[pair.Key] = pair.Value;
                }
            }
        }
Exemplo n.º 2
0
        // Visually appealing tile placement strategy.
        // TODO this will probably be removed.
        private IEnumerator CoolPlaceTiles(Feature feature, List <Feature.Configuration> configs)
        {
            yield return(new WaitForSeconds(0.5f));

            int count = 0;

            foreach (Feature.Configuration configuration in configs)
            {
                Debug.Log($"Trying configuration {count++}.");
                // Clear all gameobjects (inefficiently, but just for the demo)
                Tile[] tiles = GameObject.FindObjectsOfType <Tile>();
                for (int i = 0; i < tiles.Length; i++)
                {
                    GameObject.DestroyImmediate(tiles[i].gameObject);
                }

                // Make a new empty grid
                grid = new Grid();
                grid[new Vector2Int(0, 0)] = Section.Default();
                grid[new Vector2Int(1, 0)] = Section.Default();
                grid[new Vector2Int(0, 1)] = Section.Default();
                grid[new Vector2Int(1, 1)] = Section.Default();

                // Place the configuration
                foreach (KeyValuePair <Vector2Int, Section> keyValuePair in feature.GetConfiguration(configuration))
                {
                    grid[keyValuePair.Key] = keyValuePair.Value;
                }

                // Resolve to physical
                PlaceTiles();

                // Wait
                yield return(new WaitForSeconds(Mathf.Max(0.1f, (10 - count) / 10f)));
            }
        }