Skip to content

Commit

Permalink
Merge pull request #106 from AnnulusGames/feature-addto-behaviour
Browse files Browse the repository at this point in the history
Add: LinkBehaviour to AddTo
  • Loading branch information
AnnulusGames authored Mar 17, 2024
2 parents 5af347d + 68df576 commit b222616
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,47 @@ namespace LitMotion
[AddComponentMenu("")]
internal sealed class MotionHandleLinker : MonoBehaviour
{
FastListCore<MotionHandle> handleList = new(8);
FastListCore<MotionHandle> cancelOnDestroyList = new(8);
FastListCore<MotionHandle> cancelOnDisableList = new(8);
FastListCore<MotionHandle> completeOnDisableList = new(8);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Register(MotionHandle handle)
public void Register(MotionHandle handle, LinkBehaviour linkBehaviour)
{
handleList.Add(handle);
switch (linkBehaviour)
{
case LinkBehaviour.CancelOnDestroy:
cancelOnDestroyList.Add(handle);
break;
case LinkBehaviour.CancelOnDisable:
cancelOnDisableList.Add(handle);
break;
case LinkBehaviour.CompleteOnDisable:
completeOnDisableList.Add(handle);
break;
}
}

void OnDisable()
{
var cancelSpan = cancelOnDisableList.AsSpan();
for (int i = 0; i < cancelSpan.Length; i++)
{
ref var handle = ref cancelSpan[i];
if (handle.IsActive()) handle.Cancel();
}

var completeSpan = completeOnDisableList.AsSpan();
for (int i = 0; i < completeSpan.Length; i++)
{
ref var handle = ref completeSpan[i];
if (handle.IsActive()) handle.Complete();
}
}

void OnDestroy()
{
var span = handleList.AsSpan();
var span = cancelOnDestroyList.AsSpan();
for (int i = 0; i < span.Length; i++)
{
ref var handle = ref span[i];
Expand Down
12 changes: 12 additions & 0 deletions src/LitMotion/Assets/LitMotion/Runtime/LinkBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace LitMotion
{
/// <summary>
/// Specifies the behavior when linking motion to GameObject with AddTo.
/// </summary>
public enum LinkBehaviour
{
CancelOnDestroy = 0,
CancelOnDisable = 1,
CompleteOnDisable = 2
}
}
2 changes: 2 additions & 0 deletions src/LitMotion/Assets/LitMotion/Runtime/LinkBehaviour.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 26 additions & 2 deletions src/LitMotion/Assets/LitMotion/Runtime/MotionHandleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,19 @@ public static MotionHandle AddTo(this MotionHandle handle, CompositeMotionHandle
/// <param name="target">Target object</param>
public static MotionHandle AddTo(this MotionHandle handle, GameObject target)
{
GetOrAddComponent<MotionHandleLinker>(target).Register(handle);
GetOrAddComponent<MotionHandleLinker>(target).Register(handle, LinkBehaviour.CancelOnDestroy);
return handle;
}

/// <summary>
/// Link the motion lifecycle to the target object.
/// </summary>
/// <param name="handle">This motion handle</param>
/// <param name="target">Target object</param>
/// <param name="linkBehaviour">Link behaviour</param>
public static MotionHandle AddTo(this MotionHandle handle, GameObject target, LinkBehaviour linkBehaviour)
{
GetOrAddComponent<MotionHandleLinker>(target).Register(handle, linkBehaviour);
return handle;
}

Expand All @@ -68,7 +80,19 @@ public static MotionHandle AddTo(this MotionHandle handle, GameObject target)
/// <param name="target">Target object</param>
public static MotionHandle AddTo(this MotionHandle handle, Component target)
{
GetOrAddComponent<MotionHandleLinker>(target.gameObject).Register(handle);
GetOrAddComponent<MotionHandleLinker>(target.gameObject).Register(handle, LinkBehaviour.CancelOnDestroy);
return handle;
}

/// <summary>
/// Link the motion lifecycle to the target object.
/// </summary>
/// <param name="handle">This motion handle</param>
/// <param name="target">Target object</param>
/// <param name="linkBehaviour">Link behaviour</param>
public static MotionHandle AddTo(this MotionHandle handle, Component target, LinkBehaviour linkBehaviour)
{
GetOrAddComponent<MotionHandleLinker>(target.gameObject).Register(handle, linkBehaviour);
return handle;
}

Expand Down
28 changes: 28 additions & 0 deletions src/LitMotion/Assets/LitMotion/Tests/Runtime/AddToTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@ public IEnumerator Test_AddTo()
Assert.IsTrue(canceled);
}

[UnityTest]
public IEnumerator Test_AddTo_CancelOnDisable()
{
var canceled = false;
var obj = new GameObject("Target");
var handle = LMotion.Create(0f, 1f, 2f)
.WithOnCancel(() => canceled = true)
.RunWithoutBinding()
.AddTo(obj, LinkBehaviour.CancelOnDisable);
yield return new WaitForSeconds(0.1f);
obj.SetActive(false);
Assert.IsTrue(canceled);
}

[UnityTest]
public IEnumerator Test_AddTo_CompleteOnDisable()
{
var completed = false;
var obj = new GameObject("Target");
var handle = LMotion.Create(0f, 1f, 2f)
.WithOnComplete(() => completed = true)
.RunWithoutBinding()
.AddTo(obj, LinkBehaviour.CompleteOnDisable);
yield return new WaitForSeconds(0.1f);
obj.SetActive(false);
Assert.IsTrue(completed);
}

[UnityTest]
public IEnumerator Test_AddTo_MonoBehaviour()
{
Expand Down

0 comments on commit b222616

Please sign in to comment.