Scriptum/Assets/Scripts/REFACTORING/Application/Panel/Equipment/EquipmentDataManager.cs
2022-11-24 03:03:30 +01:00

157 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; }
bool NewGame = false;
public override void Awake()
{
if (Instance == null)
{
Debug.Log("Create: " + gameObject);
Instance = this;
}
else
{
Debug.Log(Instance);
Debug.LogError(gameObject);
Destroy(gameObject);
}
}
private void Start()
{
Debug.Log("Start SceneEquipmentData manager");
//TaskUIManager.FindOrCreateInstance();
var taskManager = EquipmentUIManager.Instance;
if (taskManager == null)
throw new NullReferenceException("EquipmentUIManager not found!!!");
//StaticDataList = (new EquipmentDataListManager()).SetUiManager(ref taskManager);
DynamicDataList = (new EquipmentDataListManager()).SetUiManager(ref taskManager);
DataLoader = new EquipmentDataLoader(OBJECT_LIST_NAME, OBJECT_FOLDER_NAME);
// taskManager.SetList();
//SaveData(taskManager.GetList(), SceneElementTypeEnum.None);
((EquipmentDataListManager)DynamicDataList).InitEquipment();
//LoadData(SceneElementTypeEnum.None, ref StaticDataList);
//taskManager.SetList(StaticDataList.GetList());
if (NewGame)
{
//taskManager.SetList(tmp);
}
else
{
LoadDynamicData();
taskManager.SetList(DynamicDataList.GetList());
// BuildList();
// when chest detect player in near arrea and player press "c"
// open panel -> chest controller handle Open panel in Scene Chest Manager passing info about what Chest Palyer want to open
// Manager Build panel and pass info about chest content
}
}
#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
protected SceneBaseDataManager<IndexValuePair<int, EquippableItem>> GetObjectType()
{
return GameObject.FindObjectOfType<EquipmentDataManager>();
}
protected SceneBaseDataManager<IndexValuePair<int, EquippableItem>> CreateInstance(ref GameObject managerGameObject)
{
return managerGameObject.AddComponent<EquipmentDataManager>();
}
public override bool SaveDynamicData()
{
Debug.Log("SaveDynamicData");
// tem approach
DynamicDataList.SetList(EquipmentUIManager.Instance.GetList());
return base.SaveDynamicData();
}
}