using FishNet.Component.Observing; using FishNet.Managing; using UnityEngine; namespace FishNet.Connection { /// /// A container for a connected client used to perform actions on and gather information for the declared client. /// public partial class NetworkConnection { #region Internal. /// /// Current GridEntry this connection is in. /// internal GridEntry HashGridEntry = HashGrid.EmptyGridEntry; #endregion #region Private. /// /// HashGrid for the NetworkManager on this connection. /// private HashGrid _hashGrid; /// /// Last unscaled time the HashGrid position was updated with this connections Objects. /// private float _nextHashGridUpdateTime; /// /// Current GridPosition this connection is in. /// private Vector2Int _hashGridPosition = HashGrid.UnsetGridPosition; #endregion /// /// Called when the FirstObject changes for this connection. /// private void Observers_FirstObjectChanged() { UpdateHashGridPositions(true); } /// /// Initializes this for use. /// private void Observers_Initialize(NetworkManager nm) { nm.TryGetInstance(out _hashGrid); } /// /// Updates the HashGridPosition value for FirstObject. /// internal void UpdateHashGridPositions(bool force) { if (_hashGrid == null) return; float unscaledTime = Time.unscaledTime; //Not enough time has passed to update. if (!force && unscaledTime < _nextHashGridUpdateTime) return; const float updateInterval = 1f; _nextHashGridUpdateTime = unscaledTime + updateInterval; if (FirstObject == null) { HashGridEntry = HashGrid.EmptyGridEntry; _hashGridPosition = HashGrid.UnsetGridPosition; } else { Vector2Int newPosition = _hashGrid.GetHashGridPosition(FirstObject); if (newPosition != _hashGridPosition) { _hashGridPosition = newPosition; HashGridEntry = _hashGrid.GetGridEntry(newPosition); } } } /// /// Resets values. /// private void Observers_Reset() { _hashGrid = null; _hashGridPosition = HashGrid.UnsetGridPosition; _nextHashGridUpdateTime = 0f; } } }