using FishNet.Managing.Predicting;
using FishNet.Managing.Timing;
using FishNet.Object;
using UnityEngine;
namespace FishNet.Component.Prediction
{
public partial class OfflineRigidbody : MonoBehaviour
{
#region Serialized.
///
/// Type of prediction movement which is being used.
///
[Tooltip("Type of prediction movement which is being used.")]
[SerializeField]
private RigidbodyType _rigidbodyType;
///
/// GraphicalObject to unparent when pausing.
///
private Transform _graphicalObject;
///
/// Sets GraphicalObject.
///
///
public void SetGraphicalObject(Transform value)
{
_graphicalObject = value;
UpdateRigidbodies();
}
///
/// True to also get rigidbody components within children.
///
[Tooltip("True to also get rigidbody components within children.")]
[SerializeField]
private bool _getInChildren;
#endregion
#region Private.
///
/// Pauser for rigidbodies.
///
private RigidbodyPauser _rigidbodyPauser = new RigidbodyPauser();
///
/// TimeManager subscribed to.
///
private PredictionManager _predictionManager;
#endregion
private void Awake()
{
InitializeOnce();
}
private void OnDestroy()
{
ChangeSubscription(false);
}
///
/// Initializes this script for use.
///
private void InitializeOnce()
{
_predictionManager = InstanceFinder.PredictionManager;
UpdateRigidbodies();
ChangeSubscription(true);
}
///
/// Sets a new TimeManager to use.
///
///
public void SetPredictionManager(PredictionManager pm)
{
if (pm == _predictionManager)
return;
//Unsub from current.
ChangeSubscription(false);
//Sub to newest.
_predictionManager = pm;
ChangeSubscription(true);
}
///
/// Finds and assigns rigidbodie using configured settings.
///
public void UpdateRigidbodies()
{
_rigidbodyPauser.UpdateRigidbodies(transform, _rigidbodyType, _getInChildren, _graphicalObject);
}
///
/// Changes the subscription to the TimeManager.
///
private void ChangeSubscription(bool subscribe)
{
if (_predictionManager == null)
return;
if (subscribe)
{
_predictionManager.OnPreReconcile += _predictionManager_OnPreReconcile;
_predictionManager.OnPostReconcile += _predictionManager_OnPostReconcile;
}
else
{
_predictionManager.OnPreReconcile -= _predictionManager_OnPreReconcile;
_predictionManager.OnPostReconcile -= _predictionManager_OnPostReconcile;
}
}
private void _predictionManager_OnPreReconcile(NetworkBehaviour obj)
{
//Make rbs all kinematic/!simulated before reconciling, which would also result in replays.
_rigidbodyPauser.Pause();
}
private void _predictionManager_OnPostReconcile(NetworkBehaviour obj)
{
_rigidbodyPauser.Unpause();
}
}
}