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

309 lines
8.6 KiB
C#
Raw Normal View History

2022-12-04 18:42:34 +01:00
using System;
2022-10-16 19:40:44 +02:00
using System.Collections.Generic;
2022-12-06 21:35:17 +01:00
using UnityEditor;
2022-10-16 19:40:44 +02:00
using UnityEngine;
2022-12-20 03:30:37 +01:00
using UnityEngine.Events;
2022-12-06 21:35:17 +01:00
2022-10-16 19:40:44 +02:00
[Serializable]
2022-12-04 18:42:34 +01:00
[CreateAssetMenu(fileName = "New Dialogue", menuName = "Dialogue/New Dialogue")]
2022-12-06 21:35:17 +01:00
public class Dialogue : ScriptableObject, IDialogue
2022-10-16 19:40:44 +02:00
{
[SerializeField]
public string SpeakerName;
[SerializeField]
2022-12-27 15:16:59 +01:00
public int CurrentStep = 0;
[SerializeField]
2022-12-06 21:35:17 +01:00
public List<DialogueStepModel> DialogueSteps;
2022-12-06 21:35:17 +01:00
public void SetSpeakerName(string speakerName)
2022-10-16 19:40:44 +02:00
{
2022-12-06 21:35:17 +01:00
SpeakerName = speakerName;
DialogueSteps.ForEach(step => step.Header = speakerName);
}
2022-12-20 03:30:37 +01:00
#region finish action api
2023-01-02 00:05:47 +01:00
public void SetActionAfterEachDialogueStep(UnityEvent _endOfDialogueStepAction)
{
foreach(var dialogueStep in DialogueSteps)
{
dialogueStep.SetActionAfterDialogueStep(_endOfDialogueStepAction);
}
}
public void SetActionAfterEachDialogueStep(Action _finishDialogueAction)
{
foreach (var dialogueStep in DialogueSteps)
{
dialogueStep.SetActionAfterDialogueStep(_finishDialogueAction);
}
}
2022-12-20 03:30:37 +01:00
public void SetActionAfterDialogueStep(int dialogueStepNo, UnityEvent _endOfDialogueStepAction)
{
if(DialogueSteps.Count >= dialogueStepNo)
DialogueSteps[dialogueStepNo].SetActionAfterDialogueStep(_endOfDialogueStepAction);
}
public void SetActionAfterDialogueStep(int dialogueStepNo, Action _finishDialogueAction)
{
2023-01-02 00:05:47 +01:00
/* Debug.Log(dialogueStepNo); */
2022-12-20 03:30:37 +01:00
if (DialogueSteps.Count >= dialogueStepNo)
DialogueSteps[dialogueStepNo].SetActionAfterDialogueStep(_finishDialogueAction);
}
#endregion
#region dialogue displaying api
public void StartDialogue()
{
// 1. Build
2022-12-06 21:35:17 +01:00
BuildDialogue(DialogueSteps);
// 2. Show first step
2022-12-06 21:35:17 +01:00
ShowDialogueStepPanel();
}
2022-12-06 21:35:17 +01:00
public void BreakDialogueStep()
{
2022-12-06 21:35:17 +01:00
foreach (var DialogueStep in DialogueSteps)
{
if (!DialogueStep.WasDisplayed && DialogueStep.DialogueController.CurrentPanel != null)
{
DialogueStep.DialogueController.CloseCurrentPanel(); // close panel
break;
}
}
2022-10-16 19:40:44 +02:00
}
2023-01-02 00:05:47 +01:00
/// <summary>
/// Function to
/// - close panel
/// - INVOKE FINAL DIALOGUE ACTION
/// - mark as dispalyed
/// - increasing step number
///
/// Dont invoke final action!!!
/// </summary>
public void FinishDialogue()
{
foreach (var DialogueStep in DialogueSteps)
{
if (!DialogueStep.WasDisplayed)
{
// status must be changes before invoked final action - example - updating state
DialogueStep.WasDisplayed = true;
CurrentStep++;
DialogueStep.DialogueController.FinishCurrentDialogue(DialogueStep.DialogueController);
break;
}
}
}
/// <summary>
/// Function to
/// - close panel
/// - mark as dispalyed
/// - increasing step number
///
/// Dont invoke final action!!!
/// </summary>
public void CloseDialogue()
{
foreach (var DialogueStep in DialogueSteps)
{
if (!DialogueStep.WasDisplayed)
{
DialogueStep.WasDisplayed = true;
CurrentStep++;
DialogueStep.DialogueController.CloseCurrentPanel(DialogueStep.DialogueController);
break;
}
}
}
2022-10-16 19:40:44 +02:00
/// <summary>
/// Dialogue API
///
/// MAIN bunction to begin dialogue
/// Create new panel instance on scene by force with sentence from queue
///
/// The best way is to invoked its after player reaction from other script (by event - collision) :)
2022-10-16 19:40:44 +02:00
/// </summary>
2022-12-06 21:35:17 +01:00
public void ShowDialogueStepPanel()
2022-10-16 19:40:44 +02:00
{
2022-12-06 21:35:17 +01:00
foreach (var DialogueStep in DialogueSteps)
2022-10-16 19:40:44 +02:00
{
2022-12-04 18:42:34 +01:00
if (!DialogueStep.WasDisplayed && DialogueStep.DialogueController.listOfDialogue.Count != 0)
2022-10-16 19:40:44 +02:00
{
DialogueStep.DialogueController.Show(DialogueStep.DialogueController.listOfDialogue.Dequeue()); // create panel
break;
}
}
}
2022-12-27 15:16:59 +01:00
/// <summary>
/// Show next sentence ONLY in the same step
///
/// Return next panel status ponly for first faounded not displayed dialogue step
///
/// TODO change name this function and one below - but this break buttons with this action assigned
///
/// It would be much safer if function were return int not void bu unity event dont allow to bind methods in that scenario
/// </summary>
/// <returns></returns>
public void ShowNextPanel()
{
// we dont use foreach and "was displayed" condition because ther is DATA LEAK
// MISLEADING - last sentence mark step ad "wasDisplayed" but not increase "CurrentPanel" index
var currentStep = GetCurrentStep();
var nextSentenceIndex = currentStep.DialogueController.ShowNextPanel(currentStep.DialogueController);
if (!nextSentenceIndex)
{
CurrentStep++;
Debug.Log($"Increase - {CurrentStep}");
}
//return nextSentenceIndex;
}
/// <summary>
/// Dialogue API
///
2022-12-06 21:35:17 +01:00
/// Function to automaticly go to next dialgue sentence (in this or next step eg after pressing button on panel
/// It provide that dialogue not braking after finishing current step
///
/// The dialogue follows the order of the lists ! !
///
/// It's default function to get next sentence if dialogue is currently started
/// It is responsible for detecting
/// 1) if there is any dialogue panel already created before displaying next sentence
/// 2) if its last panel sentence and invoiking 'end dialogue' action
///
/// Whats more, if there was no more sentences in 'step', althroughr closing panel go to next step automatiiccally
/// </summary>
public void GoToNextSentence()
{
2022-12-27 15:16:59 +01:00
// wskazujemy na inny krok kolejno niz nieoznaczony
// MISLEADING - last sentence mark step ad "wasDisplayed" but not increase "CurrentPanel" index
2022-12-06 21:35:17 +01:00
foreach (var DialogueStep in DialogueSteps)
{
if (!DialogueStep.WasDisplayed)
{
var nextSentence = DialogueStep.DialogueController.ShowNextPanel(DialogueStep.DialogueController);
if (!nextSentence)
{
DialogueStep.WasDisplayed = true;
2022-12-27 15:16:59 +01:00
CurrentStep++;
2023-01-02 00:05:47 +01:00
2022-12-06 21:35:17 +01:00
ShowDialogueStepPanel();
}
2022-10-16 19:40:44 +02:00
break;
}
}
}
2023-01-02 00:05:47 +01:00
2022-12-20 03:30:37 +01:00
#endregion
2022-10-16 19:40:44 +02:00
2023-01-02 00:05:47 +01:00
public void InvokeFinalAction()
{
foreach (var DialogueStep in DialogueSteps)
{
if (!DialogueStep.WasDisplayed)
{
DialogueStep.DialogueController.FinishDialoguStep();
break;
}
}
}
2022-12-27 15:16:59 +01:00
public void BuildDialogue()
{
BuildDialogue(DialogueSteps);
}
/// <summary>
2022-12-06 21:35:17 +01:00
/// Function to build each step of dialogue
/// </summary>
2022-12-06 21:35:17 +01:00
public void BuildDialogue(List<DialogueStepModel> Dialogue)
{
foreach (var dialogueStep in Dialogue)
{
dialogueStep.Header = SpeakerName;
dialogueStep.Build();
2022-12-27 15:16:59 +01:00
// synchronize current step counter
// cause ERROR - multi counting ....
}
}
2022-12-20 03:30:37 +01:00
2023-01-02 00:05:47 +01:00
#region Dialogue Status API
2022-12-20 03:30:37 +01:00
/// <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);
}
2022-12-27 15:16:59 +01:00
2023-01-01 17:03:35 +01:00
public void UpdateDialogueState(DialogueData dialogueData)
{
// 1 Rule - speaker name shouldnt change !!! its our 'key'
CurrentStep = dialogueData.CurrentStep;
for(int i=0; i < dialogueData.DialogueStepModelDataList.Count; i++)
{
DialogueSteps[i].WasDisplayed = dialogueData.DialogueStepModelDataList[i].WasDisplayed;
}
}
2023-01-02 00:05:47 +01:00
#endregion
2023-01-01 17:03:35 +01:00
2022-12-27 15:16:59 +01:00
public DialogueStepModel GetCurrentStep()
{// TODO - argument out of colection :/
return DialogueSteps[CurrentStep];
}
2023-01-01 17:03:35 +01:00
2022-12-27 15:16:59 +01:00
/* public DialogueStepModel GetCurrentStep()
{
foreach (var DialogueStep in DialogueSteps)
{
Debug.Log("-" + DialogueStep.DialogueController.listOfDialogue.Count);
if (!DialogueStep.WasDisplayed && DialogueStep.DialogueController.listOfDialogue.Count != 0)
{
return DialogueStep;
}
}
return null;
}*/
2022-12-04 18:42:34 +01:00
}