7 Commits

Author SHA1 Message Date
4c542df401 perf: implemented fast list with index mapping 2025-10-10 14:58:40 +03:00
28ca343b43 perf: improved pool return method by using a hashset for searching if the returning item is already queued 2025-10-10 14:21:54 +03:00
651b0614c4 fix: index check on triangle batch flush 2025-10-10 11:43:04 +03:00
f47488c6f1 fix: registering/unregistering objects during universe enter/exit causing stack overflows 2025-10-10 10:59:39 +03:00
6d159330a1 refactor: moved client and server interfaces into their files 2025-08-31 23:09:02 +03:00
8e314f3269 feat: networking type hasher added 2025-08-19 21:17:47 +03:00
f5a7077570 perf: improved garbage created by tweens slightly
They still do generate a lot of garbage but with boxed value pools I made the boxes reusable, it still does generate garbage through the delegate creation, gotta find a solution for them later
2025-08-14 20:31:46 +03:00
27 changed files with 416 additions and 84 deletions

View File

@@ -16,8 +16,8 @@ public class ActiveBehaviourCollector<T> : IBehaviourCollector<T> where T : clas
private readonly Event<IUniverse, IUniverse.UniverseObjectRegisteredArguments>.EventHandler delegateOnUniverseObjectRegistered = null!;
private readonly Event<IUniverse, IUniverse.UniverseObjectUnRegisteredArguments>.EventHandler delegateOnUniverseObjectUnregistered = null!;
private readonly List<T> monitoringBehaviours = new(32);
protected readonly List<T> activeBehaviours = new(32);
private readonly FastList<T> monitoringBehaviours = new(32);
protected readonly FastList<T> activeBehaviours = new(32);
protected readonly Dictionary<IActive, T> monitoringActiveToBehaviour = new(32);
public IUniverse Universe { get; private set; } = null!;

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
namespace Engine.Core;
@@ -15,7 +14,7 @@ public class BehaviourCollector<T> : IBehaviourCollector<T> where T : class
private readonly Event<IUniverse, IUniverse.UniverseObjectRegisteredArguments>.EventHandler delegateOnUniverseObjectRegistered = null!;
private readonly Event<IUniverse, IUniverse.UniverseObjectUnRegisteredArguments>.EventHandler delegateOnUniverseObjectUnregistered = null!;
protected readonly List<T> behaviours = new(32);
protected readonly FastList<T> behaviours = new(32);
public IUniverse Universe { get; private set; } = null!;

View File

@@ -10,7 +10,7 @@ public class BehaviourController : BaseEntity, IBehaviourController
public Event<IBehaviourController, IBehaviourController.BehaviourRemovedArguments> OnBehaviourRemoved { get; } = new();
public Event<IHasUniverseObject> OnUniverseObjectAssigned { get; } = new();
private readonly List<IBehaviour> behaviours = new(Constants.BEHAVIOURS_SIZE_INITIAL);
private readonly FastList<IBehaviour> behaviours = new(Constants.BEHAVIOURS_SIZE_INITIAL);
private IUniverseObject _universeObject = null!;

View File

@@ -0,0 +1,74 @@
using System.Collections;
using System.Collections.Generic;
namespace Engine.Core;
public class FastList<T> : IReadOnlyList<T>, IEnumerable<T> where T : notnull
{
private readonly List<T> items = [];
private readonly Dictionary<T, int> indexMap = [];
public int Count => items.Count;
public T this[int index] => items[index];
public void Add(T item)
{
indexMap[item] = items.Count;
items.Add(item);
}
public void RemoveAt(int i) => Remove(items[i], i);
public bool Remove(T item)
{
if (!indexMap.TryGetValue(item, out int index))
return false;
Remove(item, index);
return true;
}
private void Remove(T item, int index)
{
int lastIndex = items.Count - 1;
T lastItem = items[lastIndex];
items[index] = lastItem;
indexMap[lastItem] = index;
items.RemoveAt(lastIndex);
indexMap.Remove(item);
}
public void Insert(int index, T item)
{
items.Insert(index, item);
for (int i = index; i < items.Count; i++)
indexMap[items[i]] = i;
}
public void Clear()
{
items.Clear();
indexMap.Clear();
}
public bool Contains(T item) => indexMap.ContainsKey(item);
public int BinarySearch(T item, IComparer<T>? comparer = null) => items.BinarySearch(item, comparer);
public void Sort(IComparer<T> comparer)
{
items.Sort(comparer);
for (int i = 0; i < items.Count; i++)
indexMap[items[i]] = i;
}
public IEnumerator<T> GetEnumerator() => items.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public FastList() { }
public FastList(int count) { items.Capacity = count; }
}

