2022-12-23 16:02:04 +01:00
|
|
|
|
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<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()
|
|
|
|
|
{
|
|
|
|
|
Dialogue = Instantiate(languageDetector.DetectInstanceBasedOnLanguage());
|
2022-12-28 16:16:08 +01:00
|
|
|
|
// Bind setted actions
|
|
|
|
|
BindEndActionsToDialogues();
|
|
|
|
|
|
|
|
|
|
// Add one more action
|
2022-12-23 16:02:04 +01:00
|
|
|
|
Dialogue.Dialogues.ForEach(dial => dial.Value.SetActionAfterDialogueStep(dial.Key, dial.Value.ResetDialogue)); // reset dial
|
2022-12-28 16:16:08 +01:00
|
|
|
|
|
|
|
|
|
Dialogue.BuildCurrentDialogue();
|
|
|
|
|
|
|
|
|
|
/*Debug.Log(Dialogue.DialogueStepStatus());
|
|
|
|
|
Debug.Log(Dialogue
|
|
|
|
|
.Dialogues
|
|
|
|
|
.Where(dialogue => dialogue.Key == Dialogue.DialogueStepStatus().Item1)
|
|
|
|
|
.First()
|
|
|
|
|
.Value);
|
|
|
|
|
|
|
|
|
|
Debug.Log(
|
|
|
|
|
Dialogue
|
|
|
|
|
.Dialogues
|
|
|
|
|
.Where(dialogue => dialogue.Key == Dialogue.DialogueStepStatus().Item1)
|
|
|
|
|
.First()
|
|
|
|
|
.Value
|
|
|
|
|
.DialogueSteps.Count());
|
|
|
|
|
|
|
|
|
|
Debug.Log(
|
|
|
|
|
Dialogue
|
|
|
|
|
.Dialogues
|
|
|
|
|
.Where(dialogue => dialogue.Key == Dialogue.DialogueStepStatus().Item1)
|
|
|
|
|
.First()
|
|
|
|
|
.Value
|
|
|
|
|
.DialogueSteps
|
|
|
|
|
.ElementAtOrDefault(Dialogue.DialogueStepStatus().Item2)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Debug.Log(
|
|
|
|
|
Dialogue
|
|
|
|
|
.Dialogues
|
|
|
|
|
.Where(dialogue => dialogue.Key == Dialogue.DialogueStepStatus().Item1)
|
|
|
|
|
.First()
|
|
|
|
|
.Value
|
|
|
|
|
.DialogueSteps
|
|
|
|
|
.ElementAtOrDefault(Dialogue.DialogueStepStatus().Item2)
|
|
|
|
|
.DialogueController
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Debug.Log(
|
|
|
|
|
Dialogue
|
|
|
|
|
.Dialogues
|
|
|
|
|
.Where(dialogue => dialogue.Key == Dialogue.DialogueStepStatus().Item1)
|
|
|
|
|
.First()
|
|
|
|
|
.Value
|
|
|
|
|
.DialogueSteps
|
|
|
|
|
.ElementAtOrDefault(Dialogue.DialogueStepStatus().Item2)
|
|
|
|
|
.DialogueController
|
|
|
|
|
.listOfDialogue.Count()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Debug.Log(GetCurrentDialoguePanelStatus());*/
|
2022-12-23 16:02:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Update()
|
|
|
|
|
{
|
|
|
|
|
if (OpenInDefaultWay && Input.GetKeyDown(keyToOpen) && OpenPanelCondition())
|
2022-12-24 20:16:41 +01:00
|
|
|
|
{
|
2022-12-23 16:02:04 +01:00
|
|
|
|
OpenDialoguePanel();
|
2022-12-24 20:16:41 +01:00
|
|
|
|
}
|
2022-12-23 16:02:04 +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;
|
|
|
|
|
|
|
|
|
|
SpeakerName = collision.gameObject.name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public virtual void OnTriggerExit2D(Collider2D collision)
|
|
|
|
|
{
|
|
|
|
|
// don't listen when component is disabled
|
2022-12-28 16:16:08 +01:00
|
|
|
|
if (ComponentEnabledCondition() || !OpenInDefaultWay)
|
2022-12-23 16:02:04 +01:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (collision.gameObject.tag == "Player")
|
|
|
|
|
{
|
|
|
|
|
CanBeOpened = false;
|
|
|
|
|
|
|
|
|
|
if (GetCurrentDialoguePanelStatus())
|
|
|
|
|
{
|
|
|
|
|
Dialogue.BreakDialogueStep();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OpenDialoguePanel()
|
|
|
|
|
{
|
|
|
|
|
if(OpenPanelCondition())
|
2022-12-24 20:16:41 +01:00
|
|
|
|
{
|
|
|
|
|
// Update speaker name before every opening
|
|
|
|
|
Dialogue.SetSpeakerName(SpeakerName);
|
|
|
|
|
|
2022-12-23 16:02:04 +01:00
|
|
|
|
gameObject.GetComponent<DialogueManager>().Dialogue.StartDialogue();
|
2022-12-24 20:16:41 +01:00
|
|
|
|
}
|
2022-12-23 16:02:04 +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
|
|
|
|
|
2022-12-23 16:02:04 +01:00
|
|
|
|
protected virtual bool ComponentEnabledCondition()
|
|
|
|
|
{
|
|
|
|
|
return !gameObject.GetComponent<DialogueManager>().enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Opened = 1
|
|
|
|
|
/// Closed = 0
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool GetCurrentDialoguePanelStatus()
|
|
|
|
|
{
|
2022-12-28 16:21:08 +01:00
|
|
|
|
Debug.Log(Dialogue);
|
2022-12-23 16:02:04 +01:00
|
|
|
|
var multiDialStatius = Dialogue.DialogueStepStatus();
|
2022-12-28 16:21:08 +01:00
|
|
|
|
Debug.Log(multiDialStatius);
|
2022-12-28 16:25:20 +01:00
|
|
|
|
|
2022-12-28 16:21:08 +01:00
|
|
|
|
Debug.Log(Dialogue
|
|
|
|
|
.Dialogues
|
|
|
|
|
.Where(dialogue => dialogue.Key == multiDialStatius.Item1)
|
|
|
|
|
.First()
|
|
|
|
|
.Value
|
|
|
|
|
.DialogueSteps);
|
2022-12-28 16:25:20 +01:00
|
|
|
|
|
2022-12-23 16:02:04 +01:00
|
|
|
|
return Dialogue
|
|
|
|
|
.Dialogues
|
|
|
|
|
.Where(dialogue => dialogue.Key == multiDialStatius.Item1)
|
|
|
|
|
.First()
|
|
|
|
|
.Value
|
2022-12-24 20:16:41 +01:00
|
|
|
|
.DialogueSteps
|
|
|
|
|
.ElementAtOrDefault(multiDialStatius.Item2)
|
|
|
|
|
?.DialogueController.CurrentPanel != null;
|
2022-12-23 16:02:04 +01:00
|
|
|
|
}
|
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)
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-23 16:02:04 +01:00
|
|
|
|
}
|
|
|
|
|
|