diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-08-01 11:07:29 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-08-01 11:07:29 -0400 |
commit | 60b41195778af33fd609eab66d9ae3f1d1165e8f (patch) | |
tree | 7128b906d40e94c56c34ed6058f27bc31c31a08b /src/SMAPI/Framework/StateTracking/LocationTracker.cs | |
parent | b9bc1a6d17cafa0a97b46ffecda432cfc2f23b51 (diff) | |
parent | 52cf953f685c65b2b6814e375ec9a5ffa03c440a (diff) | |
download | SMAPI-60b41195778af33fd609eab66d9ae3f1d1165e8f.tar.gz SMAPI-60b41195778af33fd609eab66d9ae3f1d1165e8f.tar.bz2 SMAPI-60b41195778af33fd609eab66d9ae3f1d1165e8f.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/StateTracking/LocationTracker.cs')
-rw-r--r-- | src/SMAPI/Framework/StateTracking/LocationTracker.cs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/StateTracking/LocationTracker.cs b/src/SMAPI/Framework/StateTracking/LocationTracker.cs new file mode 100644 index 00000000..708c0716 --- /dev/null +++ b/src/SMAPI/Framework/StateTracking/LocationTracker.cs @@ -0,0 +1,103 @@ +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using Microsoft.Xna.Framework; +using StardewModdingAPI.Framework.StateTracking.FieldWatchers; +using StardewValley; +using StardewValley.Buildings; +using StardewValley.Locations; +using StardewValley.TerrainFeatures; +using Object = StardewValley.Object; + +namespace StardewModdingAPI.Framework.StateTracking +{ + /// <summary>Tracks changes to a location's data.</summary> + internal class LocationTracker : IWatcher + { + /********* + ** Properties + *********/ + /// <summary>The underlying watchers.</summary> + private readonly List<IWatcher> Watchers = new List<IWatcher>(); + + + /********* + ** Accessors + *********/ + /// <summary>Whether the value changed since the last reset.</summary> + public bool IsChanged => this.Watchers.Any(p => p.IsChanged); + + /// <summary>The tracked location.</summary> + public GameLocation Location { get; } + + /// <summary>Tracks added or removed buildings.</summary> + public ICollectionWatcher<Building> BuildingsWatcher { get; } + + /// <summary>Tracks added or removed debris.</summary> + public ICollectionWatcher<Debris> DebrisWatcher { get; } + + /// <summary>Tracks added or removed large terrain features.</summary> + public ICollectionWatcher<LargeTerrainFeature> LargeTerrainFeaturesWatcher { get; } + + /// <summary>Tracks added or removed NPCs.</summary> + public ICollectionWatcher<NPC> NpcsWatcher { get; } + + /// <summary>Tracks added or removed objects.</summary> + public IDictionaryWatcher<Vector2, Object> ObjectsWatcher { get; } + + /// <summary>Tracks added or removed terrain features.</summary> + public IDictionaryWatcher<Vector2, TerrainFeature> TerrainFeaturesWatcher { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="location">The location to track.</param> + public LocationTracker(GameLocation location) + { + this.Location = location; + + // init watchers + this.BuildingsWatcher = location is BuildableGameLocation buildableLocation + ? WatcherFactory.ForNetCollection(buildableLocation.buildings) + : (ICollectionWatcher<Building>)WatcherFactory.ForObservableCollection(new ObservableCollection<Building>()); + this.DebrisWatcher = WatcherFactory.ForNetCollection(location.debris); + this.LargeTerrainFeaturesWatcher = WatcherFactory.ForNetCollection(location.largeTerrainFeatures); + this.NpcsWatcher = WatcherFactory.ForNetCollection(location.characters); + this.ObjectsWatcher = WatcherFactory.ForNetDictionary(location.netObjects); + this.TerrainFeaturesWatcher = WatcherFactory.ForNetDictionary(location.terrainFeatures); + + this.Watchers.AddRange(new IWatcher[] + { + this.BuildingsWatcher, + this.DebrisWatcher, + this.LargeTerrainFeaturesWatcher, + this.NpcsWatcher, + this.ObjectsWatcher, + this.TerrainFeaturesWatcher + }); + } + + /// <summary>Stop watching the player fields and release all references.</summary> + public void Dispose() + { + foreach (IWatcher watcher in this.Watchers) + watcher.Dispose(); + } + + /// <summary>Update the current value if needed.</summary> + public void Update() + { + foreach (IWatcher watcher in this.Watchers) + watcher.Update(); + } + + /// <summary>Set the current value as the baseline.</summary> + public void Reset() + { + foreach (IWatcher watcher in this.Watchers) + watcher.Reset(); + } + } +} |