Skip to content

Commit 57d04bd

Browse files
committed
Added more code comments
1 parent 4c643fb commit 57d04bd

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

Assets/Scripts/Level Generation/Cell.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Cell : MonoBehaviour, IHeapItem<Cell>
1919
/// <summary>
2020
/// Was the module object already instantiated
2121
/// </summary>
22-
private bool isCellSet;
22+
private bool _isCellSet;
2323

2424
/// <summary>
2525
/// Holds the indices of the still possible modules
@@ -85,6 +85,7 @@ public void FilterCell(EdgeFilter edgeFilter)
8585
RemoveModule(removingModules[i]);
8686
}
8787

88+
// Check if the cell has only one possible module left
8889
CheckSetCell();
8990
}
9091

@@ -150,6 +151,10 @@ public void SimpleRemoveModule(int moduleIndex)
150151
LevelGenerator.Instance.OrderedCells.UpdateItem(this);
151152
}
152153

154+
/// <summary>
155+
/// Force assigns this cell one specific module and automatically removes all other possibilities.
156+
/// </summary>
157+
/// <param name="moduleIndex">The module to assign</param>
153158
public void SetSpecialModule(int moduleIndex)
154159
{
155160
possibleModulesIndices = new List<int> {moduleIndex};
@@ -181,9 +186,12 @@ public void SetSpecialModule(int moduleIndex)
181186
var newModule = Instantiate(module.moduleGO, transform.position,
182187
Quaternion.identity, transform);
183188

184-
isCellSet = true;
189+
_isCellSet = true;
185190
}
186191

192+
/// <summary>
193+
/// Checks if the cell is solved
194+
/// </summary>
187195
private void CheckSetCell()
188196
{
189197
// Only set cell if one final module is left
@@ -200,17 +208,17 @@ private void SetCell()
200208
{
201209
//Debug.Log("Set cell!", gameObject);
202210

203-
if (isCellSet) return;
211+
if (_isCellSet) return;
204212

205213
var newModule = Instantiate(ModuleManager.Instance.modules[possibleModulesIndices[0]].moduleGO,
206214
transform.position,
207215
Quaternion.identity, transform);
208216

209-
isCellSet = true;
217+
_isCellSet = true;
210218
}
211219

212220
/// <summary>
213-
/// Compares two cells.
221+
/// Compares two cells using their solved score
214222
/// </summary>
215223
/// <param name="other">Cell to compare</param>
216224
/// <returns></returns>

Assets/Scripts/Level Generation/Grid.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using UnityEngine;
5+
using UnityEngine.Serialization;
56

67
namespace LevelGeneration
78
{
@@ -28,7 +29,7 @@ public class Grid : MonoBehaviour
2829
/// <summary>
2930
/// Cells matrix ([width, height])
3031
/// </summary>
31-
public Cell[,] _cells;
32+
public Cell[,] cells;
3233

3334
/// <summary>
3435
/// <see cref="LevelGenerator"/>
@@ -47,15 +48,18 @@ private void Awake()
4748
GenerateGrid();
4849

4950
// Wave-function-collapse algorithm
50-
_levelGenerator.GenerateLevelWFC(ref _cells, seed != -1 ? seed : Environment.TickCount);
51+
_levelGenerator.GenerateLevelWFC(ref cells, seed != -1 ? seed : Environment.TickCount);
5152
}
5253

54+
/// <summary>
55+
/// Generates the two-dimensional grid
56+
/// </summary>
5357
public void GenerateGrid()
5458
{
5559
if (width > 0 && height > 0)
5660
{
5761
// Generate grid
58-
_cells = new Cell[width, height];
62+
cells = new Cell[width, height];
5963

6064
var scale = cellPrefab.transform.localScale;
6165
var origin = transform.position;
@@ -73,14 +77,14 @@ public void GenerateGrid()
7377
var cellObj = Instantiate(cellPrefab, curPos, Quaternion.identity, gameObject.transform);
7478
cellObj.name = $"({i}, {j})";
7579
var cell = cellObj.GetComponent<Cell>();
76-
_cells[i, j] = cell;
80+
cells[i, j] = cell;
7781
}
7882

7983
// Assign neighbours for every cell
8084
for (int i = 0; i < width; i++)
8185
for (int j = 0; j < height; j++)
8286
{
83-
var cell = _cells[i, j];
87+
var cell = cells[i, j];
8488
for (int k = 0; k < 4; k++)
8589
{
8690
int x = -1, y = -1;
@@ -112,7 +116,7 @@ public void GenerateGrid()
112116
}
113117
else
114118
{
115-
cell.neighbourCells[k] = _cells[x, y];
119+
cell.neighbourCells[k] = cells[x, y];
116120
}
117121
}
118122
}
@@ -123,6 +127,9 @@ public void GenerateGrid()
123127
}
124128
}
125129

