Procedural Generation with Prefabs: Part 2
In the last article, we talked about the Grid Module portion of this system but today we are going to talk about the Floor Manager. This part of the system is currently acts as a hub for the Modules and is responsible for finding the next module to collapse. As you see in the above gif, the generation process works in from the corners and that is an effect of the Floor Manager’s decision process.
First, we start with initializing the grid and loading the modules with all the information they need. This includes creating and filling the local Grid array with new GridModules, assigning those modules with locations, and when the Grid is fully populated, assigning each Module it’s neighbors.
Similar to finding the nearest enemy, FindLowestEntropy() searches through the Modules that haven’t been collapsed to find the ones with the fewest possible tile options. And since the corner Modules are short two neighbors, they have about half the possible options of a fully surrounded Module. This returns as a Bool because it is controls the generation loop.
CollapseTile() is the meat of the system. After creating a list of Modules with the least valid options, it will randomly pick one (if there is more than one) and start the Collapse of the Tile. Two things to note, First, this does not need to be a coroutine instead of regular method with a while loop. Secondly, the commented code at the end of the method is the correct placement if you want to have the generation to ‘appear’ instantly. It is removed here to allow for the progression style seen in the gif above.
The MoveAndPopulate() in the GridModule Class is on the other side of the onCheck event but is included in the CollapseTile() to create the effect in the gif and can be commented to allow the Event to reveal all tiles at one time. Otherwise, CollapseTile calls the Collapse method in the chosen Module and moves if from the Open to the Closed Module list. At least one reference is maintained of each Module in case an impossible to ‘solve’ solution presents itself. This happens whenever a Module ends up with zero valid tiles. This does not happen with the example set of fifteen (15) tiles but if not all possible alignments are not accounted for, it can be a problem. It does occur, the system resets and tries again.