Shop - open after dialogue

This commit is contained in:
kabix09 2022-12-20 03:30:37 +01:00
parent 43e1d817d7
commit c006e4a8ee
15 changed files with 244 additions and 38 deletions

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3e26afc3f871ece49983af659133aaf6
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2d1f01af9ffb9b448a1854c5ef4bbbd3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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: []

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 0b370999d245bbf4ab85d24fd050f82a
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f7d7a10db705ca246966a3d308760393
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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: []

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9e071e06365862a4b8b5762b14ff8555
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -64,7 +64,6 @@ public class DialogueController
/// <returns></returns> /// <returns></returns>
public bool ShowNextPanel(DialogueController parentDialController) public bool ShowNextPanel(DialogueController parentDialController)
{ {
Debug.Log(listOfDialogue.Count);
if (listOfDialogue.Count == 0) if (listOfDialogue.Count == 0)
{ {
CloseCurrentPanel(parentDialController); CloseCurrentPanel(parentDialController);

View File

@ -2,7 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
[Serializable] [Serializable]
[CreateAssetMenu(fileName = "New Dialogue", menuName = "Dialogue/New Dialogue")] [CreateAssetMenu(fileName = "New Dialogue", menuName = "Dialogue/New Dialogue")]
@ -24,6 +24,20 @@ public class Dialogue : ScriptableObject, IDialogue
DialogueSteps.ForEach(step => step.Header = speakerName); 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() public void StartDialogue()
{ {
// 1. Build // 1. Build
@ -101,6 +115,7 @@ public class Dialogue : ScriptableObject, IDialogue
} }
} }
} }
#endregion
/// <summary> /// <summary>
/// Function to build each step of dialogue /// Function to build each step of dialogue
@ -114,4 +129,14 @@ public class Dialogue : ScriptableObject, IDialogue
dialogueStep.Build(); dialogueStep.Build();
} }
} }
/// <summary>
/// Function to reset rememebered dialogue status
///
/// (Scriptable objects clones are overwritten during clone modification)
/// </summary>
public void ResetDialogue()
{
DialogueSteps.ForEach(step => step.WasDisplayed = false);
}
} }

View File

@ -26,10 +26,17 @@ public class DialogueStepModel
public DialogueController DialogueController { get; protected set; } 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) public DialogueStepModel(DialogueController _dialogueController)
{ {
// Add marked step as displayed action as first!
EndOfDialogueStepAction.AddListener(() => { MarkAsDisplayed(); });
DialogueController = _dialogueController; DialogueController = _dialogueController;
} }
@ -51,12 +58,22 @@ public class DialogueStepModel
DialogueController.AddSentence(DialogueModel); DialogueController.AddSentence(DialogueModel);
} }
// 3. Bind finishing action // 3 Bind ending actions
// 3.1 Add marked step as displayed action
EndOfDialogueStepAction.AddListener(() => WasDisplayed = true);
// 3.2 Bind actions
DialogueController.SetActionAfterDialogueEnds(EndOfDialogueStepAction); 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;
}
} }

View File