View File

@@ -10,22 +10,25 @@ public class Pool<T> : IPool<T>
private readonly Func<T> generator = null!;
private readonly Queue<T> queue = new();
private readonly HashSet<T> queuedHashes = [];
public T Get()
{
if (!queue.TryDequeue(out T? result))
result = generator();
queuedHashes.Remove(result);
OnRemoved?.Invoke(this, result);
return result;
}
public void Return(T item)
{
if (queue.Contains(item))
if (queuedHashes.Contains(item))
return;
queue.Enqueue(item);
queuedHashes.Add(item);
OnReturned?.Invoke(this, item);
}

View File

@@ -39,8 +39,9 @@ public class UniverseEntranceManager : Behaviour
{
for (int i = toCallExitUniverses.Count - 1; i >= 0; i--)
{
toCallExitUniverses[i].ExitUniverse(Universe);
IExitUniverse exitUniverse = toCallExitUniverses[i];
toCallExitUniverses.RemoveAt(i);
exitUniverse.ExitUniverse(Universe);
}
}
@@ -48,8 +49,9 @@ public class UniverseEntranceManager : Behaviour
{
for (int i = toCallEnterUniverses.Count - 1; i >= 0; i--)
{
toCallEnterUniverses[i].EnterUniverse(Universe);
IEnterUniverse enterUniverse = toCallEnterUniverses[i];
toCallEnterUniverses.RemoveAt(i);
enterUniverse.EnterUniverse(Universe);
}
}

View File

@@ -58,6 +58,9 @@ public class TriangleBatch : ITriangleBatch
private void Flush()
{
if (verticesIndex == 0)
return;
graphicsDevice.RasterizerState = rasterizerState;
basicEffect.Projection = _projection;
basicEffect.View = _view;

Submodule Engine.Serializers/YamlDotNet added at 62048d7abe

View File

@@ -16,22 +16,3 @@ public interface INetworkCommunicator
INetworkCommunicator SubscribeToPackets<T>(Event<IConnection, T>.EventHandler callback);
INetworkCommunicator UnsubscribeFromPackets<T>(Event<IConnection, T>.EventHandler callback);
}
public interface INetworkCommunicatorClient : INetworkCommunicator
{
INetworkCommunicatorClient Connect(string address, int port, string? password = null);
INetworkCommunicatorClient SendToServer<T>(T packet, PacketDelivery packetDelivery = PacketDelivery.ReliableInOrder) where T : class, new();
}
public interface INetworkCommunicatorServer : INetworkCommunicator
{
string Password { get; }
int MaxConnectionCount { get; }
int Port { get; }
INetworkCommunicatorServer Start(int port, int maxConnectionCount, string? password = null);
INetworkCommunicatorServer SendToClient<T>(IConnection connection, T packet, PacketDelivery packetDelivery = PacketDelivery.ReliableInOrder) where T : class, new();
INetworkCommunicatorServer SendToAll<T>(T packet, PacketDelivery packetDelivery = PacketDelivery.ReliableInOrder) where T : class, new();
}

View File

@@ -0,0 +1,8 @@
namespace Engine.Systems.Network;
public interface INetworkCommunicatorClient : INetworkCommunicator
{
INetworkCommunicatorClient Connect(string address, int port, string? password = null);
INetworkCommunicatorClient SendToServer<T>(T packet, PacketDelivery packetDelivery = PacketDelivery.ReliableInOrder) where T : class, new();
}

