blob: d31b1128ea60960ec879634479f3142874a3aa04 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
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 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.ObjectsWatcher = WatcherFactory.ForNetDictionary(location.netObjects);
this.BuildingsWatcher = location is BuildableGameLocation buildableLocation
? WatcherFactory.ForNetCollection(buildableLocation.buildings)
: (ICollectionWatcher<Building>)WatcherFactory.ForObservableCollection(new ObservableCollection<Building>());
this.TerrainFeaturesWatcher = WatcherFactory.ForNetDictionary(location.terrainFeatures);
this.Watchers.AddRange(new IWatcher[]
{
this.BuildingsWatcher,
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();
}
}
}
|