using FishNet.Managing.Logging; using FishNet.Managing.Timing; using FishNet.Object; using FishNet.Object.Prediction; using GameKit.Dependencies.Utilities; using UnityEngine; #pragma warning disable CS0618 // Type or member is obsolete namespace FishNet.Component.Transforming { /// /// Smoothes an object between ticks. /// This can be used on objects without NetworkObject components. /// public class MonoTickSmoother : MonoBehaviour { //Lazy way to display obsolete message w/o using a custom editor. [Header("This component will be obsoleted soon.")] [Header("Use NetworkTickSmoother or OfflineTickSmoother.")] [Header(" ")] #region Serialized. /// /// True to use InstanceFinder to locate the TimeManager. When false specify which TimeManager to use by calling SetTimeManager. /// [Tooltip("True to use InstanceFinder to locate the TimeManager. When false specify which TimeManager to use by calling SetTimeManager.")] [SerializeField] private bool _useInstanceFinder = true; /// /// GraphicalObject you wish to smooth. /// [Tooltip("GraphicalObject you wish to smooth.")] [SerializeField] private Transform _graphicalObject; /// /// True to enable teleport threshhold. /// [Tooltip("True to enable teleport threshold.")] [SerializeField] private bool _enableTeleport; /// /// How far the object must move between ticks to teleport rather than smooth. /// [Tooltip("How far the object must move between ticks to teleport rather than smooth.")] [Range(0f, ushort.MaxValue)] [SerializeField] private float _teleportThreshold; #endregion #region Private. /// /// TimeManager subscribed to. /// private TimeManager _timeManager; /// /// BasicTickSmoother for this script. /// private LocalTransformTickSmoother _tickSmoother; #endregion private void OnEnable() { Initialize(); } private void OnDisable() { _tickSmoother.ResetState(); ChangeSubscription(false); ObjectCaches.StoreAndDefault(ref _tickSmoother); } [Client(Logging = LoggingType.Off)] private void Update() { _tickSmoother?.Update(); } /// /// Initializes this script for use. /// private void Initialize() { _tickSmoother = ObjectCaches.Retrieve(); if (_useInstanceFinder) { _timeManager = InstanceFinder.TimeManager; ChangeSubscription(true); } } /// /// Sets a new PredictionManager to use. /// /// public void SetTimeManager(TimeManager tm) { if (tm == _timeManager) return; //Unsub from current. ChangeSubscription(false); //Sub to newest. _timeManager = tm; ChangeSubscription(true); } /// /// Changes the subscription to the TimeManager. /// private void ChangeSubscription(bool subscribe) { if (_timeManager == null) return; if (subscribe) { if (_tickSmoother != null) { float tDistance = (_enableTeleport) ? _teleportThreshold : MoveRates.UNSET_VALUE; _tickSmoother.InitializeOnce(_graphicalObject, tDistance, (float)_timeManager.TickDelta, 1); } _timeManager.OnPreTick += _timeManager_OnPreTick; _timeManager.OnPostTick += _timeManager_OnPostTick; } else { _timeManager.OnPreTick -= _timeManager_OnPreTick; _timeManager.OnPostTick -= _timeManager_OnPostTick; } } /// /// Called before a tick starts. /// private void _timeManager_OnPreTick() { _tickSmoother.OnPreTick(); } /// /// Called after a tick completes. /// private void _timeManager_OnPostTick() { _tickSmoother.OnPostTick(); } } }