diff --git a/Assets/Mirage/Runtime/InterestManagement/DistanceConstantSightInterestManager.cs b/Assets/Mirage/Runtime/InterestManagement/DistanceConstantSightInterestManager.cs index 1c2131900c6..cc04ce1588d 100644 --- a/Assets/Mirage/Runtime/InterestManagement/DistanceConstantSightInterestManager.cs +++ b/Assets/Mirage/Runtime/InterestManagement/DistanceConstantSightInterestManager.cs @@ -10,7 +10,90 @@ namespace Mirage.InterestManagement /// public class DistanceConstantSightInterestManager : InterestManager { + private class NetIdComparer : IEqualityComparer + { + public bool Equals(NetworkIdentity x, NetworkIdentity y) + { + return x.NetId == y.NetId; + } + + public int GetHashCode(NetworkIdentity obj) + { + return (int)obj.NetId; + } + } + public float SightDistnace = 10; + public float UpdateInterval = 0; + float nextUpdate = 0; + + public void Update() + { + if (nextUpdate < Time.time) + { + rebuild(); + nextUpdate += UpdateInterval; + } + } + + Dictionary> lastFrame = new Dictionary>(); + Dictionary> nextFrame = new Dictionary>(); + + private void rebuild() + { + foreach (NetworkIdentity identity in ServerObjectManager.SpawnedObjects.Values) + { + IReadOnlyCollection observers = Observers(identity); + foreach (INetworkPlayer player in observers) + { + if (!nextFrame.TryGetValue(player, out HashSet nextSet)) + { + nextSet = new HashSet(new NetIdComparer()); + nextFrame[player] = nextSet; + } + + nextSet.Add(identity); + } + } + + foreach (INetworkPlayer player in ServerObjectManager.Server.Players) + { + if (!lastFrame.TryGetValue(player, out HashSet lastSet)) + { + lastSet = new HashSet(new NetIdComparer()); + lastFrame[player] = lastSet; + } + if (!nextFrame.TryGetValue(player, out HashSet nextSet)) + { + nextSet = new HashSet(new NetIdComparer()); + nextFrame[player] = nextSet; + } + + + foreach (NetworkIdentity identity in lastSet) + { + if (!nextSet.Contains(identity)) + { + ServerObjectManager.HideForConnection(identity, player); + } + } + foreach (NetworkIdentity identity in nextSet) + { + if (!lastSet.Contains(identity)) + { + ServerObjectManager.ShowForConnection(identity, player); + } + } + + // reset collections + lastSet.Clear(); + foreach (NetworkIdentity identity in nextSet) + { + lastSet.Add(identity); + } + nextSet.Clear(); + } + } List temp = new List();