Scriptum/Assets/Scripts/REFACTORING/Application/Dialogue/Manager/DialogueManager.cs

203 lines
5.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
2022-12-27 15:16:59 +01:00
[RequireComponent(typeof(NPC))]
public class DialogueManager : MonoBehaviour
{
[SerializeField]
public string SpeakerName;
[SerializeField]
public LanguageDetector<MultiDialogue> languageDetector;
/* We user object CLONED TO PREVENT overwritting changes in main object inassets */
[SerializeField]
public MultiDialogue Dialogue;
// List<Key<dialogue No, dialogue step No>, Value : UnityEvent>
public List<IndexValuePair<IndexValuePair<int, int>, UnityEvent>> EndactionEventList;
[SerializeField] public KeyCode keyToOpen = KeyCode.E;
public bool CanBeOpened = true;
/// <summary>
/// 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
/// </summary>
public bool OpenInDefaultWay = true;
public virtual void Start()
{
2023-01-02 00:05:47 +01:00
if(languageDetector.InstanceTemplates.Count() > 0)
{
// create and config dailogue
CreateInstanceBasedOnLanguage();
2023-01-01 17:19:16 +01:00
2023-01-02 00:05:47 +01:00
Dialogue.BuildCurrentDialogue();
}
}
2023-01-02 00:05:47 +01:00
public virtual void Update()
{
if (OpenInDefaultWay && Input.GetKeyDown(keyToOpen) && OpenPanelCondition())
2022-12-24 20:16:41 +01:00
{
OpenDialoguePanel();
2022-12-24 20:16:41 +01:00
}
}
public virtual void OnTriggerEnter2D(Collider2D collision)
{
// don't listen when component is disabled
if (ComponentEnabledCondition())
return;
if (collision.gameObject.tag == "Player")
{
CanBeOpened = true;
}
}
2023-01-01 17:03:35 +01:00
public virtual void OnTriggerExit2D(Collider2D collision)
{
// don't listen when component is disabled
2023-01-02 00:05:47 +01:00
if (ComponentEnabledCondition())
return;
if (collision.gameObject.tag == "Player")
{
CanBeOpened = false;
2023-01-02 00:05:47 +01:00
// Shop dont have dialoge in this script but in its own one
// TODO change - pass eachd ialogue to this script and use conditions from child class
if (Dialogue != null && GetCurrentDialoguePanelStatus())
{
Dialogue.BreakDialogueStep();
}
}
}
2023-01-01 17:03:35 +01:00
public virtual void CreateInstanceBasedOnLanguage()
{
2023-01-02 00:05:47 +01:00
// DONT CLONE TO HAVE CONSTANT DATA - init - deep copy - to prevent overwritting chil;dern :D
Dialogue = languageDetector.DetectInstanceBasedOnLanguage();
Dialogue.ResetDialogue();
/* var cloneDialogues = new List<IndexValuePair<int, Dialogue>>(Dialogue.Dialogues);
Dialogue.Dialogues.Clear();
foreach (var dial in cloneDialogues)
{
Dialogue.Dialogues.Add(new IndexValuePair<int, Dialogue>(dial.Key, Instantiate(dial.Value)));
}*/
Dialogue.SetSpeakerName(PlayerPrefs.GetString("name"));
2023-01-01 17:19:16 +01:00
// Bind setted actions
BindEndActionsToDialogues();
2023-01-02 00:05:47 +01:00
// Add one more action - to reset proggress
Dialogue.Dialogues.ForEach(dial => dial.Value.SetActionAfterEachDialogueStep(dial.Value.ResetDialogue)); // reset dial
2023-01-01 17:03:35 +01:00
}
#region Open Panel API
public void OpenDialoguePanel()
{
if(OpenPanelCondition())
2022-12-24 20:16:41 +01:00
{
gameObject.GetComponent<DialogueManager>().Dialogue.StartDialogue();
2022-12-24 20:16:41 +01:00
}
}
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;
}
2022-12-24 20:16:41 +01:00
protected virtual bool ComponentEnabledCondition()
{
return !gameObject.GetComponent<DialogueManager>().enabled;
}
2023-01-01 17:03:35 +01:00
#endregion
/// <summary>
/// Opened = 1
/// Closed = 0
/// </summary>
/// <returns></returns>
2022-12-27 15:16:59 +01:00
public virtual bool GetCurrentDialoguePanelStatus()
{
var multiDialStatius = Dialogue.DialogueStepStatus();
2023-01-03 22:44:24 +01:00
2023-01-02 00:05:47 +01:00
// parse result (dialoge no, step no)
// dialogue not exist - finished
// dialoge exists & step no > steps sum - finished
// dialoge no exists && step no exists - not finished - check panel status
2023-01-02 00:05:47 +01:00
if(!Dialogue.Dialogues.Where(dial => dial.Key == multiDialStatius.Item1).Any())
return false;
var fixedDialogue = Dialogue.Dialogues.Where(dial => dial.Key == multiDialStatius.Item1).First().Value;
if (fixedDialogue.CurrentStep >= fixedDialogue.DialogueSteps.Count())
return false;
// CONTINUE dont increment counter - only mark checkboxes
if (fixedDialogue
.DialogueSteps
.Where(step => step.WasDisplayed == false)
.ToArray()
.Count() == 0)
return false;
// when we are here we can be sure given points are correct
return Dialogue
.Dialogues
.Where(dialogue => dialogue.Key == multiDialStatius.Item1)
.First()
.Value
2022-12-24 20:16:41 +01:00
.DialogueSteps
.ElementAtOrDefault(multiDialStatius.Item2)
2022-12-27 15:16:59 +01:00
.DialogueController.CurrentPanel != null;
}
2022-12-28 16:16:08 +01:00
public void BindEndActionsToDialogues()
{
foreach(var finalAction in EndactionEventList)
{
// finalAction.Key.Key | .Value/
Dialogue
.Dialogues
.Where(dial => dial.Key == finalAction.Key.Key)
.FirstOrDefault()
.Value
.DialogueSteps
.ElementAtOrDefault(finalAction.Key.Value)
.SetActionAfterDialogueStep(finalAction.Value)
;
}
}
}