Skip to content

Commit

Permalink
add WaintUntilForSeconds yield instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
fraidev committed Oct 24, 2023
1 parent adecba5 commit d4f80a6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Runtime/Scripts/Helpers/WaitUntilForSeconds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using UnityEngine;

namespace TezosSDK.Helpers
{
public class WaitUntilForSeconds: CustomYieldInstruction
{
float pauseTime;
float timer;
bool waitingForFirst;
Func<bool> myChecker;
Action<float> onInterrupt;
bool alwaysTrue;

public WaitUntilForSeconds(Func<bool> myChecker, float pauseTime,
Action<float> onInterrupt = null)
{
this.myChecker = myChecker;
this.pauseTime = pauseTime;
this.onInterrupt = onInterrupt;

waitingForFirst = true;
}

public override bool keepWaiting
{
get
{
bool checkThisTurn = myChecker();
if (waitingForFirst)
{
if (checkThisTurn)
{
waitingForFirst = false;
timer = pauseTime;
alwaysTrue = true;
}
}
else
{
timer -= Time.deltaTime;

if (onInterrupt != null && !checkThisTurn && alwaysTrue)
{
onInterrupt(timer);
}
alwaysTrue &= checkThisTurn;

// Alternate version: Interrupt the timer on false,
// and restart the wait
// if (!alwaysTrue || timer <= 0)

if (timer <= 0)
{
if (alwaysTrue)
{
return false;
}
else
{
waitingForFirst = true;
}
}
}

return true;
}
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Scripts/Helpers/WaitUntilForSeconds.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d4f80a6

Please sign in to comment.