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 => "";
|
|
|
|
|
|
|
|
|
2022-11-27 21:28:55 +01:00
|
|
|
protected UIBaseManager<T> UiManager;
|
|
|
|
|
2022-10-22 18:04:21 +02:00
|
|
|
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; }
|
|
|
|
|
2022-11-19 17:02:31 +01:00
|
|
|
public virtual void Awake()
|
2022-10-22 18:04:21 +02:00
|
|
|
{
|
2022-11-19 17:02:31 +01:00
|
|
|
if (Instance == null)
|
2022-10-22 18:04:21 +02:00
|
|
|
{
|
2022-11-24 03:03:30 +01:00
|
|
|
Debug.Log("Create: "+gameObject);
|
|
|
|
|
2022-11-19 17:02:31 +01:00
|
|
|
Instance = this;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-24 03:03:30 +01:00
|
|
|
Debug.Log(Instance);
|
|
|
|
Debug.LogError(gameObject);
|
2022-11-19 17:02:31 +01:00
|
|
|
Destroy(gameObject);
|
2022-10-22 18:04:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 21:28:55 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Function to manage objects on scene depending on class responsibility
|
|
|
|
///
|
|
|
|
/// Important - USE THIS METHOD IN EVERY CHILD'S 'start' FUNCTION
|
|
|
|
/// </summary>
|
|
|
|
public virtual void Start()
|
|
|
|
{
|
|
|
|
// Decide which element use during game
|
|
|
|
AfterStart();
|
|
|
|
}
|
|
|
|
|
2022-10-22 18:04:21 +02:00
|
|
|
protected static SceneBaseDataManager<T> GetObjectType()
|
|
|
|
{
|
|
|
|
return GameObject.FindObjectOfType<SceneBaseDataManager<T>>();
|
|
|
|
}
|
|
|
|
|
2022-11-27 21:28:55 +01:00
|
|
|
/*
|
|
|
|
* TODO WHAT WITH THAT...
|
|
|
|
* protected static SceneBaseDataManager<T> CreateInstance()
|
|
|
|
{
|
|
|
|
var managerGameObject = HandleManagerOnScene();
|
2022-10-22 18:04:21 +02:00
|
|
|
|
2022-11-27 21:28:55 +01:00
|
|
|
if (!managerGameObject)
|
|
|
|
throw new Exception("Game object to srote Managers scripts not found on scene!!!");
|
2022-10-22 18:04:21 +02:00
|
|
|
|
2022-11-27 21:28:55 +01:00
|
|
|
return managerGameObject.AddComponent<SceneBaseDataManager<T>>();
|
|
|
|
}
|
2022-10-22 18:04:21 +02:00
|
|
|
|
2022-11-27 21:28:55 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Function to handel GLOBAL manager object from scene
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
protected static GameObject HandleManagerOnScene()
|
|
|
|
{
|
|
|
|
return GameObject.FindGameObjectWithTag("Manager").transform.Find("DataManger").gameObject;
|
|
|
|
}*/
|
|
|
|
|
|
|
|
protected void AfterStart()
|
2022-10-22 18:04:21 +02:00
|
|
|
{
|
2022-11-27 21:28:55 +01:00
|
|
|
if (OnMapAppearanceMethod.Gateway == OnMapAppearanceMethodEnum.NewGame)
|
|
|
|
{
|
|
|
|
UseDefaultSettings();
|
|
|
|
} else {
|
|
|
|
UseDynamicSettings();
|
|
|
|
}
|
2022-10-22 18:04:21 +02:00
|
|
|
}
|
|
|
|
|
2022-11-27 21:28:55 +01:00
|
|
|
protected abstract void UseDefaultSettings();
|
|
|
|
protected abstract void UseDynamicSettings();
|
|
|
|
|
|
|
|
#region Lad & Save
|
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(
|
2022-11-19 17:02:31 +01:00
|
|
|
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) {
|
2022-11-19 17:02:31 +01:00
|
|
|
Debug.LogError(e.Message);
|
2022-10-22 18:04:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-11-27 21:28:55 +01:00
|
|
|
#endregion
|
2022-11-19 17:02:31 +01:00
|
|
|
|
2022-11-27 21:28:55 +01:00
|
|
|
#region DynamicData Methods
|
|
|
|
/// <summary>
|
|
|
|
/// Part of dynamic list CURD methods
|
|
|
|
/// </summary>
|
2022-10-22 18:04:21 +02:00
|
|
|
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-27 21:28:55 +01:00
|
|
|
DynamicDataList.SetList(UiManager.GetList());
|
2022-11-19 17:02:31 +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);
|
|
|
|
}
|
2022-11-27 21:28:55 +01:00
|
|
|
#endregion
|
2022-10-22 18:04:21 +02:00
|
|
|
}
|