Example #1
0
        /// <summary>
        /// Sets the quest node to a quest state and performs all related activities
        /// such as enabling connections and executing actions. This may cause other
        /// nodes to advance their states, too.
        /// </summary>
        /// <param name="newState">New state.</param>
        public void SetState(QuestNodeState newState, bool informListeners = true)
        {
            if (QuestMachine.debug)
            {
                Debug.Log("Quest Machine: " + ((quest != null) ? quest.GetEditorName() : "Quest") + "." + GetEditorName() + ".SetState(" + newState + ")", quest);
            }

            m_state = newState;

            SetConditionChecking(newState == QuestNodeState.Active);

            if (!informListeners)
            {
                return;
            }

            // Execute state actions:
            var stateInfo = GetStateInfo(m_state);

            if (stateInfo != null && stateInfo.actionList != null)
            {
                for (int i = 0; i < stateInfo.actionList.Count; i++)
                {
                    if (stateInfo.actionList[i] == null)
                    {
                        continue;
                    }
                    stateInfo.actionList[i].Execute();
                }
            }

            // Notify that state changed:
            QuestMachineMessages.QuestNodeStateChanged(this, quest.id, id, m_state);
            try
            {
                stateChanged(this);
            }
            catch (Exception e) // Don't let exceptions in user-added events break our code.
            {
                if (Debug.isDebugBuild)
                {
                    Debug.LogException(e);
                }
            }

            // Handle special node types:
            switch (m_state)
            {
            case QuestNodeState.Active:
                if (nodeType != QuestNodeType.Condition)
                {
                    // Automatically switch non-Condition nodes to True state:
                    SetState(QuestNodeState.True);
                }
                break;

            case QuestNodeState.True:
                // If it's an endpoint, set the overall quest state:
                switch (nodeType)
                {
                case QuestNodeType.Success:
                    if (quest != null)
                    {
                        quest.SetState(QuestState.Successful);
                    }
                    break;

                case QuestNodeType.Failure:
                    if (quest != null)
                    {
                        quest.SetState(QuestState.Failed);
                    }
                    break;
                }
                break;
            }
        }