35 lines
1.4 KiB
C#

using System.Collections.Generic;
using Syntriax.Engine.Core;
namespace Syntriax.Engine.Systems.Tween;
public static class TweenShape2DExtensions
{
public static ITween TweenShape2D(this Shape2D shape, ITweenManager tweenManager, float duration, Shape2D targetShape2D)
{
List<Vector2D> initialVertices = new(shape.Vertices);
List<Vector2D> shapeVertices = new(shape.Vertices);
return tweenManager.StartTween(duration,
t =>
{
shapeVertices.Clear();
int maxCount = initialVertices.Count.Max(targetShape2D.Vertices.Count);
for (int i = 0; i < maxCount; i++)
{
int initialIndex = (i * (initialVertices.Count / (float)maxCount)).RoundToInt(Math.RoundMode.Floor);
int targetIndex = (i * (targetShape2D.Vertices.Count / (float)maxCount)).RoundToInt(Math.RoundMode.Floor);
shapeVertices.Add(targetShape2D.Vertices[targetIndex].Lerp(initialVertices[initialIndex], 1f - t));
}
shape.Vertices = shapeVertices;
}
).OnComplete(() =>
{
shapeVertices.Clear();
for (int i = 0; i < targetShape2D.Vertices.Count; i++)
shapeVertices.Add(targetShape2D.Vertices[i]);
shape.Vertices = shapeVertices;
});
}
}