Skip to content

Commit e6f0681

Browse files
committed
Optimizations
1 parent 6585de1 commit e6f0681

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

Assets/Scripts/Level Generation/Cell.cs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using System;
2-
using System.Collections;
3-
using System.Collections.Generic;
1+
using System.Collections.Generic;
2+
using System.Linq;
43
using UnityEngine;
54
using Random = UnityEngine.Random;
65

@@ -28,13 +27,17 @@ private void Awake()
2827

2928
public void PopulateCell()
3029
{
31-
// At the beginning every module is possible
30+
// at the beginning every module is possible
3231
for (var i = 0; i < levelGenerator.modules.Count; i++)
3332
{
3433
possibleModules.Add(levelGenerator.modules[i]);
3534
}
3635
}
3736

37+
/// <summary>
38+
/// Applies an <see cref="EdgeFilter"/> to this cell.
39+
/// </summary>
40+
/// <param name="filter">The filter to apply.</param>
3841
public void FilterCell(EdgeFilter filter)
3942
{
4043
if (possibleModules.Count == 1) return;
@@ -97,6 +100,7 @@ public void RemoveModule(Module module)
97100
}
98101
}
99102

103+
100104
public void SetModule(Module module)
101105
{
102106
possibleModules = new List<Module> {module};
@@ -121,8 +125,27 @@ public void SetModule(Module module)
121125
if (neighbours[i] == null) continue;
122126

123127
// Populate edge changes to neighbour cell
124-
var edgeFilter = new EdgeFilter(i, module.edgeConnections[i], true);
125-
neighbours[i].FilterCell(edgeFilter);
128+
neighbours[i].FilterCell(new EdgeFilter(i, module.edgeConnections[i], true));
129+
}
130+
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);
126149
}
127150

128151
Instantiate(module.moduleGO, transform.position, Quaternion.identity, transform);
@@ -131,10 +154,11 @@ public void SetModule(Module module)
131154
}
132155

133156
/// <summary>
134-
/// Compares two cells using their solved score
157+
/// Compares two cells using their solved score.
158+
/// TODO: Refactor
135159
/// </summary>
136160
/// <param name="other">Cell to compare</param>
137-
/// <returns></returns>
161+
/// <returns>Comparison value</returns>
138162
public int CompareTo(Cell other)
139163
{
140164
var compare = possibleModules.Count.CompareTo(other.possibleModules.Count);

Assets/Scripts/Level Generation/LevelGenerator.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Linq;
45
using Debug = UnityEngine.Debug;
56
using Random = UnityEngine.Random;
67

@@ -75,7 +76,7 @@ public void GenerateLevel()
7576
if (cell.possibleModules.Count == 1)
7677
{
7778
if (cell.isCellSet) OrderedCells.RemoveFirst();
78-
else cell.SetModule(cell.possibleModules[0]);
79+
else cell.ForceSetModule(cell.possibleModules[0]);
7980
}
8081
else
8182
{
@@ -84,11 +85,10 @@ public void GenerateLevel()
8485
}
8586

8687
// Remove random module from cell
87-
// TODO: Instead of removing a random module, set a random module
8888
if (OrderedCells.Count > 0)
8989
{
9090
var cell = OrderedCells.GetFirst();
91-
cell.RemoveModule(cell.possibleModules[Random.Range(0, cell.possibleModules.Count)]);
91+
cell.SetModule(cell.possibleModules[Random.Range(0, cell.possibleModules.Count)]);
9292
}
9393
else
9494
{
@@ -107,8 +107,6 @@ public void GenerateLevel()
107107
/// </summary>
108108
private void ApplyInitialConstraints()
109109
{
110-
Debug.Log("Resolve initial constraints");
111-
112110
StartGoalConstraint();
113111
BorderOutsideConstraint();
114112
}

0 commit comments

Comments
 (0)