Esempio n. 1
0
        protected virtual IEnumerator ExecuteBlock(Action onComplete = null)
        {
            Flowchart flowchart = GetFlowchart();

            executionState = ExecutionState.Executing;

                        #if UNITY_EDITOR
            // Select the executing block & the first command
            flowchart.selectedBlock = this;
            if (commandList.Count > 0)
            {
                flowchart.ClearSelectedCommands();
                flowchart.AddSelectedCommand(commandList[0]);
            }
                        #endif

            int i = 0;
            while (true)
            {
                // Executing commands specify the next command to skip to by setting jumpToCommandIndex using Command.Continue()
                if (jumpToCommandIndex > -1)
                {
                    i = jumpToCommandIndex;
                    jumpToCommandIndex = -1;
                }

                // Skip disabled commands, comments and labels
                while (i < commandList.Count &&
                       (!commandList[i].enabled ||
                        commandList[i].GetType() == typeof(Comment) ||
                        commandList[i].GetType() == typeof(Label)))
                {
                    i = commandList[i].commandIndex + 1;
                }

                if (i >= commandList.Count)
                {
                    break;
                }

                // The previous active command is needed for if / else / else if commands
                if (activeCommand == null)
                {
                    previousActiveCommandIndex = -1;
                }
                else
                {
                    previousActiveCommandIndex = activeCommand.commandIndex;
                }

                Command command = commandList[i];
                activeCommand = command;

                if (flowchart.gameObject.activeInHierarchy)
                {
                    // Auto select a command in some situations
                    if ((flowchart.selectedCommands.Count == 0 && i == 0) ||
                        (flowchart.selectedCommands.Count == 1 && flowchart.selectedCommands[0].commandIndex == previousActiveCommandIndex))
                    {
                        flowchart.ClearSelectedCommands();
                        flowchart.AddSelectedCommand(commandList[i]);
                    }
                }

                command.isExecuting = true;
                // This icon timer is managed by the FlowchartWindow class, but we also need to
                // set it here in case a command starts and finishes execution before the next window update.
                command.executingIconTimer = Time.realtimeSinceStartup + executingIconFadeTime;
                command.Execute();

                // Wait until the executing command sets another command to jump to via Command.Continue()
                while (jumpToCommandIndex == -1)
                {
                    yield return(null);
                }

                                #if UNITY_EDITOR
                if (flowchart.stepPause > 0f)
                {
                    yield return(new WaitForSeconds(flowchart.stepPause));
                }
                                #endif

                command.isExecuting = false;
            }

            executionState = ExecutionState.Idle;
            activeCommand  = null;

            if (onComplete != null)
            {
                onComplete();
            }
        }
Esempio n. 2
0
		IEnumerator ExecuteAfterDelay(Command command, float delay)
		{
			activeCommand = command;
			yield return new WaitForSeconds(delay);
			command.Execute();
		}
Esempio n. 3
0
        public virtual void ExecuteCommand(int commandIndex)
        {
            if (activeCommand == null)
            {
                previousActiveCommandIndex = -1;
            }
            else
            {
                previousActiveCommandIndex = activeCommand.commandIndex;
            }

            if (commandIndex >= commandList.Count)
            {
                Stop();
                return;
            }

            if (commandIndex == 0)
            {
                executionCount++;
            }

            FungusScript fungusScript = GetFungusScript();

            // Skip disabled commands, comments and labels
            while (commandIndex < commandList.Count &&
                   (!commandList[commandIndex].enabled ||
                    commandList[commandIndex].GetType() == typeof(Comment) ||
                    commandList[commandIndex].GetType() == typeof(Label)))
            {
                commandIndex = commandList[commandIndex].commandIndex + 1;
            }

            if (commandIndex >= commandList.Count)
            {
                Stop();
                return;
            }

            Command nextCommand = commandList[commandIndex];

            activeCommand      = null;
            executingIconTimer = 0.5f;

            if (nextCommand == null)
            {
                Stop();
            }
            else
            {
                if (fungusScript.gameObject.activeInHierarchy)
                {
                    // Auto select a command in some situations
                    if ((fungusScript.selectedCommands.Count == 0 && commandIndex == 0) ||
                        (fungusScript.selectedCommands.Count == 1 && fungusScript.selectedCommands[0].commandIndex == previousActiveCommandIndex))
                    {
                        fungusScript.ClearSelectedCommands();
                        fungusScript.AddSelectedCommand(nextCommand);
                    }

                    if (!runSlowInEditor)
                    {
                        activeCommand = nextCommand;
                        nextCommand.Execute();
                    }
                    else
                    {
                        StartCoroutine(ExecuteAfterDelay(nextCommand, fungusScript.runSlowDuration));
                    }
                }
            }
        }