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

176 lines
4.9 KiB
C#
Raw 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
2022-12-04 18:42:34 +01:00
{
2022-12-27 15:16:59 +01:00
[SerializeField]
public Mission MissionTemplate;
/* 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();
}
public override void Update() { }
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;
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
{
Debug.Log("Start - UpdateProggres");
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
if (OpenInDefaultWay && 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
Mission = MissionTemplate;
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()
{
return Mission
.GetCurrentStep()
.DialogueStep
2023-01-03 22:44:24 +01:00
.GetCurrentStep()
?.DialogueController
2022-12-27 15:16:59 +01:00
.CurrentPanel != null;
}
}