Пример #1
0
        /// <summary>
        /// Called by mediator to perform next action in sequence
        /// </summary>
        /// <param name="random"></param>
        /// <returns>
        /// True when there are additional sequence actions to perform
        /// False when performing the last action in sequence
        /// False in the event of an action state check failure(rollback will be performed automatically)
        /// </returns>
        public bool DoNext(DeterministicRandom random)
        {
            if (queue.Count == 0)
            {
                throw new InvalidOperationException("This sequence does not contain a next action");
            }

            IAction next = queue.Dequeue();

            //If Action is in a suitable state, perform action.
            //Otherwise, engage rollback.
            if (next.CanPerform())
            {
                Trace.WriteLine("[Sequence] Performing Action:" + next.GetType());
                next.Perform(random);
            }
            else if (rollbackAction != null && rollbackAction.CanPerform())
            {
                Trace.WriteLine("[Sequence] Rolling Back Sequence");
                rollbackAction.Perform(random);
                queue.Clear();
            }

            return(queue.Count > 0);
        }