-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from theowiik/property-fixes
Add timer factory
- Loading branch information
Showing
3 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Timer = Godot.Timer; | ||
|
||
namespace GodotSharper; | ||
|
||
/// <summary> | ||
/// A factory class for creating Timer objects with various configurations. | ||
/// </summary> | ||
public static class TimerFactory | ||
{ | ||
/// <summary> | ||
/// Creates a new Timer object that starts automatically and triggers only once after the specified wait time. Note: | ||
/// also destroys the timer after it triggers! | ||
/// </summary> | ||
/// <param name="waitTime">The time to wait before triggering the timer, in seconds.</param> | ||
/// <param name="onTimeout">An optional action to execute when the timer triggers.</param> | ||
/// <returns>The newly created Timer object.</returns> | ||
public static Timer StartedOneShot(double waitTime, Action onTimeout = null) | ||
{ | ||
var timer = InternalCreateTimer(true, true, waitTime); | ||
timer.Timeout += () => | ||
{ | ||
onTimeout?.Invoke(); | ||
timer.QueueFree(); | ||
}; | ||
|
||
return timer; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new Timer object that starts automatically and triggers repeatedly after the specified wait time. | ||
/// </summary> | ||
/// <param name="waitTime">The time to wait before each trigger of the timer, in seconds.</param> | ||
/// <param name="onTimeout">An optional action to execute when the timer triggers.</param> | ||
/// <returns>The newly created Timer object.</returns> | ||
public static Timer StartedRepeating(double waitTime, Action onTimeout = null) | ||
{ | ||
var timer = InternalCreateTimer(true, false, waitTime); | ||
timer.Timeout += () => onTimeout?.Invoke(); | ||
|
||
return timer; | ||
} | ||
|
||
private static Timer InternalCreateTimer(bool autostart, bool oneShot, double waitTime) | ||
{ | ||
return new Timer | ||
{ | ||
Autostart = autostart, | ||
OneShot = oneShot, | ||
WaitTime = waitTime | ||
}; | ||
} | ||
} |