using UnityEngine; namespace Chronos { /// /// An abstract base component that saves snapshots at regular intervals to enable rewinding. /// [HelpURL("http://ludiq.io/chronos/documentation#Recorder")] public abstract class Recorder : MonoBehaviour { private bool enabledOnce; private class DelegatedRecorder : RecorderTimeline { private Recorder parent; public DelegatedRecorder(Recorder parent, Timeline timeline) : base(timeline, null) { this.parent = parent; } protected override void ApplySnapshot(TSnapshot snapshot) { parent.ApplySnapshot(snapshot); } protected override TSnapshot CopySnapshot() { return parent.CopySnapshot(); } protected override TSnapshot LerpSnapshots(TSnapshot from, TSnapshot to, float t) { return parent.LerpSnapshots(from, to, t); } } protected virtual void Awake() { CacheComponents(); } protected virtual void Start() { recorder.OnStartOrReEnable(); } protected virtual void OnEnable() { if (enabledOnce) { recorder.OnStartOrReEnable(); } else { enabledOnce = true; } } protected virtual void Update() { recorder.Update(); } protected virtual void OnDisable() { recorder.OnDisable(); } /// /// Modifies all snapshots via the specified modifier delegate. /// public virtual void ModifySnapshots(RecorderTimeline.SnapshotModifier modifier) { recorder.ModifySnapshots(modifier); } private Timeline timeline; private RecorderTimeline recorder; protected abstract void ApplySnapshot(TSnapshot snapshot); protected abstract TSnapshot CopySnapshot(); protected abstract TSnapshot LerpSnapshots(TSnapshot from, TSnapshot to, float t); public virtual void CacheComponents() { timeline = GetComponentInParent(); if (timeline == null) { throw new ChronosException(string.Format("Missing timeline for recorder.")); } recorder = new DelegatedRecorder(this, timeline); } } }