Scriptum/Assets/Scripts/REFACTORING/Application/Mission/NpcMissionManager.cs

194 lines
5.5 KiB
C#
Raw Permalink Normal View History

2022-12-04 18:42:34 +01:00
using System;
using System.Collections;
using System.Collections.Generic;
2022-12-27 15:16:59 +01:00
using System.Linq;
2022-12-04 18:42:34 +01:00
using UnityEngine;
//[Serializable]
2022-12-27 15:16:59 +01:00
2022-12-04 18:42:34 +01:00
//[CreateAssetMenu(fileName = "New Mission", menuName = "Mission/New Mission")]
2022-12-27 15:16:59 +01:00
public class NpcMissionManager : DialogueManager // ScriptableObject
2023-01-15 13:29:43 +01:00
{
2022-12-27 15:16:59 +01:00
[SerializeField]
2023-01-15 13:29:43 +01:00
public LanguageDetector<Mission> missionLanguageDetector;
2022-12-27 15:16:59 +01:00
/* We user object CLONED TO PREVENT overwritting changes in main object inassets */
[SerializeField]
public Mission Mission;
public bool OpenMissionInDefaultWay = true;
2022-12-27 15:16:59 +01:00
[SerializeField]
public DialogueStepModel FreeDialogue;
// List<Key<mission No, mission step No>, Value : UnityEvent>
2023-01-01 17:03:35 +01:00
//public List<IndexValuePair<IndexValuePair<int, int>, MissionCondition>> EndactionEventList;
private void Awake()
2022-12-04 18:42:34 +01:00
{
2023-01-03 22:44:24 +01:00
CanBeOpened = false;
OpenInDefaultWay = false;
}
2023-01-03 22:44:24 +01:00
public override void Start()
{
//base.Start();
2023-01-03 22:44:24 +01:00
CreateInstanceBasedOnLanguage();
2023-01-03 22:44:24 +01:00
// UPDATE DIALOGUE
// search in scene manager list
if (Mission != null)
{
var machedDialogueData = ((MissionDataManager)MissionDataManager.Instance).GetMission(Mission);
if (machedDialogueData != null)
{
UpdateMissionState(machedDialogueData);
}
else
{
Debug.Log("Start - UpdateProggres");
UpdateProggres();
}
}
Mission.BuildMission();
2022-12-27 15:16:59 +01:00
if (FreeDialogue != null)
FreeDialogue.Build();
}
2023-01-12 15:22:49 +01:00
public override void Update()
{
if (!OpenInDefaultWay && Input.GetKeyDown(keyToOpen) && OpenPanelCondition())
{
if (languageDetector.InstanceTemplates.Count() > 0)
{
// create and config dailogue
CreateInstanceBasedOnLanguage();
Mission.BuildMission();
}
Mission.StartDialogue();
}
}
2022-12-27 15:16:59 +01:00
public override void OnTriggerEnter2D(Collider2D collision)
{
// don't listen when component is disabled
if (ComponentEnabledCondition() || collision.tag != "Player")
2022-12-27 15:16:59 +01:00
return;
2023-01-12 15:22:49 +01:00
Debug.Log("OnTriggerEnter2D");
2022-12-27 15:16:59 +01:00
if (collision.gameObject.tag == "Player" && !GetCurrentDialoguePanelStatus())
2022-12-04 18:42:34 +01:00
{
2022-12-27 15:16:59 +01:00
CanBeOpened = true;
CreateInstanceBasedOnLanguage();
// UPDATE DIALOGUE
// search in scene manager list
if (Mission != null)
{
var machedDialogueData = ((MissionDataManager)MissionDataManager.Instance).GetMission(Mission);
if (machedDialogueData != null)
{
UpdateMissionState(machedDialogueData);
}
else
{
UpdateProggres();
}
}
Mission.BuildMission();
if (!OpenInDefaultWay)
return;
2022-12-27 15:16:59 +01:00
Mission.StartDialogue();
2022-12-04 18:42:34 +01:00
}
}
2022-12-27 15:16:59 +01:00
public override void OnTriggerExit2D(Collider2D collision)
2022-12-04 18:42:34 +01:00
{
2022-12-27 15:16:59 +01:00
// don't listen when component is disabled
if (ComponentEnabledCondition())
return;
if (collision.gameObject.tag == "Player")
2022-12-04 18:42:34 +01:00
{
2022-12-27 15:16:59 +01:00
CanBeOpened = false;
2022-12-04 18:42:34 +01:00
2023-01-12 15:22:49 +01:00
// dont depend on OpenInDefaultWay flag - colse alway if were opened and player move foward
if (Mission != null && GetCurrentDialoguePanelStatus())
2022-12-27 15:16:59 +01:00
{
Debug.Log("BreakDialogueStep");
Mission.BreakDialogueStep();
2022-12-04 18:42:34 +01:00
}
}
}
2022-12-27 15:16:59 +01:00
2023-01-03 22:44:24 +01:00
public override void CreateInstanceBasedOnLanguage()
{
// DONT CLONE TO HAVE CONSTANT DATA - init - deep copy - to prevent overwritting chil;dern :D
2023-01-15 13:29:43 +01:00
Mission = missionLanguageDetector.DetectInstanceBasedOnLanguage();
2023-01-03 22:44:24 +01:00
Mission.ResetMission();
/* 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)));
}*/
Mission.SetSpeakerName(gameObject.GetComponent<NPC>().Name);
// update dial state - sync in order to prepare to save
Mission.MissionStepsList.ForEach(dial => dial.DialogueStep.SetActionAfterEachDialogueStep(UpdateProggres));
2022-12-27 15:16:59 +01:00
2023-01-03 22:44:24 +01:00
// Add one more action - to reset proggress
//Dialogue.Dialogues.ForEach(dial => dial.Value.SetActionAfterEachDialogueStep(dial.Value.ResetDialogue)); // reset dial
}
public void UpdateMissionState(MissionData missionState)
2022-12-27 15:16:59 +01:00
{
2023-01-03 22:44:24 +01:00
// 1 Rule - speaker name shouldnt change !!! its our 'key'
Mission.UpdateMissionState(missionState);
2022-12-27 15:16:59 +01:00
}
2023-01-03 22:44:24 +01:00
/// <summary>
/// Function to sync local dialogue state with remote one
/// Handled in Scene Manager
/// </summary>
public void UpdateProggres()
{
((MissionDataManager)MissionDataManager.Instance).RegisterMission(Mission);
}
2022-12-27 15:16:59 +01:00
protected override bool ComponentEnabledCondition()
{
return !gameObject.GetComponent<NpcMissionManager>().enabled;
}
public override bool GetCurrentDialoguePanelStatus()
{
2023-01-07 17:49:15 +01:00
if (Mission.CurrentStep >= Mission.MissionStepsList.Count())
return false;
2022-12-27 15:16:59 +01:00
return Mission
.GetCurrentStep()
.DialogueStep
2023-01-03 22:44:24 +01:00
.GetCurrentStep()
?.DialogueController
2022-12-27 15:16:59 +01:00
.CurrentPanel != null;
}
}