diff --git a/Assets/Scripts/NPCs' Scripts/NPCFollowing.cs b/Assets/Scripts/NPCs' Scripts/NPCFollowing.cs index 837c5358..1f593d7e 100644 --- a/Assets/Scripts/NPCs' Scripts/NPCFollowing.cs +++ b/Assets/Scripts/NPCs' Scripts/NPCFollowing.cs @@ -85,7 +85,7 @@ public class NPCFollowing : MonoBehaviour gameObject.GetComponent().State = NPCStateEnum.Talking; - gameObject.GetComponent().CheckIfCanBeOpen(); + gameObject.GetComponent().OpenDialoguePanel(); } } diff --git a/Assets/Scripts/REFACTORING/Application/Dialogue/Manager.meta b/Assets/Scripts/REFACTORING/Application/Dialogue/Manager.meta new file mode 100644 index 00000000..76508490 --- /dev/null +++ b/Assets/Scripts/REFACTORING/Application/Dialogue/Manager.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 57b3e5afcf852154b9429f4860e19878 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/REFACTORING/Application/Dialogue/Manager/DialogueManager.cs b/Assets/Scripts/REFACTORING/Application/Dialogue/Manager/DialogueManager.cs new file mode 100644 index 00000000..8a808d5b --- /dev/null +++ b/Assets/Scripts/REFACTORING/Application/Dialogue/Manager/DialogueManager.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; +using UnityEngine.Events; + +public class DialogueManager : MonoBehaviour +{ + [SerializeField] + public string SpeakerName; + + [SerializeField] + public LanguageDetector languageDetector; + + /* We user object CLONED TO PREVENT overwritting changes in main object inassets */ + [SerializeField] + public MultiDialogue Dialogue; + + // List, Value : UnityEvent> + public List, UnityEvent>> EndactionEventList; + + [SerializeField] public KeyCode keyToOpen = KeyCode.E; + + public bool CanBeOpened = true; + + /// + /// This flag tell that manager will open dialogue automatically after + /// - beeing in collision range + /// - when dialogue is closed + /// - when Player press KeyToOpen button + /// + /// Change var status in other sctipt if you want to use other triggers to open dialogue + /// + public bool OpenInDefaultWay = true; + + public virtual void Start() + { + Dialogue = Instantiate(languageDetector.DetectInstanceBasedOnLanguage()); + Dialogue.SetSpeakerName(SpeakerName); + + Dialogue.Dialogues.ForEach(dial => dial.Value.SetActionAfterDialogueStep(dial.Key, dial.Value.ResetDialogue)); // reset dial + } + + + public void Update() + { + if (OpenInDefaultWay && Input.GetKeyDown(keyToOpen) && OpenPanelCondition()) + OpenDialoguePanel(); + } + + + public virtual void OnTriggerEnter2D(Collider2D collision) + { + // don't listen when component is disabled + if (ComponentEnabledCondition()) + return; + + if (collision.gameObject.tag == "Player") + { + CanBeOpened = true; + + SpeakerName = collision.gameObject.name; + } + } + public virtual void OnTriggerExit2D(Collider2D collision) + { + // don't listen when component is disabled + if (ComponentEnabledCondition()) + return; + + if (collision.gameObject.tag == "Player") + { + CanBeOpened = false; + + if (GetCurrentDialoguePanelStatus()) + { + Debug.Log("Close dialog - defaulty way"); + Dialogue.BreakDialogueStep(); + } + } + } + + public void OpenDialoguePanel() + { + if(OpenPanelCondition()) + gameObject.GetComponent().Dialogue.StartDialogue(); + } + public virtual bool OpenPanelCondition() + { + /* + * Conditions: + * - player is in collision range + * - panel assigned to next sentence in dialogue must be closed + */ + return IsInRangeCondition() && !GetCurrentDialoguePanelStatus(); + } + + public bool IsInRangeCondition() + { + return CanBeOpened; + } + protected virtual bool ComponentEnabledCondition() + { + return !gameObject.GetComponent().enabled; + } + + /// + /// Opened = 1 + /// Closed = 0 + /// + /// + public bool GetCurrentDialoguePanelStatus() + { + var multiDialStatius = Dialogue.DialogueStepStatus(); + + return Dialogue + .Dialogues + .Where(dialogue => dialogue.Key == multiDialStatius.Item1) + .First() + .Value + .DialogueSteps[multiDialStatius.Item2].DialogueController.CurrentPanel != null; + } +} + diff --git a/Assets/Scripts/REFACTORING/Application/Dialogue/Manager/DialogueManager.cs.meta b/Assets/Scripts/REFACTORING/Application/Dialogue/Manager/DialogueManager.cs.meta new file mode 100644 index 00000000..e0534f75 --- /dev/null +++ b/Assets/Scripts/REFACTORING/Application/Dialogue/Manager/DialogueManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ce2c370efba4d2e43b409e7ec8429171 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/REFACTORING/Application/NPC/NpcDialogueManager.cs b/Assets/Scripts/REFACTORING/Application/NPC/NpcDialogueManager.cs index 6ae71dee..88aa9623 100644 --- a/Assets/Scripts/REFACTORING/Application/NPC/NpcDialogueManager.cs +++ b/Assets/Scripts/REFACTORING/Application/NPC/NpcDialogueManager.cs @@ -6,101 +6,28 @@ using UnityEngine; using UnityEngine.Events; [RequireComponent(typeof(NPC))] -public class NpcDialogueManager : MonoBehaviour +public class NpcDialogueManager : DialogueManager { - [SerializeField] - public string SpeakerName; - - [SerializeField] - public LanguageDetector languageDetector; - - /* We user object CLONED TO PREVENT overwritting changes in main object inassets */ - [SerializeField] - public MultiDialogue Dialogue; - - // List, Value : UnityEvent> - public List, UnityEvent>> EndactionEventList; - - [SerializeField] public KeyCode keyToOpen = KeyCode.E; - - public bool CanBeOpened = false; - - /// - /// This flag tell that manager will open dialogue automatically after - /// - beeing in collision range - /// - when dialogue is closed - /// - when Player press KeyToOpen button - /// - /// Change var status in other sctipt if you want to use other triggers to open dialogue - /// - public bool OpenInDefaultWay = true; - - public void Start() + public override void Start() { - Dialogue = Instantiate(languageDetector.DetectInstanceBasedOnLanguage()); - Dialogue.SetSpeakerName(SpeakerName); + base.Start(); - Dialogue.Dialogues.ForEach(dial => dial.Value.SetActionAfterDialogueStep(dial.Key, dial.Value.ResetDialogue)); // reset dial + CanBeOpened = false; } - - public void Update() - { - if(OpenInDefaultWay && CanBeOpened && !GetCurrentDialoguePanelStatus() && Input.GetKeyDown(keyToOpen)) - gameObject.GetComponent().Dialogue.StartDialogue(); - } - - public void OnTriggerEnter2D(Collider2D collision) - { - // don't listen when component is disabled - if (!gameObject.GetComponent().enabled) - return; - - if (collision.gameObject.tag == "Player") - CheckIfCanBeOpen(); - } - - - public void OnTriggerExit2D(Collider2D collision) - { - // don't listen when component is disabled - if (!gameObject.GetComponent().enabled) - return; - - if (collision.gameObject.tag == "Player") - { - CanBeOpened = false; - } - } - - /// - /// Opened = 1 - /// Closed = 0 - /// - /// - public bool GetCurrentDialoguePanelStatus() - { - var multiDialStatius = Dialogue.DialogueStepStatus(); - - return Dialogue - .Dialogues - .Where(dialogue => dialogue.Key == multiDialStatius.Item1) - .First() - .Value - .DialogueSteps[multiDialStatius.Item2].DialogueController.CurrentPanel != null; - } - - public void CheckIfCanBeOpen() + public override bool OpenPanelCondition() { /* * Conditions: - * - agent in collision must be player * - npc bust me in Talking mode + * - player is in collision range * - panel assigned to next sentence in dialogue must be closed */ - if(gameObject.GetComponent().State == NPCStateEnum.Talking && !GetCurrentDialoguePanelStatus()) - { - CanBeOpened = true; - } + return gameObject.GetComponent().State == NPCStateEnum.Talking && base.OpenPanelCondition(); + } + + protected virtual bool ColliderTypeCondition() + { + return !gameObject.GetComponent().enabled; } ///