View File

@@ -0,0 +1,13 @@
namespace Engine.Systems.Network;
public interface INetworkCommunicatorServer : INetworkCommunicator
{
string Password { get; }
int MaxConnectionCount { get; }
int Port { get; }
INetworkCommunicatorServer Start(int port, int maxConnectionCount, string? password = null);
INetworkCommunicatorServer SendToClient<T>(IConnection connection, T packet, PacketDelivery packetDelivery = PacketDelivery.ReliableInOrder) where T : class, new();
INetworkCommunicatorServer SendToAll<T>(T packet, PacketDelivery packetDelivery = PacketDelivery.ReliableInOrder) where T : class, new();
}

View File

@@ -0,0 +1,27 @@
namespace Engine.Systems.Network;
public static class TypeHasher<T>
{
private static long _fnv1a = 0;
public static long FNV1a
{
get
{
if (_fnv1a == 0)
unchecked
{
const long fnvPrime = 1099511628211;
_fnv1a = 1469598103934665603;
string typeName = typeof(T).FullName ?? typeof(T).Name;
foreach (char c in typeName)
{
_fnv1a ^= c;
_fnv1a *= fnvPrime;
}
}
return _fnv1a;
}
}
}

View File

@@ -1,10 +1,24 @@
using System;
using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenAABBExtensions
{
public static ITween TweenAABB(this AABB initialAABB, ITweenManager tweenManager, float duration, AABB targetAABB, Action<AABB> setMethod)
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(new AABB(initialAABB.LowerBoundary.Lerp(targetAABB.LowerBoundary, t), initialAABB.UpperBoundary.Lerp(targetAABB.UpperBoundary, t))));
private static readonly BoxedPool<AABB> boxedAABBPool = new(2);
public static ITween TweenAABB(this AABB initialAABB, ITweenManager tweenManager, float duration, AABB targetAABB, System.Action<AABB> setMethod)
{
Boxed<AABB> boxedInitial = boxedAABBPool.Get(initialAABB);
Boxed<AABB> boxedTarget = boxedAABBPool.Get(targetAABB);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(new AABB(boxedInitial.Value.LowerBoundary.Lerp(boxedTarget.Value.LowerBoundary, t), boxedInitial.Value.UpperBoundary.Lerp(boxedTarget.Value.UpperBoundary, t))));
tween.OnComplete(() =>
{
boxedAABBPool.Return(boxedInitial);
boxedAABBPool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -4,9 +4,21 @@ namespace Engine.Systems.Tween;
public static class TweenCamera2DExtensions
{
private static readonly BoxedPool<float> boxedFloatPool = new(2);
public static ITween TweenZoom(this ICamera2D camera2D, ITweenManager tweenManager, float duration, float targetZoom)
{
float initialZoom = camera2D.Zoom;
return tweenManager.StartTween(duration, t => camera2D.Zoom = initialZoom.Lerp(targetZoom, t));
Boxed<float> boxedInitial = boxedFloatPool.Get(camera2D.Zoom);
Boxed<float> boxedTarget = boxedFloatPool.Get(targetZoom);
ITween tween = tweenManager.StartTween(duration, t => camera2D.Zoom = boxedInitial.Value.Lerp(boxedTarget.Value, t));
tween.OnComplete(() =>
{
boxedFloatPool.Return(boxedInitial);
boxedFloatPool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -4,13 +4,28 @@ namespace Engine.Systems.Tween;
public static class TweenCircleExtensions
{
private static readonly BoxedPool<Circle> boxedCirclePool = new(2);
public static ITween TweenCircle(this Circle initialCircle, ITweenManager tweenManager, float duration, Circle targetCircle, System.Action<Circle> setMethod)
=> tweenManager.StartTween(duration,
{
Boxed<Circle> boxedInitial = boxedCirclePool.Get(initialCircle);
Boxed<Circle> boxedTarget = boxedCirclePool.Get(targetCircle);
ITween tween = tweenManager.StartTween(duration,
t => setMethod?.Invoke(
new Circle(
initialCircle.Center.Lerp(targetCircle.Center, t),
initialCircle.Diameter.Lerp(targetCircle.Diameter, t)
boxedInitial.Value.Center.Lerp(boxedTarget.Value.Center, t),
boxedInitial.Value.Diameter.Lerp(boxedTarget.Value.Diameter, t)
)
)
);
tween.OnComplete(() =>
{
boxedCirclePool.Return(boxedInitial);
boxedCirclePool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -4,6 +4,9 @@ namespace Engine.Systems.Tween;
public static class TweenColorExtensions
{
private static readonly BoxedPool<ColorHSV> boxedColorHSVPool = new(2);
private static readonly BoxedPool<ColorHSVA> boxedColorHSVAPool = new(2);
public static ITween TweenColor(this ColorRGB initialColorRGB, ITweenManager tweenManager, float duration, ColorRGB targetColorRGB, System.Action<ColorRGB> setMethod)
=> TweenColor((ColorHSV)initialColorRGB, tweenManager, duration, (ColorHSV)targetColorRGB, color => setMethod?.Invoke(color));
@@ -11,8 +14,34 @@ public static class TweenColorExtensions
=> TweenColor((ColorHSVA)initialColorRGBA, tweenManager, duration, (ColorHSVA)targetColorRGBA, color => setMethod?.Invoke(color));
public static ITween TweenColor(this ColorHSV initialColorHSV, ITweenManager tweenManager, float duration, ColorHSV targetColorHSV, System.Action<ColorHSV> setMethod)
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialColorHSV.Lerp(targetColorHSV, t)));
{
Boxed<ColorHSV> boxedInitial = boxedColorHSVPool.Get(initialColorHSV);
Boxed<ColorHSV> boxedTarget = boxedColorHSVPool.Get(targetColorHSV);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value.Lerp(boxedTarget.Value, t)));
tween.OnComplete(() =>
{
boxedColorHSVPool.Return(boxedInitial);
boxedColorHSVPool.Return(boxedTarget);
});
return tween;
}
public static ITween TweenColor(this ColorHSVA initialColorHSVA, ITweenManager tweenManager, float duration, ColorHSVA targetColorHSVA, System.Action<ColorHSVA> setMethod)
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialColorHSVA.Lerp(targetColorHSVA, t)));
{
Boxed<ColorHSVA> boxedInitial = boxedColorHSVAPool.Get(initialColorHSVA);
Boxed<ColorHSVA> boxedTarget = boxedColorHSVAPool.Get(targetColorHSVA);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value.Lerp(boxedTarget.Value, t)));
tween.OnComplete(() =>
{
boxedColorHSVAPool.Return(boxedInitial);
boxedColorHSVAPool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -4,13 +4,28 @@ namespace Engine.Systems.Tween;
public static class TweenLine2DEquationExtensions
{
private static readonly BoxedPool<Line2DEquation> boxedLine2DEquationPool = new(2);
public static ITween TweenLine2DEquation(this Line2DEquation initialLine2DEquation, ITweenManager tweenManager, float duration, Line2DEquation targetLine2DEquation, System.Action<Line2DEquation> setMethod)
=> tweenManager.StartTween(duration,
{
Boxed<Line2DEquation> boxedInitial = boxedLine2DEquationPool.Get(initialLine2DEquation);
Boxed<Line2DEquation> boxedTarget = boxedLine2DEquationPool.Get(targetLine2DEquation);
ITween tween = tweenManager.StartTween(duration,
t => setMethod?.Invoke(
new Line2DEquation(
initialLine2DEquation.Slope.Lerp(targetLine2DEquation.Slope, t),
initialLine2DEquation.OffsetY.Lerp(targetLine2DEquation.OffsetY, t)
boxedInitial.Value.Slope.Lerp(boxedTarget.Value.Slope, t),
boxedInitial.Value.OffsetY.Lerp(boxedTarget.Value.OffsetY, t)
)
)
);
tween.OnComplete(() =>
{
boxedLine2DEquationPool.Return(boxedInitial);
boxedLine2DEquationPool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -4,13 +4,28 @@ namespace Engine.Systems.Tween;
public static class TweenLine2DExtensions
{
private static readonly BoxedPool<Line2D> boxedLine2DPool = new(2);
public static ITween TweenLine2D(this Line2D initialLine2D, ITweenManager tweenManager, float duration, Line2D targetLine2D, System.Action<Line2D> setMethod)
=> tweenManager.StartTween(duration,
{
Boxed<Line2D> boxedInitial = boxedLine2DPool.Get(initialLine2D);
Boxed<Line2D> boxedTarget = boxedLine2DPool.Get(targetLine2D);
ITween tween = tweenManager.StartTween(duration,
t => setMethod?.Invoke(
new Line2D(
initialLine2D.From.Lerp(targetLine2D.From, t),
initialLine2D.To.Lerp(targetLine2D.To, t)
boxedInitial.Value.From.Lerp(boxedTarget.Value.From, t),
boxedInitial.Value.To.Lerp(boxedTarget.Value.To, t)
)
)
);
tween.OnComplete(() =>
{
boxedLine2DPool.Return(boxedInitial);
boxedLine2DPool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -0,0 +1,41 @@
using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenPrimitiveExtensions
{
private static readonly BoxedPool<float> boxedFloatPool = new(2);
private static readonly BoxedPool<int> boxedIntPool = new(2);
public static ITween TweenFloat(this float initialFloat, ITweenManager tweenManager, float duration, float targetFloat, System.Action<float> setMethod)
{
Boxed<float> boxedInitial = boxedFloatPool.Get(initialFloat);
Boxed<float> boxedTarget = boxedFloatPool.Get(targetFloat);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value.Lerp(boxedTarget.Value, t)));
tween.OnComplete(() =>
{
boxedFloatPool.Return(boxedInitial);
boxedFloatPool.Return(boxedTarget);
});
return tween;
}
public static ITween TweenInt(this int initialInt, ITweenManager tweenManager, float duration, int targetInt, System.Action<float> setMethod)
{
Boxed<int> boxedInitial = boxedIntPool.Get(initialInt);
Boxed<int> boxedTarget = boxedIntPool.Get(targetInt);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value + (boxedTarget.Value - boxedInitial.Value) * t));
tween.OnComplete(() =>
{
boxedIntPool.Return(boxedInitial);
boxedIntPool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -1,17 +1,31 @@
using System;
using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenProjection1DExtensions
{
public static ITween TweenProjection1D(this Projection1D initialProjection1D, ITweenManager tweenManager, float duration, Projection1D targetProjection1D, Action<Projection1D> setMethod)
=> tweenManager.StartTween(duration,
private static readonly BoxedPool<Projection1D> boxedProjection1DPool = new(2);
public static ITween TweenProjection1D(this Projection1D initialProjection1D, ITweenManager tweenManager, float duration, Projection1D targetProjection1D, System.Action<Projection1D> setMethod)
{
Boxed<Projection1D> boxedInitial = boxedProjection1DPool.Get(initialProjection1D);
Boxed<Projection1D> boxedTarget = boxedProjection1DPool.Get(targetProjection1D);
ITween tween = tweenManager.StartTween(duration,
t => setMethod?.Invoke(
new Projection1D(
initialProjection1D.Min.Lerp(targetProjection1D.Min, t),
initialProjection1D.Max.Lerp(targetProjection1D.Max, t)
boxedInitial.Value.Min.Lerp(boxedTarget.Value.Min, t),
boxedInitial.Value.Max.Lerp(boxedTarget.Value.Max, t)
)
)
);
tween.OnComplete(() =>
{
boxedProjection1DPool.Return(boxedInitial);
boxedProjection1DPool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -1,10 +1,24 @@
using System;
using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenQuaternionExtensions
{
public static ITween TweenQuaternion(this Quaternion initialQuaternion, ITweenManager tweenManager, float duration, Quaternion targetQuaternion, Action<Quaternion> setMethod)
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialQuaternion.SLerp(targetQuaternion, t)));
private static readonly BoxedPool<Quaternion> boxedQuaternionPool = new(2);
public static ITween TweenQuaternion(this Quaternion initialQuaternion, ITweenManager tweenManager, float duration, Quaternion targetQuaternion, System.Action<Quaternion> setMethod)
{
Boxed<Quaternion> boxedInitial = boxedQuaternionPool.Get(initialQuaternion);
Boxed<Quaternion> boxedTarget = boxedQuaternionPool.Get(targetQuaternion);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value.SLerp(boxedTarget.Value, t)));
tween.OnComplete(() =>
{
boxedQuaternionPool.Return(boxedInitial);
boxedQuaternionPool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -5,40 +5,23 @@ namespace Engine.Systems.Tween;
public static class TweenTransform2DExtensions
{
public static ITween TweenPosition(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D targetPosition)
{
Vector2D initialPosition = transform2D.Position;
return tweenManager.StartTween(duration, t => transform2D.Position = initialPosition.Lerp(targetPosition, t));
}
=> transform2D.Position.TweenVector2D(tweenManager, duration, targetPosition, x => transform2D.Position = x);
public static ITween TweenScale(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D targetScale)
{
Vector2D initialScale = transform2D.Scale;
return tweenManager.StartTween(duration, t => transform2D.Scale = initialScale.Lerp(targetScale, t));
}
=> transform2D.Scale.TweenVector2D(tweenManager, duration, targetScale, x => transform2D.Scale = x);
public static ITween TweenRotation(this ITransform2D transform2D, ITweenManager tweenManager, float duration, float targetRotation)
{
float initialRotation = transform2D.Rotation;
return tweenManager.StartTween(duration, t => transform2D.Rotation = initialRotation.Lerp(targetRotation, t));
}
=> transform2D.Rotation.TweenFloat(tweenManager, duration, targetRotation, x => transform2D.Rotation = x);
public static ITween TweenLocalPosition(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D targetLocalPosition)
{
Vector2D initialLocalPosition = transform2D.LocalPosition;
return tweenManager.StartTween(duration, t => transform2D.LocalPosition = initialLocalPosition.Lerp(targetLocalPosition, t));
}
=> transform2D.LocalPosition.TweenVector2D(tweenManager, duration, targetLocalPosition, x => transform2D.LocalPosition = x);
public static ITween TweenLocalScale(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D targetLocalScale)
{
Vector2D initialLocalScale = transform2D.LocalScale;
return tweenManager.StartTween(duration, t => transform2D.LocalScale = initialLocalScale.Lerp(targetLocalScale, t));
}
=> transform2D.LocalScale.TweenVector2D(tweenManager, duration, targetLocalScale, x => transform2D.LocalScale = x);
public static ITween TweenLocalRotation(this ITransform2D transform2D, ITweenManager tweenManager, float duration, float targetLocalRotation)
{
float initialLocalRotation = transform2D.LocalRotation;
return tweenManager.StartTween(duration, t => transform2D.LocalRotation = initialLocalRotation.Lerp(targetLocalRotation, t));
}
=> transform2D.LocalRotation.TweenFloat(tweenManager, duration, targetLocalRotation, x => transform2D.LocalRotation = x);
public static ITween TweenPositionAdditive(this ITransform2D transform2D, ITweenManager tweenManager, float duration, Vector2D additivePosition)
{
Vector2D progressedPosition = Vector2D.Zero;

View File

@@ -1,18 +1,31 @@
using System;
using Engine.Core;
namespace Engine.Systems.Tween;
public static class TweenTriangleExtensions
{
public static ITween TweenTriangle(this Triangle initialTriangle, ITweenManager tweenManager, float duration, Triangle targetTriangle, Action<Triangle> setMethod)
=> tweenManager.StartTween(duration,
t => setMethod?.Invoke(
private static readonly BoxedPool<Triangle> boxedTrianglePool = new(2);
public static ITween TweenTriangle(this Triangle initialTriangle, ITweenManager tweenManager, float duration, Triangle targetTriangle, System.Action<Triangle> setMethod)
{
Boxed<Triangle> boxedInitial = boxedTrianglePool.Get(initialTriangle);
Boxed<Triangle> boxedTarget = boxedTrianglePool.Get(targetTriangle);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(
new Triangle(
initialTriangle.A.Lerp(targetTriangle.A, t),
initialTriangle.B.Lerp(targetTriangle.B, t),
initialTriangle.C.Lerp(targetTriangle.C, t)
boxedInitial.Value.A.Lerp(boxedTarget.Value.A, t),
boxedInitial.Value.B.Lerp(boxedTarget.Value.B, t),
boxedInitial.Value.C.Lerp(boxedTarget.Value.C, t)
)
)
);
tween.OnComplete(() =>
{
boxedTrianglePool.Return(boxedInitial);
boxedTrianglePool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -4,6 +4,21 @@ namespace Engine.Systems.Tween;
public static class TweenVector2DExtensions
{
private static readonly BoxedPool<Vector2D> boxedVector2DPool = new(2);
public static ITween TweenVector2D(this Vector2D initialVector2D, ITweenManager tweenManager, float duration, Vector2D targetVector2D, System.Action<Vector2D> setMethod)
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialVector2D.Lerp(targetVector2D, t)));
{
Boxed<Vector2D> boxedInitial = boxedVector2DPool.Get(initialVector2D);
Boxed<Vector2D> boxedTarget = boxedVector2DPool.Get(targetVector2D);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value.Lerp(boxedTarget.Value, t)));
tween.OnComplete(() =>
{
boxedVector2DPool.Return(boxedInitial);
boxedVector2DPool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -4,6 +4,21 @@ namespace Engine.Systems.Tween;
public static class TweenVector3DExtensions
{
private static readonly BoxedPool<Vector3D> boxedVector3DPool = new(2);
public static ITween TweenVector3D(this Vector3D initialVector3D, ITweenManager tweenManager, float duration, Vector3D targetVector3D, System.Action<Vector3D> setMethod)
=> tweenManager.StartTween(duration, t => setMethod?.Invoke(initialVector3D.Lerp(targetVector3D, t)));
{
Boxed<Vector3D> boxedInitial = boxedVector3DPool.Get(initialVector3D);
Boxed<Vector3D> boxedTarget = boxedVector3DPool.Get(targetVector3D);
ITween tween = tweenManager.StartTween(duration, t => setMethod?.Invoke(boxedInitial.Value.Lerp(boxedTarget.Value, t)));
tween.OnComplete(() =>
{
boxedVector3DPool.Return(boxedInitial);
boxedVector3DPool.Return(boxedTarget);
});
return tween;
}
}

View File

@@ -0,0 +1,12 @@
using Engine.Core;
namespace Engine.Systems.Tween;
public class Boxed<T> where T : struct
{
public Event<Boxed<T>, BoxedValueChangedArguments> OnValueChanged { get; } = new();
public T Value { get; set; } = default;
public readonly record struct BoxedValueChangedArguments(T PreviousValue, T CurrentValue);
}

View File

@@ -0,0 +1,14 @@
using Engine.Core;
namespace Engine.Systems.Tween;
public class BoxedPool<T>(int initialCapacity = 1) : Pool<Boxed<T>>(() => new(), initialCapacity) where T : struct;
public static class BoxedPoolExtensions
{
public static Boxed<T> Get<T>(this BoxedPool<T> boxedPool, T value) where T : struct
{
Boxed<T> boxed = boxedPool.Get();
boxed.Value = value;
return boxed;
}
}