Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FlxTimer.wait and FlxTimer.loop #3040

Merged
merged 5 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading