// Copyright (c) Pixel Crushers. All rights reserved. using UnityEngine; using System; using UnityEngine.Events; namespace PixelCrushers.QuestMachine { /// /// Provides methods to control parts of a quest. /// [AddComponentMenu("")] // Use wrapper instead. public class QuestControl : MonoBehaviour { #region Serialized Private Fields [Header("Quest")] [Tooltip("ID of quest to use in methods such as SetQuestState.")] [SerializeField] private StringField m_questID; [Tooltip("ID of quest node to use in methods such as SetQuestNodeState.")] [SerializeField] private StringField m_questNodeID; [Header("Quest Counter")] [Tooltip("Name of quest counter to use in methods such as IncrementQuestCounter.")] [SerializeField] private StringField m_counterName; [Serializable] public class ConditionalEvent { [HelpBox("This component's TryConditionalEvent method checks a quest and required state. If Quest Node ID is non-blank, it also checks a specific node and state. If conditions are met, executes On Condition Met () event.", HelpBoxMessageType.None)] public StringField questID; public QuestState requiredQuestState; public StringField questNodeID; public QuestNodeState requiredQuestNodeState; public UnityEvent onConditionMet = new UnityEvent(); } [SerializeField] private ConditionalEvent m_conditionalEvent; #endregion #region Public Properties public StringField questID { get { return m_questID; } set { m_questID = value; } } public StringField questNodeID { get { return m_questNodeID; } set { m_questNodeID = value; } } public StringField counterName { get { return m_counterName; } set { m_counterName = value; } } public ConditionalEvent conditionalEvent { get { return m_conditionalEvent; } set { m_conditionalEvent = value; } } #endregion /// /// Sets the quest to a state. The quest is specified in the /// questID property. /// /// New state. public void SetQuestState(QuestState state) { var quest = QuestMachine.GetQuestInstance(questID); if (quest == null) { if (QuestMachine.debug) Debug.LogWarning("Quest Machine: Can't find quest with ID '" + questID + "' to set its state.", this); return; } quest.SetState(state); } /// /// Sets the quest node to a state. The quest and quest node are specified /// in the questID and questNodeID properties. /// /// New state. public void SetQuestNodeState(QuestNodeState state) { var quest = QuestMachine.GetQuestInstance(questID); if (quest == null) { if (QuestMachine.debug) Debug.LogWarning("Quest Machine: Can't find quest with ID '" + questID + "' to set the state of node with ID '" + questNodeID + "'.", this); return; } var questNode = quest.GetNode(questNodeID); if (questNode == null) { if (QuestMachine.debug) Debug.LogWarning("Quest Machine: Can't find node with ID '" + questNodeID + "' in quest '" + questID + "' to set its state.", this); return; } questNode.SetState(state); } /// /// Sets a quest counter value. The quest and counter are specified in the /// questID and counterName properties. /// /// New value. public void SetQuestCounter(int value) { AdjustCounter(value, true); } /// /// Increments a quest counter value. The quest and counter are specified in the /// questID and counterName properties. /// /// Value to increment/decrement by. public void IncrementQuestCounter(int value) { AdjustCounter(value, false); } private void AdjustCounter(int value, bool set) { var quest = QuestMachine.GetQuestInstance(questID); if (quest == null) { if (QuestMachine.debug) Debug.LogWarning("Quest Machine: Can't find quest with ID '" + questID + "' to adjust counter " + counterName + ".", this); return; } var counter = quest.GetCounter(counterName); if (counter == null) { if (QuestMachine.debug) Debug.LogWarning("Quest Machine: Can't find counter " + counterName + " in quest '" + questID + "' to adjust its value.", this); return; } if (set) { counter.currentValue = value; } else { counter.currentValue += value; ; } } /// /// Sends a message to the Message System. If the message contains a /// colon (:), the part after the colon is sent as the parameter. If /// it contains a second colon, the part after the second colon is sent /// as a value. /// /// public void SendToMessageSystem(string message) { QuestMachineMessages.SendCompositeMessage(this, message); } /// /// Shows an alert. /// /// Text to show. public void ShowAlert(string text) { if (QuestMachine.defaultQuestAlertUI == null || string.IsNullOrEmpty(text)) return; QuestMachine.defaultQuestAlertUI.ShowAlert(text); } /// /// Checks the conditions of the assigned conditional event /// public void TryConditionalEvent() { if (IsConditionMet()) { conditionalEvent.onConditionMet.Invoke(); } } /// /// Checks if the condition event's conditions are met. /// /// public bool IsConditionMet() { if (StringField.IsNullOrEmpty(conditionalEvent.questID)) return true; if (QuestMachine.GetQuestState(conditionalEvent.questID) != conditionalEvent.requiredQuestState) return false; if (StringField.IsNullOrEmpty(conditionalEvent.questNodeID)) return true; return QuestMachine.GetQuestNodeState(conditionalEvent.questID, conditionalEvent.questNodeID) == conditionalEvent.requiredQuestNodeState; } } }