@ -147,6 +147,16 @@ public class MultiDialogue : ScriptableObject, IDialogue
} }
} }
/// <summary>
/// Function to reset rememebered dialogue status
///
/// (Scriptable objects clones are overwritten during clone modification)
/// </summary>
public void ResetDialogue()
{
Dialogues.ForEach(dial => dial.Value.DialogueSteps.ForEach(step => step.WasDisplayed = false));
}
public (int, int) DialogueStepStatus() public (int, int) DialogueStepStatus()
{ {
var currentDialogueStepIndex = Dialogues var currentDialogueStepIndex = Dialogues

View File

@ -5,6 +5,7 @@ using System.Linq;
using UnityEngine; using UnityEngine;
using UnityEngine.Events; using UnityEngine.Events;
[RequireComponent(typeof(NPC))]
[Serializable] [Serializable]
public class NpcDialogueManager : MonoBehaviour public class NpcDialogueManager : MonoBehaviour
{ {

View File

@ -5,29 +5,29 @@ using System.Linq;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
[RequireComponent(typeof(NPC))]
class NpcShopManager : MonoBehaviour class NpcShopManager : MonoBehaviour
{ {
[SerializeField] [SerializeField]
public Dialogue DialogueTemplate; public List<IndexValuePair<SystemLanguageEnum, Dialogue>> DialogueTemplate;
/* We user object CLONED TO PREVENT overwritting changes in main object inassets */ /* We user object CLONED TO PREVENT overwritting changes in main object inassets */
private Dialogue Dialogue; public Dialogue Dialogue;
[SerializeField] [SerializeField]
public Shop shop; 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 // 1. Set npc state to trading
this.RegisterShop(); gameObject.GetComponent<NPC>().State = NPCStateEnum.Trading;
// 2. Init dialogue model
CreateInstanceBasedOnLanguage();
// 1. Create dialogue object from template //this.RegisterShop();
// 2. Set speaker name
Dialogue = Instantiate(DialogueTemplate);
Dialogue.SetSpeakerName(gameObject.GetComponent<NPC>().name);
// if its new game or we dont have any save from this map - use deffault shop settings / content // if its new game or we dont have any save from this map - use deffault shop settings / content
// else overwrite value by saved one // else overwrite value by saved one
if (OnMapAppearanceMethod.GameStatus == GameStatus.NewGame || if (OnMapAppearanceMethod.GameStatus == GameStatus.NewGame ||
@ -42,26 +42,67 @@ class NpcShopManager : MonoBehaviour
}).First(); }).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) public void OnTriggerEnter2D(Collider2D collision)
{ {
if(collision.gameObject.tag == "Player" && gameObject.GetComponent<NPC>().State == NPCStateEnum.Trading && !ShopUIManager.Instance.GetPanelStatus()) // don't listen when component is disabled
if (!gameObject.GetComponent<NpcShopManager>().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<NPC>().State == NPCStateEnum.Trading && !ShopUIManager.Instance.GetPanelStatus())
{ {
// 1. Open dialogue panel CanShopBeOpened = true;
// 2. Open shop
OpenShop();
} }
} }
public void OnTriggerExit2D(Collider2D collision) public void OnTriggerExit2D(Collider2D collision)
{ {
// don't listen when component is disabled
if (!gameObject.GetComponent<NpcShopManager>().enabled)
return;
if (collision.gameObject.tag == "Player") if (collision.gameObject.tag == "Player")
{ {
CanShopBeOpened = false;
if (Dialogue.DialogueSteps.First().DialogueController?.CurrentPanel != null)
Dialogue.BreakDialogueStep();
CloseShop(); CloseShop();
} }
} }
public void CreateInstanceBasedOnLanguage()
{
Dialogue = Instantiate(DetectInstanceBasedOnLanguage());
Dialogue.SetSpeakerName(gameObject.GetComponent<NPC>().name);
Dialogue.SetActionAfterDialogueStep(0, Dialogue.ResetDialogue);
Dialogue.SetActionAfterDialogueStep(0, OpenShop);
}
public void RegisterShop() public void RegisterShop()
{ {
// 1. Set owner name // 1. Set owner name
@ -80,7 +121,10 @@ class NpcShopManager : MonoBehaviour
public void OpenShop() 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 // Open shopa action - invoked as dialogue end action
ShopUIManager.Instance.CurrentShopOwnerName = gameObject.GetComponent<NPC>().name; ShopUIManager.Instance.CurrentShopOwnerName = gameObject.GetComponent<NPC>().name;
@ -90,9 +134,31 @@ class NpcShopManager : MonoBehaviour
public void CloseShop() public void CloseShop()
{ {
ShopUIManager.Instance.ClosePanel();
ShopUIManager.Instance.CurrentShopOwnerName = ""; 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;
}
}
} }
} }

View File

@ -20,8 +20,11 @@ public class SceneShopDataManager : SceneBaseDataManager<Shop>
{ {
if (Instance == null) if (Instance == null)
{ {
Debug.Log("Create: " + gameObject);
Instance = this; Instance = this;
Start();
} }
else else
{ {
@ -31,13 +34,13 @@ public class SceneShopDataManager : SceneBaseDataManager<Shop>
public override void Start() public override void Start()
{ {
Debug.Log("Start SceneChestData manager"); Debug.Log("Start SceneShopData manager");
//TaskUIManager.FindOrCreateInstance(); //TaskUIManager.FindOrCreateInstance();
UiManager = ShopUIManager.Instance; UiManager = ShopUIManager.Instance;
if (UiManager == null) if (UiManager == null)
throw new NullReferenceException("ChestUIManager not found!!!"); throw new NullReferenceException("ShopUIManager not found!!!");
DynamicDataList = (new ShopDataListManager()).SetUiManager(ref UiManager); DynamicDataList = (new ShopDataListManager()).SetUiManager(ref UiManager);
@ -58,7 +61,6 @@ public class SceneShopDataManager : SceneBaseDataManager<Shop>
UseDefaultSettings(); UseDefaultSettings();
}else }else
{ {
Debug.Log("UseDynamicSettings");
UseDynamicSettings(); UseDynamicSettings();
} }
} }
@ -78,18 +80,12 @@ public class SceneShopDataManager : SceneBaseDataManager<Shop>
protected override void UseDefaultSettings() protected override void UseDefaultSettings()
{ {
Debug.Log("UseDefaultSettings");
// we dont need to convert anything // we dont need to convert anything
// Instead of build elements on scene we collect shops distracted on scene from npc's // Instead of build elements on scene we collect shops distracted on scene from npc's
} }
protected override void UseDynamicSettings() protected override void UseDynamicSettings()
{ {
Debug.Log("UseDynamicSettings");
// BuildList();
// when chest detect player in near arrea and player press "c" // 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 // 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 // Manager Build panel and pass info about chest content

View File

@ -53,6 +53,8 @@ public class ShopUIManager : UIBaseManager<Shop>
ShopContentUIManager.Instance.DynamicPanel = null; ShopContentUIManager.Instance.DynamicPanel = null;
CurrentShopOwnerName = "";
// Close additionals panels // Close additionals panels
if (InventoryUIManager.Instance.GetPanelStatus()) InventoryUIManager.Instance.ClosePanel(); if (InventoryUIManager.Instance.GetPanelStatus()) InventoryUIManager.Instance.ClosePanel();