Created
November 18, 2022 22:20
-
-
Save cdiggins/65296ab751622f41c2e4b91260263a86 to your computer and use it in GitHub Desktop.
Animation Clip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IAnimationCurve | |
{ | |
double this[double t] { get; } | |
double Duration { get; } | |
} | |
public class AnimationClip<T> : IAnimationClip<T> | |
{ | |
public AnimationClip(IAnimationCurve curve, T from, T to, Func<T, T, double, T> lerp) | |
=> (Curve, From, To, Lerp) = (curve, from, to, lerp); | |
public IAnimationCurve Curve { get; set; } | |
public Func<T, T, double, T> Lerp { get; } | |
public T From { get; set; } | |
public T To { get; set; } | |
public bool Started { get; private set; } = false; | |
public bool Stopped { get; private set; } = false; | |
public double StartTime { get; private set; } | |
public double StopTime { get; private set; } | |
public void Start(double t) | |
{ | |
StartTime = t; | |
Started = true; | |
Stopped = false; | |
} | |
public void Update(double t) | |
{ | |
if (!Started || Stopped) return; | |
OnUpdate?.Invoke(this, Evaluate(t)); | |
if (Elapsed(t) > Curve.Duration) | |
Stop(t); | |
} | |
public void Continue(double t) | |
{ | |
if (!Stopped) return; | |
StartTime += t - StopTime; | |
} | |
public void Stop(double t) | |
{ | |
Stopped = true; | |
StopTime = t; | |
} | |
public double Elapsed(double t) | |
=> Started ? t - StartTime : StopTime - StartTime; | |
public T Evaluate(double t) | |
=> Lerp(From, To, Curve[Elapsed(t)]); | |
public event EventHandler<T> OnUpdate; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment