using FishNet.Connection; using FishNet.Managing.Server; using FishNet.Object; using System; using UnityEngine; namespace FishNet.Observing { /// /// Condition a connection must meet to be added as an observer. /// This class can be inherited from for custom conditions. /// public abstract class ObserverCondition : ScriptableObject { #region Public. /// /// NetworkObject this condition is for. /// [HideInInspector] public NetworkObject NetworkObject; #endregion #region Private. /// /// True if this condition is enabled. /// private bool _isEnabled = true; /// /// Gets the enabled state of this condition. /// /// public bool GetIsEnabled() => _isEnabled; /// /// Sets the enabled state of this condition. /// If the state has changed observers will be rebuilt /// for this object. /// /// public void SetIsEnabled(bool value) { if (value == GetIsEnabled()) return; _isEnabled = value; //No object to rebuild for. if (NetworkObject == null) return; ServerObjects so = NetworkObject?.ServerManager?.Objects; if (so != null) so.RebuildObservers(NetworkObject); } #endregion /// /// Initializes this script for use. /// /// NetworkObject this condition is initializing for. public virtual void Initialize(NetworkObject networkObject) { NetworkObject = networkObject; } /// /// Deinitializes this script. /// /// True if the object is being destroyed, false if being despawned. An object may deinitialize for despawn, then destroy after. public virtual void Deinitialize(bool destroyed) { NetworkObject = null; } /// /// Returns if the object which this condition resides should be visible to connection. /// /// Connection which the condition is being checked for. /// True if the connection currently has visibility of this object. /// True if the condition was not processed. This can be used to skip processing for performance. While output as true this condition result assumes the previous ConditionMet value. public abstract bool ConditionMet(NetworkConnection connection, bool currentlyAdded, out bool notProcessed); /// /// Type of condition this is. Certain types are handled different, such as Timed which are checked for changes at timed intervals. /// /// public abstract ObserverConditionType GetConditionType(); } }