130+
/// <summary>
131+
/// Destroys the current grid
132+
/// </summary>
126133
public void RemoveGrid()
127134
{
128135
foreach (Transform child in gameObject.transform)
@@ -134,10 +141,10 @@ public void RemoveGrid()
134141
/// <summary>
135142
/// Checks if the grid is valid
136143
/// </summary>
137-
/// <returns></returns>
144+
/// <returns>true if the grid is valid</returns>
138145
public bool CheckGrid()
139146
{
140-
var notMatchingCells = _levelGenerator.CheckGeneratedLevel(ref _cells);
147+
var notMatchingCells = _levelGenerator.CheckGeneratedLevel(ref cells);
141148

142149
return notMatchingCells.Count == 0;
143150
}

Assets/Scripts/Level Generation/LevelGenerator.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class LevelGenerator
1818
/// </summary>
1919
public static LevelGenerator Instance => _instance ?? (_instance = new LevelGenerator());
2020

21+
/// <summary>
22+
/// Stores the cells in a heap having the closest cell to being solved as first element
23+
/// </summary>
2124
public Heap<Cell> OrderedCells;
2225

2326
private LevelGenerator()
@@ -26,7 +29,7 @@ private LevelGenerator()
2629

2730
/// <summary>
2831
/// Wave-function-collapse algorithm
29-
/// TODO: Multithreading?
32+
/// TODO: Could be multithreaded to increase performance
3033
/// </summary>
3134
/// <param name="cells">The grid`s cells</param>
3235
/// <param name="seed">RNG seed</param>
@@ -51,8 +54,10 @@ public void GenerateLevelWFC(ref Cell[,] cells, int seed)
5154

5255
Debug.LogWarning("Start Wave-function-collapse algorithm");
5356

57+
// Make sure the level fits our initial constraints
5458
ApplyInitialConstraints(ref cells);
5559

60+
// Wave-function-collapse Algorithm
5661
while (true)
5762
{
5863
//Debug.Log("Starting another iteration! Removing next module.");
@@ -196,7 +201,7 @@ private void BorderOutsideConstraint(ref Cell[,] cells)
196201
}
197202

198203
/// <summary>
199-
/// Initial constraint: Place start and goal module
204+
/// Initial constraint: Place one start and one goal module
200205
/// </summary>
201206
/// <param name="cells">The grid`s cells</param>
202207
private void StartGoalConstraint(ref Cell[,] cells)

Assets/Scripts/Other/MainUI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void Generate()
2222

2323
grid.RemoveGrid();
2424
grid.GenerateGrid();
25-
LevelGenerator.Instance.GenerateLevelWFC(ref grid._cells, grid.seed != -1 ? grid.seed : Environment.TickCount);
25+
LevelGenerator.Instance.GenerateLevelWFC(ref grid.cells, grid.seed != -1 ? grid.seed : Environment.TickCount);
2626

2727
cameraController.AdjustCamera(grid.width, grid.height);
2828

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Wave-function-collapse algorithm with Unity
2-
This Unity example project utilizes the wave-function-collapse algorithm to procedurally generate levels.
2+
This Unity example project utilizes the wave-function-collapse algorithm to procedurally generate levels on a two-dimensional grid.
33

4-
![Demo image](./images/Demo0.png)
4+
[![Demo image](./images/Demo0.png)](https://www.atalantus.de/projects/games/wfc/index.html)
55

66
## About this project
77
Using Unity version `2019.1.14f1`.
88

9-
**TODO**
9+
### [Try it out!](https://www.atalantus.de/projects/games/wfc/index.html)
1010

1111
## Resources
12-
Special thanks to [Oskar Stålberg](https://twitter.com/OskSta) for [his talk](https://www.youtube.com/watch?v=0bcZb-SsnrA) about this algorithm and it's implementation in the game Bad North.
12+
Special credits to [Oskar Stålberg](https://twitter.com/OskSta) for [his talk](https://www.youtube.com/watch?v=0bcZb-SsnrA) about this algorithm and it's implementation in the game Bad North.
1313
If you want to learn more about the algorithm go check it out.
1414

1515
Other helpful resources about WFC:

0 commit comments

Comments
 (0)