From c006e4a8ee9fa8c41a7f6b97d4baf2e3b8858359 Mon Sep 17 00:00:00 2001 From: kabix09 Date: Tue, 20 Dec 2022 03:30:37 +0100 Subject: [PATCH] Shop - open after dialogue --- Assets/Resources/Dialogue/Shop.meta | 8 ++ Assets/Resources/Dialogue/Shop/ANG.meta | 8 ++ .../Dialogue/Shop/ANG/ShopDialAng.asset | 25 +++++ .../Dialogue/Shop/ANG/ShopDialAng.asset.meta | 8 ++ Assets/Resources/Dialogue/Shop/PL.meta | 8 ++ .../Dialogue/Shop/PL/ShopDialPl.asset | 25 +++++ .../Dialogue/Shop/PL/ShopDialPl.asset.meta | 8 ++ .../Dialogue/Controller/DialogueController.cs | 1 - .../Application/Dialogue/Dialogue.cs | 27 ++++- .../Dialogue/Model/DialogueStepModel.cs | 31 +++-- .../Application/Dialogue/MultiDialogue.cs | 10 ++ .../Application/NPC/NpcDialogueManager.cs | 1 + .../Application/NPC/NpcShopManager.cs | 106 ++++++++++++++---- .../Panel/Shop/SceneShopDataManager.cs | 14 +-- .../Application/UI/Shop/ShopUIManager.cs | 2 + 15 files changed, 244 insertions(+), 38 deletions(-) create mode 100644 Assets/Resources/Dialogue/Shop.meta create mode 100644 Assets/Resources/Dialogue/Shop/ANG.meta create mode 100644 Assets/Resources/Dialogue/Shop/ANG/ShopDialAng.asset create mode 100644 Assets/Resources/Dialogue/Shop/ANG/ShopDialAng.asset.meta create mode 100644 Assets/Resources/Dialogue/Shop/PL.meta create mode 100644 Assets/Resources/Dialogue/Shop/PL/ShopDialPl.asset create mode 100644 Assets/Resources/Dialogue/Shop/PL/ShopDialPl.asset.meta diff --git a/Assets/Resources/Dialogue/Shop.meta b/Assets/Resources/Dialogue/Shop.meta new file mode 100644 index 00000000..0c605dac --- /dev/null +++ b/Assets/Resources/Dialogue/Shop.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3e26afc3f871ece49983af659133aaf6 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/Dialogue/Shop/ANG.meta b/Assets/Resources/Dialogue/Shop/ANG.meta new file mode 100644 index 00000000..d5d99420 --- /dev/null +++ b/Assets/Resources/Dialogue/Shop/ANG.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2d1f01af9ffb9b448a1854c5ef4bbbd3 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/Dialogue/Shop/ANG/ShopDialAng.asset b/Assets/Resources/Dialogue/Shop/ANG/ShopDialAng.asset new file mode 100644 index 00000000..f949dd24 --- /dev/null +++ b/Assets/Resources/Dialogue/Shop/ANG/ShopDialAng.asset @@ -0,0 +1,25 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 06201cadd951bcc4db5fccaa01783060, type: 3} + m_Name: ShopDialAng + m_EditorClassIdentifier: + SpeakerName: + CurrentStep: 0 + DialogueSteps: + - Header: + WasDisplayed: 0 + ListOfSentences: + - Sentence: Hello, are you interested in buying something? + Buttons: [] + EndOfDialogueStepAction: + m_PersistentCalls: + m_Calls: [] diff --git a/Assets/Resources/Dialogue/Shop/ANG/ShopDialAng.asset.meta b/Assets/Resources/Dialogue/Shop/ANG/ShopDialAng.asset.meta new file mode 100644 index 00000000..91e332f7 --- /dev/null +++ b/Assets/Resources/Dialogue/Shop/ANG/ShopDialAng.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0b370999d245bbf4ab85d24fd050f82a +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/Dialogue/Shop/PL.meta b/Assets/Resources/Dialogue/Shop/PL.meta new file mode 100644 index 00000000..8a565e30 --- /dev/null +++ b/Assets/Resources/Dialogue/Shop/PL.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: f7d7a10db705ca246966a3d308760393 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/Dialogue/Shop/PL/ShopDialPl.asset b/Assets/Resources/Dialogue/Shop/PL/ShopDialPl.asset new file mode 100644 index 00000000..0b27cdf5 --- /dev/null +++ b/Assets/Resources/Dialogue/Shop/PL/ShopDialPl.asset @@ -0,0 +1,25 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 06201cadd951bcc4db5fccaa01783060, type: 3} + m_Name: ShopDialPl + m_EditorClassIdentifier: + SpeakerName: + CurrentStep: 0 + DialogueSteps: + - Header: + WasDisplayed: 0 + ListOfSentences: + - Sentence: "Witaj, czy chcia\u0142 by\u015B co\u015B kupi\u0107?" + Buttons: [] + EndOfDialogueStepAction: + m_PersistentCalls: + m_Calls: [] diff --git a/Assets/Resources/Dialogue/Shop/PL/ShopDialPl.asset.meta b/Assets/Resources/Dialogue/Shop/PL/ShopDialPl.asset.meta new file mode 100644 index 00000000..691b836f --- /dev/null +++ b/Assets/Resources/Dialogue/Shop/PL/ShopDialPl.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9e071e06365862a4b8b5762b14ff8555 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/REFACTORING/Application/Dialogue/Controller/DialogueController.cs b/Assets/Scripts/REFACTORING/Application/Dialogue/Controller/DialogueController.cs index 19d84f0e..0313728e 100644 --- a/Assets/Scripts/REFACTORING/Application/Dialogue/Controller/DialogueController.cs +++ b/Assets/Scripts/REFACTORING/Application/Dialogue/Controller/DialogueController.cs @@ -64,7 +64,6 @@ public class DialogueController /// public bool ShowNextPanel(DialogueController parentDialController) { - Debug.Log(listOfDialogue.Count); if (listOfDialogue.Count == 0) { CloseCurrentPanel(parentDialController); diff --git a/Assets/Scripts/REFACTORING/Application/Dialogue/Dialogue.cs b/Assets/Scripts/REFACTORING/Application/Dialogue/Dialogue.cs index 36af7296..6407785f 100644 --- a/Assets/Scripts/REFACTORING/Application/Dialogue/Dialogue.cs +++ b/Assets/Scripts/REFACTORING/Application/Dialogue/Dialogue.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using UnityEditor; using UnityEngine; - +using UnityEngine.Events; [Serializable] [CreateAssetMenu(fileName = "New Dialogue", menuName = "Dialogue/New Dialogue")] @@ -24,6 +24,20 @@ public class Dialogue : ScriptableObject, IDialogue DialogueSteps.ForEach(step => step.Header = speakerName); } + #region finish action api + public void SetActionAfterDialogueStep(int dialogueStepNo, UnityEvent _endOfDialogueStepAction) + { + if(DialogueSteps.Count >= dialogueStepNo) + DialogueSteps[dialogueStepNo].SetActionAfterDialogueStep(_endOfDialogueStepAction); + } + public void SetActionAfterDialogueStep(int dialogueStepNo, Action _finishDialogueAction) + { + if (DialogueSteps.Count >= dialogueStepNo) + DialogueSteps[dialogueStepNo].SetActionAfterDialogueStep(_finishDialogueAction); + } + #endregion + + #region dialogue displaying api public void StartDialogue() { // 1. Build @@ -101,6 +115,7 @@ public class Dialogue : ScriptableObject, IDialogue } } } + #endregion /// /// Function to build each step of dialogue @@ -114,4 +129,14 @@ public class Dialogue : ScriptableObject, IDialogue dialogueStep.Build(); } } + + /// + /// Function to reset rememebered dialogue status + /// + /// (Scriptable objects clones are overwritten during clone modification) + /// + public void ResetDialogue() + { + DialogueSteps.ForEach(step => step.WasDisplayed = false); + } } diff --git a/Assets/Scripts/REFACTORING/Application/Dialogue/Model/DialogueStepModel.cs b/Assets/Scripts/REFACTORING/Application/Dialogue/Model/DialogueStepModel.cs index 6d9d75dc..aa96c38d 100644 --- a/Assets/Scripts/REFACTORING/Application/Dialogue/Model/DialogueStepModel.cs +++ b/Assets/Scripts/REFACTORING/Application/Dialogue/Model/DialogueStepModel.cs @@ -26,10 +26,17 @@ public class DialogueStepModel public DialogueController DialogueController { get; protected set; } - public DialogueStepModel() { } + public DialogueStepModel() + { + // Add marked step as displayed action as first! + EndOfDialogueStepAction.AddListener(() => { MarkAsDisplayed(); }); + } public DialogueStepModel(DialogueController _dialogueController) { + // Add marked step as displayed action as first! + EndOfDialogueStepAction.AddListener(() => { MarkAsDisplayed(); }); + DialogueController = _dialogueController; } @@ -51,12 +58,22 @@ public class DialogueStepModel DialogueController.AddSentence(DialogueModel); } - // 3. Bind finishing action - - // 3.1 Add marked step as displayed action - EndOfDialogueStepAction.AddListener(() => WasDisplayed = true); - - // 3.2 Bind actions + // 3 Bind ending actions DialogueController.SetActionAfterDialogueEnds(EndOfDialogueStepAction); } + + public void SetActionAfterDialogueStep(UnityEvent _endOfDialogueStepAction) + { + EndOfDialogueStepAction = _endOfDialogueStepAction; + } + + public void SetActionAfterDialogueStep(Action _finishDialogueAction) + { + EndOfDialogueStepAction.AddListener(new UnityAction(_finishDialogueAction)); + } + + public void MarkAsDisplayed() + { + WasDisplayed = true; + } } \ No newline at end of file diff --git a/Assets/Scripts/REFACTORING/Application/Dialogue/MultiDialogue.cs b/Assets/Scripts/REFACTORING/Application/Dialogue/MultiDialogue.cs index e212d0c0..a76a3f2e 100644 --- a/Assets/Scripts/REFACTORING/Application/Dialogue/MultiDialogue.cs +++ b/Assets/Scripts/REFACTORING/Application/Dialogue/MultiDialogue.cs @@ -147,6 +147,16 @@ public class MultiDialogue : ScriptableObject, IDialogue } } + /// + /// Function to reset rememebered dialogue status + /// + /// (Scriptable objects clones are overwritten during clone modification) + /// + public void ResetDialogue() + { + Dialogues.ForEach(dial => dial.Value.DialogueSteps.ForEach(step => step.WasDisplayed = false)); + } + public (int, int) DialogueStepStatus() { var currentDialogueStepIndex = Dialogues diff --git a/Assets/Scripts/REFACTORING/Application/NPC/NpcDialogueManager.cs b/Assets/Scripts/REFACTORING/Application/NPC/NpcDialogueManager.cs index 38c713ae..62cf372f 100644 --- a/Assets/Scripts/REFACTORING/Application/NPC/NpcDialogueManager.cs +++ b/Assets/Scripts/REFACTORING/Application/NPC/NpcDialogueManager.cs @@ -5,6 +5,7 @@ using System.Linq; using UnityEngine; using UnityEngine.Events; +[RequireComponent(typeof(NPC))] [Serializable] public class NpcDialogueManager : MonoBehaviour { diff --git a/Assets/Scripts/REFACTORING/Application/NPC/NpcShopManager.cs b/Assets/Scripts/REFACTORING/Application/NPC/NpcShopManager.cs index 31143787..94a4cb89 100644 --- a/Assets/Scripts/REFACTORING/Application/NPC/NpcShopManager.cs +++ b/Assets/Scripts/REFACTORING/Application/NPC/NpcShopManager.cs @@ -5,29 +5,29 @@ using System.Linq; using UnityEngine; using UnityEngine.SceneManagement; +[RequireComponent(typeof(NPC))] class NpcShopManager : MonoBehaviour { [SerializeField] - public Dialogue DialogueTemplate; + public List> DialogueTemplate; /* We user object CLONED TO PREVENT overwritting changes in main object inassets */ - private Dialogue Dialogue; + public Dialogue Dialogue; [SerializeField] public Shop shop; - public void Start() + bool CanShopBeOpened = false; + + public virtual void Start() { - // TODO - remove - we want ot register ir manually because condition below is not meeted in test env - this.RegisterShop(); + // 1. Set npc state to trading + gameObject.GetComponent().State = NPCStateEnum.Trading; + // 2. Init dialogue model + CreateInstanceBasedOnLanguage(); - // 1. Create dialogue object from template - - // 2. Set speaker name - Dialogue = Instantiate(DialogueTemplate); - Dialogue.SetSpeakerName(gameObject.GetComponent().name); - + //this.RegisterShop(); // if its new game or we dont have any save from this map - use deffault shop settings / content // else overwrite value by saved one if (OnMapAppearanceMethod.GameStatus == GameStatus.NewGame || @@ -42,26 +42,67 @@ class NpcShopManager : MonoBehaviour }).First(); } + public void Update() + { + /* + * Conditions: + * - player must be in range of shopping + * - user must press specific keyboard button + * - dialogue panel must be closed + * - shop panel must be closed + */ + if (CanShopBeOpened && Input.GetKeyDown(ShopUIManager.Instance.keyToOpen) && Dialogue.DialogueSteps.First().DialogueController?.CurrentPanel == null && !ShopUIManager.Instance.GetPanelStatus()) + { + // Open dialogue panel (Shop will be opened later) + Dialogue.StartDialogue(); + } + + } + public void OnTriggerEnter2D(Collider2D collision) { - if(collision.gameObject.tag == "Player" && gameObject.GetComponent().State == NPCStateEnum.Trading && !ShopUIManager.Instance.GetPanelStatus()) + // don't listen when component is disabled + if (!gameObject.GetComponent().enabled) + return; + + /* + * Conditions: + * - agent in collision must be player + * - npc bust me in Trading mode + * - shop must be currently closed + */ + if (collision.gameObject.tag == "Player" && gameObject.GetComponent().State == NPCStateEnum.Trading && !ShopUIManager.Instance.GetPanelStatus()) { - // 1. Open dialogue panel - - // 2. Open shop - - OpenShop(); + CanShopBeOpened = true; } } public void OnTriggerExit2D(Collider2D collision) { + // don't listen when component is disabled + if (!gameObject.GetComponent().enabled) + return; + if (collision.gameObject.tag == "Player") { + CanShopBeOpened = false; + + if (Dialogue.DialogueSteps.First().DialogueController?.CurrentPanel != null) + Dialogue.BreakDialogueStep(); + + CloseShop(); } } + public void CreateInstanceBasedOnLanguage() + { + Dialogue = Instantiate(DetectInstanceBasedOnLanguage()); + Dialogue.SetSpeakerName(gameObject.GetComponent().name); + Dialogue.SetActionAfterDialogueStep(0, Dialogue.ResetDialogue); + Dialogue.SetActionAfterDialogueStep(0, OpenShop); + } + public void RegisterShop() { // 1. Set owner name @@ -80,7 +121,10 @@ class NpcShopManager : MonoBehaviour public void OpenShop() { - Debug.Log("OpenShop"); + if (EquipmentUIManager.Instance.GetPanelStatus()) EquipmentUIManager.Instance.ClosePanel(); + + + // Set info about current shop in shop UI manager (and wait for event - pressing E) // Open shopa action - invoked as dialogue end action ShopUIManager.Instance.CurrentShopOwnerName = gameObject.GetComponent().name; @@ -90,9 +134,31 @@ class NpcShopManager : MonoBehaviour public void CloseShop() { - ShopUIManager.Instance.ClosePanel(); - ShopUIManager.Instance.CurrentShopOwnerName = ""; + + ShopUIManager.Instance.ClosePanel(); + } + + public Dialogue DetectInstanceBasedOnLanguage() + { + /*if (!PlayerPrefs.HasKey("language")) + throw new Exception("Language not setted !!!");*/ + + switch(PlayerPrefs.GetString("language")) + { + case "English": + { + return DialogueTemplate.Where(template => template.Key == SystemLanguageEnum.English).First().Value; + } + case "Polish": + { + return DialogueTemplate.Where(template => template.Key == SystemLanguageEnum.Polish).First().Value; + } + default: + { + return DialogueTemplate.Where(template => template.Key == SystemLanguageEnum.Polish).First().Value; + } + } } } diff --git a/Assets/Scripts/REFACTORING/Application/Panel/Shop/SceneShopDataManager.cs b/Assets/Scripts/REFACTORING/Application/Panel/Shop/SceneShopDataManager.cs index b4217b57..5e6c07cb 100644 --- a/Assets/Scripts/REFACTORING/Application/Panel/Shop/SceneShopDataManager.cs +++ b/Assets/Scripts/REFACTORING/Application/Panel/Shop/SceneShopDataManager.cs @@ -20,8 +20,11 @@ public class SceneShopDataManager : SceneBaseDataManager { if (Instance == null) { + Debug.Log("Create: " + gameObject); Instance = this; + + Start(); } else { @@ -31,13 +34,13 @@ public class SceneShopDataManager : SceneBaseDataManager public override void Start() { - Debug.Log("Start SceneChestData manager"); + Debug.Log("Start SceneShopData manager"); //TaskUIManager.FindOrCreateInstance(); UiManager = ShopUIManager.Instance; if (UiManager == null) - throw new NullReferenceException("ChestUIManager not found!!!"); + throw new NullReferenceException("ShopUIManager not found!!!"); DynamicDataList = (new ShopDataListManager()).SetUiManager(ref UiManager); @@ -58,7 +61,6 @@ public class SceneShopDataManager : SceneBaseDataManager UseDefaultSettings(); }else { - Debug.Log("UseDynamicSettings"); UseDynamicSettings(); } } @@ -78,18 +80,12 @@ public class SceneShopDataManager : SceneBaseDataManager protected override void UseDefaultSettings() { - Debug.Log("UseDefaultSettings"); - // we dont need to convert anything // Instead of build elements on scene we collect shops distracted on scene from npc's } protected override void UseDynamicSettings() { - Debug.Log("UseDynamicSettings"); - - // BuildList(); - // when chest detect player in near arrea and player press "c" // open panel -> chest controller handle Open panel in Scene Chest Manager passing info about what Chest Palyer want to open // Manager Build panel and pass info about chest content diff --git a/Assets/Scripts/REFACTORING/Application/UI/Shop/ShopUIManager.cs b/Assets/Scripts/REFACTORING/Application/UI/Shop/ShopUIManager.cs index 8bf5317b..5739b989 100644 --- a/Assets/Scripts/REFACTORING/Application/UI/Shop/ShopUIManager.cs +++ b/Assets/Scripts/REFACTORING/Application/UI/Shop/ShopUIManager.cs @@ -53,6 +53,8 @@ public class ShopUIManager : UIBaseManager ShopContentUIManager.Instance.DynamicPanel = null; + CurrentShopOwnerName = ""; + // Close additionals panels if (InventoryUIManager.Instance.GetPanelStatus()) InventoryUIManager.Instance.ClosePanel();