using FishNet.Managing.Logging; using FishNet.Managing.Timing; using FishNet.Object; using FishNet.Object.Prediction; using GameKit.Dependencies.Utilities; using UnityEngine; namespace FishNet.Component.Transforming { /// /// Smoothes an object between ticks. /// public class NetworkTickSmoother : NetworkBehaviour { #region Serialized. /// /// 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 OnDestroy() { ChangeSubscription(false); ObjectCaches.StoreAndDefault(ref _tickSmoother); } public override void OnStartClient() { Initialize(); } [Client(Logging = LoggingType.Off)] private void Update() { _tickSmoother?.Update(); } /// /// Initializes this script for use. /// private void Initialize() { if (_tickSmoother == null) _tickSmoother = ObjectCaches.Retrieve(); SetTimeManager(base.TimeManager); } /// /// 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 : MoveRatesCls.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(); } } }