Skip to content

Commit

Permalink
Improve API for creating tweens
Browse files Browse the repository at this point in the history
  • Loading branch information
Inspiaaa committed Sep 5, 2024
1 parent c2d9e2b commit 237eb4f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/Coroutines/Coroutine.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;

namespace HCoroutines;
Expand All @@ -22,6 +23,16 @@ public Coroutine(
{
this.routine = routine;
}

public Coroutine(
Func<Coroutine, IEnumerator> creator,
CoProcessMode processMode = CoProcessMode.Inherit,
CoRunMode runMode = CoRunMode.Inherit
)
: base(processMode, runMode)
{
this.routine = creator(this);
}

protected override void OnEnter()
{
Expand Down
16 changes: 14 additions & 2 deletions src/Coroutines/RepeatCoroutine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RepeatCoroutine : CoroutineBase
{
private readonly int repeatTimes;
private int currentRepeatCount;
private readonly Func<RepeatCoroutine, CoroutineBase> coroutineCreator;
private readonly Func<CoroutineBase> coroutineCreator;

private bool IsInfinite => repeatTimes == -1;

Expand All @@ -25,6 +25,18 @@ public RepeatCoroutine(
CoRunMode runMode = CoRunMode.Inherit
)
: base(processMode, runMode)
{
this.repeatTimes = repeatTimes;
this.coroutineCreator = () => coroutineCreator(this);
}

public RepeatCoroutine(
int repeatTimes,
Func<CoroutineBase> coroutineCreator,
CoProcessMode processMode = CoProcessMode.Inherit,
CoRunMode runMode = CoRunMode.Inherit
)
: base(processMode, runMode)
{
this.repeatTimes = repeatTimes;
this.coroutineCreator = coroutineCreator;
Expand All @@ -46,7 +58,7 @@ protected override void OnStart()
private void Repeat()
{
currentRepeatCount += 1;
CoroutineBase coroutine = coroutineCreator.Invoke(this);
CoroutineBase coroutine = coroutineCreator();
StartCoroutine(coroutine);
}

Expand Down
12 changes: 8 additions & 4 deletions src/Coroutines/TweenCoroutine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ namespace HCoroutines;
/// </summary>
public class TweenCoroutine : CoroutineBase
{
private readonly Func<Tween> createTween;
private readonly Action<Tween> setupTween;
private Tween tween;

public TweenCoroutine(Func<Tween> createTween, CoRunMode runMode = CoRunMode.Inherit)
// TODO: Create additional methods for creating Tween Coroutines (e.g. create Tween on Manager, setup via Action,
// or bound to a node)

public TweenCoroutine(Action<Tween> setupTween, CoRunMode runMode = CoRunMode.Inherit)
: base(CoProcessMode.Inherit, runMode)
{
this.createTween = createTween;
this.setupTween = setupTween;
}

protected override void OnStart()
{
tween = createTween();
tween = Manager.CreateTween();
setupTween(tween);

if (!tween.IsValid() || !tween.IsRunning()) {
Kill();
Expand Down

0 comments on commit 237eb4f

Please sign in to comment.