TimeSpan GetTimeToWaitBeforeLoop(TimeSpan lastTimeLoop)
{
var numTicks = ScummHelper.ToTicks(timeToWait);
// Notify the script about how much time has passed, in ticks (60 ticks per second)
if (VariableTimer.HasValue)
_variables[VariableTimer.Value] = numTicks;
if (VariableTimerTotal.HasValue)
_variables[VariableTimerTotal.Value] += numTicks;
// Determine how long to wait before the next loop iteration should start
deltaTicks = VariableTimerNext.HasValue ? _variables[VariableTimerNext.Value] : 4;
if (deltaTicks < 1) // Ensure we don't get into an endless loop
deltaTicks = 1; // by not decreasing sleepers.
timeToWait = ScummHelper.ToTimeSpan(deltaTicks);
if (timeToWait > lastTimeLoop)
{
timeToWait -= lastTimeLoop;
}
else
{
timeToWait = TimeSpan.Zero;
}
return timeToWait;
}