Skip to content

Commit 95148fc

Browse files
committed
Code refactoring and cleanup
1 parent e6f0681 commit 95148fc

8 files changed

Lines changed: 139 additions & 152 deletions

File tree

Assets/Scripts/Level Generation/Cell.cs

Lines changed: 51 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using UnityEngine;
43
using Random = UnityEngine.Random;
54

65
namespace LevelGeneration
76
{
7+
/// <summary>
8+
/// A cell inside the level`s grid.
9+
/// </summary>
810
public class Cell : MonoBehaviour, IHeapItem<Cell>
911
{
10-
public bool isCellSet;
12+
/// <summary>
13+
/// Has the decision of the final module already been propagated.
14+
///
15+
/// NOTE:
16+
/// This is in fact different then just having <code>Count</code> of <see cref="possibleModules"/> 1!
17+
///
18+
/// For example a cell's possibility space can be given only one module from the start.
19+
/// In this case <code>Count</code> of <see cref="possibleModules"/> is already 1 but the decision to chose
20+
/// this only model`s object as object for the cell has not been propagated yet.
21+
/// </summary>
22+
public bool isFinal;
1123

24+
/// <summary>
25+
/// The possible <see cref="Module"/> objects. (Possibility space)
26+
/// </summary>
1227
public List<Module> possibleModules;
1328

14-
public LevelGenerator levelGenerator;
15-
1629
/// <summary>
17-
/// (bottom, right, top, left)
30+
/// The adjacent <see cref="Cell"/> objects inside the grid.
31+
/// Element can be null if the cell is on the grid`s edge.
32+
///
33+
/// [bottom, right, top, left]
1834
/// </summary>
1935
public Cell[] neighbours = new Cell[4];
2036

37+
public LevelGenerator levelGenerator;
38+
2139
public int HeapIndex { get; set; }
2240

2341
private void Awake()
@@ -44,43 +62,42 @@ public void FilterCell(EdgeFilter filter)
4462

4563
var removingModules = new List<Module>();
4664

47-
// Filter possible Modules list for a given filter
65+
// filter possible modules list
4866
for (var i = 0; i < possibleModules.Count; i++)
4967
{
50-
var module = possibleModules[i];
51-
var removeModule = filter.CheckModule(module);
52-
53-
if (removeModule)
54-
{
55-
// Remove module
56-
removingModules.Add(possibleModules[i]);
57-
}
68+
if (filter.CheckModule(possibleModules[i])) removingModules.Add(possibleModules[i]);
5869
}
5970

60-
// Now remove filtered modules
71+
// remove filtered modules
6172
for (var i = 0; i < removingModules.Count; i++)
6273
{
6374
RemoveModule(removingModules[i]);
6475
}
6576
}
6677

78+
/// <summary>
79+
/// Removes a <see cref="Module"/> from <see cref="possibleModules"/>
80+
/// checking if it was the last one on any edge of a specific edge type and
81+
/// if so propagating the changes to the affected neighbour.
82+
/// </summary>
83+
/// <param name="module">The <see cref="Module"/> to remove.</param>
6784
public void RemoveModule(Module module)
6885
{
69-
// Remove module from possibility space
86+
// remove module from possibility space
7087
possibleModules.Remove(module);
7188

72-
// Update item on the heap
73-
levelGenerator.OrderedCells.UpdateItem(this);
89+
// update item on the heap
90+
levelGenerator.orderedCells.UpdateItem(this);
7491

7592
for (var j = 0; j < neighbours.Length; j++)
7693
{
77-
// Only check if cell has a neighbour on this edge
94+
// only check if cell has a neighbour on this edge
7895
if (neighbours[j] == null) continue;
7996

8097
var edgeType = module.edgeConnections[j];
8198
var lastWithEdgeType = true;
8299

83-
// Search in other possible modules for the same edge type
100+
// search in other possible modules for the same edge type
84101
for (var i = 0; i < possibleModules.Count; i++)
85102
{
86103
if (possibleModules[i].edgeConnections[j] == edgeType)
@@ -92,73 +109,53 @@ public void RemoveModule(Module module)
92109

93110
if (lastWithEdgeType)
94111
{
95-
// Populate edge changes to neighbour cell
112+
// populate edge changes to neighbour cell
96113
var edgeFilter = new EdgeFilter(j, edgeType, false);
97-
98114
neighbours[j].FilterCell(edgeFilter);
99115
}
100116
}
101117
}
102118

103-
119+
/// <summary>
120+
/// Assigns this cell a specific <see cref="Module"/> removing others.
121+
/// </summary>
122+
/// <param name="module">The <see cref="Module"/>.</param>
104123
public void SetModule(Module module)
105124
{
106125
possibleModules = new List<Module> {module};
107126

108-
// Update item on the heap
109-
levelGenerator.OrderedCells.UpdateItem(this);
127+
// update item on the heap
128+
levelGenerator.orderedCells.UpdateItem(this);
110129

111130
// check if it fits to already set neighbour cells
112131
for (var i = 0; i < neighbours.Length; i++)
113132
{
114-
if (neighbours[i] == null || !neighbours[i].isCellSet) continue;
133+
if (neighbours[i] == null || !neighbours[i].isFinal) continue;
115134

116135
if (module.edgeConnections[i] != neighbours[i].possibleModules[0].edgeConnections[(i + 2) % 4])
117136
Debug.LogError(
118137
$"Setting module {module} would not fit already set neighbour {neighbours[i].gameObject}!",
119138
gameObject);
120139
}
121140

122-
// Propagate changes to neighbours
141+
// propagate changes to neighbours
123142
for (var i = 0; i < neighbours.Length; i++)
124143
{
125144
if (neighbours[i] == null) continue;
126145

127-
// Populate edge changes to neighbour cell
146+
// populate edge changes to neighbour cell
128147
neighbours[i].FilterCell(new EdgeFilter(i, module.edgeConnections[i], true));
129148
}
130149

131-
Instantiate(module.moduleGO, transform.position, Quaternion.identity, transform);
132-
133-
isCellSet = true;
134-
}
135-
136-
public void ForceSetModule(Module module)
137-
{
138-
possibleModules = new List<Module> {module};
139-
140-
// check if it fits to already set neighbour cells
141-
for (var i = 0; i < neighbours.Length; i++)
142-
{
143-
if (neighbours[i] == null || !neighbours[i].isCellSet) continue;
144-
145-
if (module.edgeConnections[i] != neighbours[i].possibleModules[0].edgeConnections[(i + 2) % 4])
146-
Debug.LogError(
147-
$"Setting module {module} would not fit already set neighbour {neighbours[i].gameObject}!",
148-
gameObject);
149-
}
150-
151-
Instantiate(module.moduleGO, transform.position, Quaternion.identity, transform);
152-
153-
isCellSet = true;
150+
isFinal = true;
154151
}
155152

156153
/// <summary>
157154
/// Compares two cells using their solved score.
158-
/// TODO: Refactor
155+
/// TODO: Refactor. Is the extra randomness necessary?
159156
/// </summary>
160-
/// <param name="other">Cell to compare</param>
161-
/// <returns>Comparison value</returns>
157+
/// <param name="other">Cell to compare.</param>
158+
/// <returns>Comparison value.</returns>
162159
public int CompareTo(Cell other)
163160
{
164161
var compare = possibleModules.Count.CompareTo(other.possibleModules.Count);

Assets/Scripts/Level Generation/EdgeFilter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public class EdgeFilter
2121

2222
/// <summary>
2323
/// The edges`s direction.
24-
/// (bottom, right, top, left)
24+
///
25+
/// [bottom, right, top, left]
2526
/// </summary>
2627
private readonly int _edgeDirection;
2728

@@ -32,6 +33,11 @@ public EdgeFilter(int edgeDirection, Module.EdgeConnectionTypes filterType, bool
3233
_isInclusive = isInclusive;
3334
}
3435

36+
/// <summary>
37+
/// Checks if a <see cref="Module"/> matches this filter.
38+
/// </summary>
39+
/// <param name="module">The <see cref="Module"/>.</param>
40+
/// <returns>True if the <see cref="Module"/> matches this filter.</returns>
3541
public bool CheckModule(Module module)
3642
{
3743
var edge = (_edgeDirection + 2) % 4;

Assets/Scripts/Level Generation/GridGenerator.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public class GridGenerator : MonoBehaviour
2727
public GameObject cellPrefab;
2828

2929
/// <summary>
30-
/// Cells matrix ([width, height])
30+
/// Cells matrix ([width, height]).
3131
/// </summary>
3232
protected Cell[,] cells;
3333

3434
/// <summary>
35-
/// Generates the two-dimensional grid
35+
/// Generates the two-dimensional grid.
3636
/// </summary>
3737
protected void GenerateGrid(LevelGenerator levelGenerator)
3838
{
@@ -42,7 +42,7 @@ protected void GenerateGrid(LevelGenerator levelGenerator)
4242
return;
4343
}
4444

45-
// Generate grid
45+
// generate grid
4646
cells = new Cell[width, height];
4747

4848
var scale = cellPrefab.transform.localScale;
@@ -58,7 +58,7 @@ protected void GenerateGrid(LevelGenerator levelGenerator)
5858
{
5959
var curPos = new Vector3(bottomLeft.x + x * scale.x, bottomLeft.y, bottomLeft.z + z * scale.z);
6060

61-
// Create new cell
61+
// create new cell
6262
var cellObj = Instantiate(cellPrefab, curPos, Quaternion.identity, gameObject.transform);
6363
cellObj.name = $"({x}, {z})";
6464
var cell = cellObj.GetComponent<Cell>();
@@ -67,7 +67,7 @@ protected void GenerateGrid(LevelGenerator levelGenerator)
6767
cells[x, z] = cell;
6868

6969
/*
70-
* assign neighbours
70+
* Assign neighbours
7171
*/
7272

7373
if (x > 0)
@@ -87,7 +87,7 @@ protected void GenerateGrid(LevelGenerator levelGenerator)
8787
}
8888

8989
/// <summary>
90-
/// Destroys the current grid
90+
/// Destroys the current grid.
9191
/// </summary>
9292
protected void RemoveGrid()
9393
{

0 commit comments

Comments
 (0)