Skip to content

Commit

Permalink
remove loops from wait, take no arg in callback, add loop()
Browse files Browse the repository at this point in the history
  • Loading branch information
Geokureli committed Feb 23, 2024
1 parent 425daef commit 0d23100
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
20 changes: 16 additions & 4 deletions flixel/util/FlxTimer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,30 @@ import flixel.util.FlxDestroyUtil.IFlxDestroyable;
*/
class FlxTimer implements IFlxDestroyable
{
/**
* Handy tool to create and start a `FlxTimer`
* @param time The duration of the timer, in seconds. If `0` then `onComplete`
* fires on the next game update.
* @param onComplete Triggered whenever the time runs out
* @return The `FlxTimer` instance
*/
public static inline function wait(time:Float, onComplete:()->Void)
{
return new FlxTimer().start(time, (_)->onComplete());
}

/**
* Handy tool to create and start a `FlxTimer`
* @param time The duration of the timer, in seconds. If `0` then `onComplete`
* fires on the next game update, and the `loops` argument is ignored.
* @param onComplete Optional, triggered whenever the time runs out, once for each loop.
* Callback should be formed `onTimer(Timer:FlxTimer);`
* @param onComplete triggered whenever the time runs out, once for each loop.
* Should take a single `Int` arg representing the number of completed loops
* @param loops How many times the timer should go off. `0` means "looping forever".
* @return The `FlxTimer` instance
*/
public static inline function wait(time:Float, onComplete:(FlxTimer)->Void, loops = 1)
public static inline function loop(time:Float, onComplete:(loop:Int)->Void, loops:Int)
{
return new FlxTimer().start(time, onComplete, loops);
return new FlxTimer().start(time, (t)->onComplete(loops - t.loopsLeft), loops);
}

/**
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/src/flixel/util/FlxTimerTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class FlxTimerTest extends FlxTest
{
var calledBack:Bool = false;
timer.start(0, function(_) calledBack = true);

Assert.isFalse(calledBack);
step();

Assert.isTrue(calledBack);
}

Expand Down Expand Up @@ -67,4 +68,28 @@ class FlxTimerTest extends FlxTest
Assert.isTrue(timer2.progress > 0);
Assert.isTrue(timer3.progress > 0);
}

@Test
function testWait()
{
var calledBack = false;
function onComplete() { calledBack = true; }
final timer1 = FlxTimer.wait(2/60, onComplete);
final timer2 = FlxTimer.wait(0.0001, timer1.cancel);

step(3);
Assert.isTrue(timer1.finished && calledBack);
}

@Test
function testLoop()
{
var calledBack = false;
function onComplete(n) { calledBack = true; }
final timer1 = FlxTimer.loop(2/60, onComplete, 0);
final timer2 = FlxTimer.loop(0.0001, (loop)->{ if (loop == 3) timer1.cancel(); }, 3);

step(3);
Assert.isTrue(timer1.finished && calledBack);
}
}

0 comments on commit 0d23100

Please sign in to comment.