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 Object = StardewValley.Object;
namespace StardewModdingAPI.Framework.StateTracking
{
/// Tracks changes to a location's data.
internal class LocationTracker : IWatcher
{
/*********
** Properties
*********/
/// The underlying watchers.
private readonly List Watchers = new List();
/*********
** Accessors
*********/
/// Whether the value changed since the last reset.
public bool IsChanged => this.Watchers.Any(p => p.IsChanged);
/// The tracked location.
public GameLocation Location { get; }
/// Tracks changes to the location's buildings.
public ICollectionWatcher BuildingsWatcher { get; }
/// Tracks changes to the location's objects.
public IDictionaryWatcher ObjectsWatcher { get; }
/*********
** Public methods
*********/
/// Construct an instance.
/// The location to track.
public LocationTracker(GameLocation location)
{
this.Location = location;
// init watchers
this.ObjectsWatcher = WatcherFactory.ForNetDictionary(location.netObjects);
this.BuildingsWatcher = location is BuildableGameLocation buildableLocation
? WatcherFactory.ForNetCollection(buildableLocation.buildings)
: (ICollectionWatcher)WatcherFactory.ForObservableCollection(new ObservableCollection());
this.Watchers.AddRange(new IWatcher[]
{
this.BuildingsWatcher,
this.ObjectsWatcher
});
}
/// Stop watching the player fields and release all references.
public void Dispose()
{
foreach (IWatcher watcher in this.Watchers)
watcher.Dispose();
}
/// Update the current value if needed.
public void Update()
{
foreach (IWatcher watcher in this.Watchers)
watcher.Update();
}
/// Set the current value as the baseline.
public void Reset()
{
foreach (IWatcher watcher in this.Watchers)
watcher.Reset();
}
}
}