@@ -30,6 +30,9 @@ internal class WorldLocationsTracker : IWatcher
3030 /// <summary>A lookup of registered buildings and their indoor location.</summary>
3131 private readonly Dictionary < Building , GameLocation ? > BuildingIndoors = new ( new ObjectReferenceComparer < Building > ( ) ) ;
3232
33+ /// <summary>The pooled list instance for <see cref="GetLocationsWhoseBuildingsChanged"/>.</summary>
34+ private static readonly List < LocationTracker > PooledLocationsWithBuildingsChanged = new ( ) ;
35+
3336
3437 /*********
3538 ** Accessors
@@ -95,21 +98,23 @@ public void Update()
9598 }
9699
97100 // detect building changed
98- foreach ( LocationTracker watcher in this . Locations . Where ( p => p . BuildingsWatcher . IsChanged ) . ToArray ( ) )
101+ foreach ( LocationTracker watcher in this . GetLocationsWhoseBuildingsChanged ( ) )
99102 {
100103 this . Remove ( watcher . BuildingsWatcher . Removed ) ;
101104 this . Add ( watcher . BuildingsWatcher . Added ) ;
102105 }
103106
104107 // detect building interiors changed (e.g. construction completed)
105- foreach ( ( Building building , GameLocation ? oldIndoors ) in this . BuildingIndoors . Where ( p => ! object . Equals ( p . Key . indoors . Value , p . Value ) ) )
108+ foreach ( ( Building building , GameLocation ? oldIndoors ) in this . BuildingIndoors )
106109 {
107110 GameLocation ? newIndoors = building . indoors . Value ;
111+ if ( object . ReferenceEquals ( oldIndoors , newIndoors ) )
112+ continue ;
113+
114+ this . Remove ( oldIndoors ) ;
115+ this . Add ( newIndoors ) ;
108116
109- if ( oldIndoors != null )
110- this . Added . Add ( oldIndoors ) ;
111- if ( newIndoors != null )
112- this . Removed . Add ( newIndoors ) ;
117+ this . BuildingIndoors [ building ] = newIndoors ;
113118 }
114119 }
115120
@@ -259,5 +264,21 @@ private IEnumerable<IWatcher> GetWatchers()
259264 foreach ( LocationTracker watcher in this . Locations )
260265 yield return watcher ;
261266 }
267+
268+ /// <summary>Get the locations whose building list changed, if any.</summary>
269+ private List < LocationTracker > GetLocationsWhoseBuildingsChanged ( )
270+ {
271+ List < LocationTracker > list = WorldLocationsTracker . PooledLocationsWithBuildingsChanged ;
272+ if ( list . Count > 0 )
273+ list . Clear ( ) ;
274+
275+ foreach ( LocationTracker watcher in this . LocationDict . Values )
276+ {
277+ if ( watcher . IsChanged )
278+ list . Add ( watcher ) ;
279+ }
280+
281+ return list ;
282+ }
262283 }
263284}
0 commit comments