Exemple #1
0
        // TODO Find a proper

        public static IEnumerator <WaitCommand> Visit(this IEnumerator <WaitCommand> routine, Action <WaitCommand> visit)
        {
            while (routine.MoveNext())
            {
                // Recursively skip subroutines
                var currentInstruction = routine.Current;
                if (currentInstruction.IsRoutine)
                {
                    var instructionRoutines = routine.Current;

                    var wrappedRoutines = new IEnumerator <WaitCommand> [instructionRoutines.RoutineCount];
                    for (int i = 0; i < instructionRoutines.RoutineCount; i++)
                    {
                        wrappedRoutines[i] = Visit(instructionRoutines.GetRoutine(i), visit);
                    }

                    yield return(WaitCommand.Interleave(wrappedRoutines));
                }
                else
                {
                    visit(currentInstruction);
                    yield return(currentInstruction);
                }
            }
        }
Exemple #2
0
        private void FetchNextInstruction()
        {
            // Push/Pop (sub-)coroutines until we get another instruction or we run out of instructions.
            while (!_isDone && IsRunningInstructionFinished)
            {
                if (_fibre.MoveNext())
                {
                    var newInstruction = _fibre.Current;
                    if (newInstruction.IsRoutine)
                    {
                        for (int i = 0; i < newInstruction.RoutineCount; i++)
                        {
                            var subroutine        = newInstruction.GetRoutine(i);
                            var startedSubroutine = _createSubroutine(subroutine);
                            _activeSubroutines.Add(startedSubroutine);
                        }

                        _activeWaitCommand = WaitCommand.DontWait;
                    }
                    else
                    {
                        _activeWaitCommand = newInstruction;
                    }
                }
                else
                {
                    _isDone = true;
                }
            }
        }
Exemple #3
0
 public void Initialize(IEnumerator <WaitCommand> fibre)
 {
     _fibre = fibre;
     _activeSubroutines.Clear();
     _activeWaitCommand = WaitCommand.DontWait;
     _isDone            = false;
     Update(new Duration(seconds: 0f, frameCount: 0));
 }
Exemple #4
0
 public void Reset()
 {
     _fibre = null;
     for (int i = 0; i < _activeSubroutines.Count; i++)
     {
         var activeRoutine = _activeSubroutines[i];
         activeRoutine.Dispose();
     }
     _activeSubroutines.Clear();
     _activeWaitCommand = WaitCommand.DontWait;
 }
Exemple #5
0
        public Duration Update(Duration timePassed)
        {
            // Find a new instruction and make it the current one
            if (IsRunningInstructionFinished)
            {
                FetchNextInstruction();
            }

            Duration leftOverTime = timePassed;

            // Update the current instruction
            if (!_isDone)
            {
                if (_activeSubroutines.Count > 0)
                {
                    for (int i = _activeSubroutines.Count - 1; i >= 0; i--)
                    {
                        var subroutine         = _activeSubroutines[i];
                        var subroutineTimeLeft = subroutine.Update(timePassed);
                        if (subroutine.IsDone)
                        {
                            subroutine.Dispose();
                            _activeSubroutines.RemoveAt(i);
                        }
                        leftOverTime = Duration.Min(leftOverTime, subroutineTimeLeft);
                    }
                }
                else
                {
                    leftOverTime       = timePassed - _activeWaitCommand.Duration.Value;
                    _activeWaitCommand = _activeWaitCommand - timePassed;
                }

                // TODO Check if we're stuck on a subroutine, or a wait command
                //      if not continue
                if (_activeWaitCommand.IsFinished && _activeSubroutines.Count == 0)
                {
                    leftOverTime = Update(leftOverTime);
                }
            }
            return(leftOverTime);
        }
Exemple #6
0
 public EventResult(AsyncResult <T> asyncResult, WaitCommand waitUntilReady)
 {
     _asyncResult   = asyncResult;
     WaitUntilReady = waitUntilReady;
 }
Exemple #7
0
        public static IEnumerator <WaitCommand> AndThen(this WaitCommand first, WaitCommand second)
        {
            yield return(first);

            yield return(second);
        }
Exemple #8
0
 public static IEnumerator <WaitCommand> AndThen(this WaitCommand first, IEnumerator <WaitCommand> second)
 {
     return(AndThen(first, second.AsWaitCommand()));
 }
Exemple #9
0
 public static IEnumerator <WaitCommand> AndThen(this IEnumerator <WaitCommand> first, WaitCommand second)
 {
     return(AndThen(first.AsWaitCommand(), second));
 }
Exemple #10
0
 public static IEnumerator <WaitCommand> RunUntil(this WaitCommand waitCommand, Predicate predicate)
 {
     return(RunUntil(waitCommand.AsRoutine, predicate));
 }
Exemple #11
0
 public static IEnumerator <WaitCommand> WaitWhile(this WaitCommand waitCommand, Predicate predicate)
 {
     return(WaitWhile(waitCommand.AsRoutine, predicate));
 }
Exemple #12
0
 public static WaitCommand AsWaitCommand(this IEnumerator <WaitCommand> coroutine)
 {
     return(WaitCommand.WaitRoutine(coroutine));
 }