Skip to content

Commit ee041d3

Browse files
committed
Cache listOfSpinners within the entity
... to avoid doing linq magic on each frame until the task finished running
1 parent d584de4 commit ee041d3

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

Entities/SpinnerBreakingBallGeneric.cs

+11-9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static void Unload() {
3333
private CancellationTokenSource computeSpinnerNeighborsToken;
3434

3535
private Dictionary<SpinnerType, HashSet<SpinnerType>> spinnerNeighbors;
36+
private List<SpinnerType> listOfSpinners;
3637
private HashSet<SpinnerType> shatteredSpinners = new HashSet<SpinnerType>();
3738

3839
public SpinnerBreakingBallGeneric(EntityData data, Vector2 offset, EntityID entityID, ColorType color) : base(data.Position + offset) {
@@ -62,6 +63,7 @@ public SpinnerBreakingBallGeneric(EntityData data, Vector2 offset, EntityID enti
6263
private void onTransitionOut() {
6364
// wipe the spinner connections so that they are computed again after the transition (in the new room).
6465
spinnerNeighbors = null;
66+
listOfSpinners = null;
6567
}
6668

6769
public override void Awake(Scene scene) {
@@ -84,15 +86,15 @@ public override void Update() {
8486

8587
base.Update();
8688

87-
IEnumerable<SpinnerType> listOfSpinners;
88-
if (spinnerNeighbors == null) {
89-
if (computeSpinnerNeighbors == null) {
90-
computeSpinnerNeighborsToken = new CancellationTokenSource();
91-
computeSpinnerNeighbors = computeSpinnerConnections(computeSpinnerNeighborsToken.Token);
92-
}
93-
listOfSpinners = Scene.Tracker.GetEntities<SpinnerType>().OfType<SpinnerType>().Where(spinner => getColor(spinner).Equals(color));
94-
} else {
95-
listOfSpinners = spinnerNeighbors.Keys;
89+
if (spinnerNeighbors == null && computeSpinnerNeighbors == null) {
90+
computeSpinnerNeighborsToken = new CancellationTokenSource();
91+
computeSpinnerNeighbors = computeSpinnerConnections(computeSpinnerNeighborsToken.Token);
92+
}
93+
if (listOfSpinners == null) {
94+
listOfSpinners = Scene.Tracker.GetEntities<SpinnerType>()
95+
.OfType<SpinnerType>()
96+
.Where(spinner => getColor(spinner).Equals(color))
97+
.ToList();
9698
}
9799

98100
// we want to check all spinners explicitly instead of just going CollideCheck<SpinnerType>(),

0 commit comments

Comments
 (0)