Scriptum/Assets/Scripts/REFACTORING/Application/Shared/Manager/Panel/SceneBaseDataManager.cs

154 lines
4.1 KiB
C#
Raw Normal View History

2022-10-22 18:04:21 +02:00
using System;
using System.Collections.Generic;
using UnityEngine;
2022-11-06 21:34:17 +01:00
/*where T : Task*/
public abstract class SceneBaseDataManager<T> : MonoBehaviour
2022-10-22 18:04:21 +02:00
{
protected virtual string OBJECT_FOLDER_NAME => "";
protected virtual string OBJECT_LIST_NAME => "";
protected bool IsNewGame = true; // change to use global "gateway"
protected DataListManager<T> StaticDataList;
protected DataListManager<T> DynamicDataList;
2022-11-06 21:34:17 +01:00
protected SceneBaseDataLoader<T> DataLoader { get; set; }
2022-10-22 18:04:21 +02:00
public static SceneBaseDataManager<T> Instance; // { get; private set; }
public virtual void Awake()
2022-10-22 18:04:21 +02:00
{
if (Instance == null)
2022-10-22 18:04:21 +02:00
{
2022-11-24 03:03:30 +01:00
Debug.Log("Create: "+gameObject);
Instance = this;
}
else
{
2022-11-24 03:03:30 +01:00
Debug.Log(Instance);
Debug.LogError(gameObject);
Destroy(gameObject);
2022-10-22 18:04:21 +02:00
}
}
protected static SceneBaseDataManager<T> GetObjectType()
{
return GameObject.FindObjectOfType<SceneBaseDataManager<T>>();
}
protected static SceneBaseDataManager<T> CreateInstance()
{
var managerGameObject = HandleManagerOnScene();
if (!managerGameObject)
throw new Exception("Game object to srote Managers scripts not found on scene!!!");
return managerGameObject.AddComponent<SceneBaseDataManager<T>>();
}
/// <summary>
/// Function to handel GLOBAL manager object from scene
/// </summary>
/// <returns></returns>
protected static GameObject HandleManagerOnScene()
{
return GameObject.FindGameObjectWithTag("Manager").transform.Find("DataManger").gameObject;
}
2022-11-06 21:34:17 +01:00
protected virtual bool LoadData(SceneElementTypeEnum type, ref DataListManager<T> dataListManager)
2022-10-22 18:04:21 +02:00
{
try
{
dataListManager.SetList(
DataLoader.LoadData(type) as List<T>
2022-10-22 18:04:21 +02:00
);
return true;
}
catch (Exception e) {
Debug.LogWarning(e.Message);
}
return false;
}
2022-11-06 21:34:17 +01:00
protected virtual bool SaveData(List<T> _elements, SceneElementTypeEnum type)
2022-10-22 18:04:21 +02:00
{
try
{
DataLoader.SaveData(_elements, type);
return true;
}
catch (Exception e) {
Debug.LogError(e.Message);
2022-10-22 18:04:21 +02:00
}
return false;
}
2022-10-22 18:04:21 +02:00
/// <summary>
/// Part of static list CURD methods
/// </summary>
public bool LoadStaticData()
{
return LoadData(SceneElementTypeEnum.Static, ref StaticDataList);
}
protected virtual void AddElementToStaticList(T _element)
{
StaticDataList.AddElementToList(_element);
}
protected virtual void RemoveElementFromStaticList(T _element)
{
StaticDataList.RemoveElementFromList(_element);
}
public bool SaveStaticData()
{
// TODO
// how to get list
// 1. List in UI manager should be synchronized with list in this manager
2022-11-06 21:34:17 +01:00
2022-10-22 18:04:21 +02:00
// approach:
// 1. get from outside, update local list && us it
// 2. Handle list synchronized all the time & pass local list
2022-11-06 21:34:17 +01:00
Debug.Log("SaveStaticData");
2022-10-22 18:04:21 +02:00
return SaveData(StaticDataList.GetList(), SceneElementTypeEnum.Static);
}
/// <summary>
/// Part of dynamic list CURD methods
/// </summary>
public bool LoadDynamicData()
{
return LoadData(SceneElementTypeEnum.Dynamic, ref DynamicDataList);
}
protected virtual void AddElementToDynamicList(T _element)
{
DynamicDataList.AddElementToList(_element);
}
protected virtual void RemoveElementFromDynamicList(T _element)
{
DynamicDataList.RemoveElementFromList(_element);
}
2022-11-24 03:03:30 +01:00
public virtual bool SaveDynamicData()
2022-10-22 18:04:21 +02:00
{
// TODO
// how to get list
// 1. List in UI manager should be synchronized with list in this manager
2022-11-24 03:03:30 +01:00
2022-10-22 18:04:21 +02:00
// approach:
// 1. get from outside, update local list && us it
// 2. Handle list synchronized all the time & pass local list
return SaveData(DynamicDataList.GetList(), SceneElementTypeEnum.Dynamic);
}
}