Scriptum/Assets/Scripts/REFACTORING/Application/Panel/Equipment/EquipmentDataManager.cs
2023-01-06 03:44:07 +01:00

153 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class EquipmentDataManager : SceneBaseDataManager<IndexValuePair<int, EquippableItem>>
{
protected override string OBJECT_FOLDER_NAME { get { return "Equipment"; } }
protected override string OBJECT_LIST_NAME { get { return "EquipmentList"; } }
protected new SceneBaseDataLoader<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> DataLoader { get; set; }
[SerializeField] public List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> ElementsToBuildOnSceneList = new List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>>();
[SerializeField] List<IndexValuePair<int, EquippableItem>> convertedData = new List<IndexValuePair<int, EquippableItem>>();
public override void Awake()
{
if (Instance == null)
{
Debug.Log("Create: " + gameObject);
Instance = this;
}
else
{
Debug.Log(Instance);
Debug.LogError(gameObject);
Destroy(gameObject);
}
}
public override void Start()
{
Debug.Log("Start SceneEquipmentData manager");
//TaskUIManager.FindOrCreateInstance();
UiManager = EquipmentUIManager.Instance;
if (UiManager == null)
throw new NullReferenceException("EquipmentUIManager not found!!!");
//StaticDataList = (new EquipmentDataListManager()).SetUiManager(ref taskManager);
DynamicDataList = (new EquipmentDataListManager()).SetUiManager(ref UiManager);
DataLoader = new EquipmentDataLoader(OBJECT_LIST_NAME, OBJECT_FOLDER_NAME);
((EquipmentDataListManager)DynamicDataList).InitEquipment();
base.Start();
}
protected SceneBaseDataManager<IndexValuePair<int, EquippableItem>> GetObjectType()
{
return GameObject.FindObjectOfType<EquipmentDataManager>();
}
protected SceneBaseDataManager<IndexValuePair<int, EquippableItem>> CreateInstance(ref GameObject managerGameObject)
{
return managerGameObject.AddComponent<EquipmentDataManager>();
}
protected override void UseDefaultSettings()
{
foreach (IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset> asset in ElementsToBuildOnSceneList)
{
convertedData.Add(
new IndexValuePair<int, EquippableItem>((int)asset.Key, (EquippableItem)asset.Value.EquippableItem)
);
}
DynamicDataList.SetList(convertedData);
UiManager.SetList(convertedData);
}
protected override void UseDynamicSettings()
{
LoadDynamicData();
UiManager.SetList(DynamicDataList.GetList());
}
#region override load & save
protected override bool LoadData(SceneElementTypeEnum type, ref DataListManager<IndexValuePair<int, EquippableItem>> dataListManager)
{
try
{
// 1. Convert EquippableItemPrefabAsset to EquippableItem list
List<IndexValuePair<int, EquippableItem>> convertedList = new List<IndexValuePair<int, EquippableItem>>();
foreach (IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset> loadedEquippableItemPrefarbAssetElement in (List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>>)DataLoader.LoadData(type))
{
if(loadedEquippableItemPrefarbAssetElement.Value != null)
convertedList.Add(new IndexValuePair<int, EquippableItem>((int)loadedEquippableItemPrefarbAssetElement.Key, (EquippableItem)loadedEquippableItemPrefarbAssetElement.Value.EquippableItem));
else
convertedList.Add(new IndexValuePair<int, EquippableItem>((int)loadedEquippableItemPrefarbAssetElement.Key, null));
}
// 2. Pass loaded list to InventoryDataManager
dataListManager.SetList(convertedList);
return true;
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
return false;
}
protected override bool SaveData(List<IndexValuePair<int, EquippableItem>> _elements, SceneElementTypeEnum type)
{
try
{
// 1. Convert EquippableItem to EquippableItemPrefabAsset list
List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>> convertedList = new List<IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>>();
foreach (IndexValuePair<int, EquippableItem> itemElement in _elements)
{
if(itemElement.Value)
convertedList.Add(new IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>(
(EquipmentPanelSlotsTypeEnum)itemElement.Key,
new EquippableItemPrefabAsset(
itemElement.Value.Name,
itemElement.Value.ItemModel.name,
new Vector3(0, 0, 0),
itemElement.Value
)
));
else
convertedList.Add(new IndexValuePair<EquipmentPanelSlotsTypeEnum, EquippableItemPrefabAsset>((EquipmentPanelSlotsTypeEnum)itemElement.Key, null));
}
// 2. Pass loaded list to InventoryDataManager
DataLoader.SaveData(convertedList, type);
return true;
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
return false;
}
#endregion
}