mirror of
https://github.com/MaddyThorson/StrawberryBF.git
synced 2025-04-11 03:06:04 +08:00
Tween component. Bezier struct
This commit is contained in:
parent
5fdc34c610
commit
bdd32cba8b
54
src/Components/Logic/Tween.bf
Normal file
54
src/Components/Logic/Tween.bf
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
using System;
|
||||||
|
namespace Strawberry
|
||||||
|
{
|
||||||
|
public class Tween : Component
|
||||||
|
{
|
||||||
|
public Ease.Easer Easer;
|
||||||
|
public delegate void(float) OnUpdate;
|
||||||
|
public delegate void() OnComplete;
|
||||||
|
public bool RemoveOnComplete;
|
||||||
|
|
||||||
|
public float T { get; private set; }
|
||||||
|
|
||||||
|
public this(Ease.Easer easer = null, delegate void(float) onUpdate = null, delegate void() onComplete = null, bool removeOnComplete = true, bool start = true)
|
||||||
|
: base(start, false)
|
||||||
|
{
|
||||||
|
Easer = easer;
|
||||||
|
OnUpdate = onUpdate;
|
||||||
|
OnComplete = onComplete;
|
||||||
|
RemoveOnComplete = removeOnComplete;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float Eased => Easer != null ? Easer(T) : T;
|
||||||
|
|
||||||
|
public void Play()
|
||||||
|
{
|
||||||
|
T = 0;
|
||||||
|
Active = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Started()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update()
|
||||||
|
{
|
||||||
|
T = Math.Min(T + Time.Delta, 1);
|
||||||
|
OnUpdate?.Invoke(Eased);
|
||||||
|
|
||||||
|
if (T >= 1)
|
||||||
|
{
|
||||||
|
OnComplete?.Invoke();
|
||||||
|
Active = false;
|
||||||
|
if (RemoveOnComplete)
|
||||||
|
RemoveSelf();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Draw()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
src/Struct/Bezier.bf
Normal file
35
src/Struct/Bezier.bf
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
namespace Strawberry
|
||||||
|
{
|
||||||
|
public struct Bezier
|
||||||
|
{
|
||||||
|
public Vector Start;
|
||||||
|
public Vector Control;
|
||||||
|
public Vector End;
|
||||||
|
|
||||||
|
public this(Vector start, Vector control, Vector end)
|
||||||
|
{
|
||||||
|
Start = start;
|
||||||
|
Control = control;
|
||||||
|
End = end;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector this[float t]
|
||||||
|
{
|
||||||
|
get => (Start * (1 - t) * (1 - t)) + (Control * 2f * (1 - t) * t) + (End * t * t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public float GetLengthParametric(int resolution)
|
||||||
|
{
|
||||||
|
Vector last = Start;
|
||||||
|
float length = 0;
|
||||||
|
for (int i = 1; i <= resolution; i++)
|
||||||
|
{
|
||||||
|
Vector at = this[i / (float)resolution];
|
||||||
|
length += (at - last).Length;
|
||||||
|
last = at;
|
||||||
|
}
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user