11using System . Collections . Generic ;
2- using System . Linq ;
32using UnityEngine ;
43using Random = UnityEngine . Random ;
54
65namespace 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 ) ;
0 commit comments