Skip to content

Commit

Permalink
Add FlxTimer.wait and FlxTimer.loop (#3040)
Browse files Browse the repository at this point in the history
* add FlxTimer.wait

* fix arg

* remove loops from wait, take no arg in callback, add loop()

* simplify wait

* docs
  • Loading branch information
Geokureli authored Feb 24, 2024
1 parent 986d57b commit c90b25a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
39 changes: 32 additions & 7 deletions flixel/util/FlxTimer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,32 @@ 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 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 loop(time:Float, onComplete:(loop:Int)->Void, loops:Int)
{
return new FlxTimer().start(time, (t)->onComplete(t.elapsedLoops), loops);
}

/**
* The global timer manager that handles global timers
* @since 4.2.0
Expand Down Expand Up @@ -54,10 +80,9 @@ class FlxTimer implements IFlxDestroyable
public var finished:Bool = false;

/**
* Function that gets called when timer completes.
* Callback should be formed "onTimer(Timer:FlxTimer);"
* Called when timer completes. The function header should be `(timer:FlxTimer)`
*/
public var onComplete:FlxTimer->Void;
public var onComplete:(FlxTimer)->Void;

/**
* Read-only: check how much time is left on the timer.
Expand Down Expand Up @@ -115,14 +140,14 @@ class FlxTimer implements IFlxDestroyable
/**
* Starts the timer and adds the timer to the timer manager.
*
* @param time How many seconds it takes for the timer to go off.
* If 0 then timer will fire OnComplete callback only once at the first call of update method (which means that Loops argument will be ignored).
* @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);"
* The function header should be `(timer:FlxTimer)`
* @param loops How many times the timer should go off. 0 means "looping forever".
* @return A reference to itself (handy for chaining or whatever).
*/
public function start(time:Float = 1, ?onComplete:FlxTimer->Void, loops:Int = 1):FlxTimer
public function start(time:Float = 1, ?onComplete:(FlxTimer)->Void, loops:Int = 1):FlxTimer
{
if (manager != null && !_inManager)
{
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 c90b25a

Please sign in to comment.