Scriptum/Assets/Scripts/REFACTORING/Application/Panel/Inventory/SceneInventoryDataManager.cs
2022-11-28 00:52:47 +01:00

158 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class SceneInventoryDataManager : SceneBaseDataManager<IndexValuePair<int, EquippableItem>>
{
protected override string OBJECT_FOLDER_NAME { get { return "Inventory"; } }
protected override string OBJECT_LIST_NAME { get { return "InventoryList"; } }
protected new SceneBaseDataLoader<IndexValuePair<int, EquippableItemPrefabAsset>> DataLoader { get; set; }
public new static SceneBaseDataManager<IndexValuePair<int, EquippableItem>> Instance; // { get; private set; }
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 SceneInventoryData manager");
//TaskUIManager.FindOrCreateInstance();
UiManager = InventoryUIManager.Instance;
if (UiManager == null)
throw new NullReferenceException("InventoryUIManager not found!!!");
//StaticDataList = (new InventoryDataListManager()).SetUiManager(ref inventoryManager);
DynamicDataList = (new InventoryDataListManager()).SetUiManager(ref UiManager);
DataLoader = new SceneInventoryDataLoader(OBJECT_LIST_NAME, OBJECT_FOLDER_NAME);
base.Start();
}
protected SceneBaseDataManager<IndexValuePair<int, EquippableItem>> GetObjectType()
{
return GameObject.FindObjectOfType<SceneInventoryDataManager>();
}
protected SceneBaseDataManager<IndexValuePair<int, EquippableItem>> CreateInstance(ref GameObject managerGameObject)
{
return managerGameObject.AddComponent<SceneInventoryDataManager>();
}
protected override void UseDefaultSettings()
{
return; // at the beginning we want to have inventory empty
}
protected override void UseDynamicSettings()
{
LoadDynamicData();
UiManager.SetList(DynamicDataList.GetList());
}
// TODO whats with that
/*
public int AddToInventory(EquippableItem pickable)
{
if (this._items.Count <= MAX_ITEMS)
{
for (int slotNumber = 0; slotNumber < MAX_ITEMS; slotNumber++)
{
if (!this._items.ContainsKey(slotNumber))
{
this._items[slotNumber] = pickable;
return slotNumber;
}
}
}
return -1;
}
*/
#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<int, EquippableItemPrefabAsset> loadedEquippableItemPrefarbAssetElement in (List<IndexValuePair<int, EquippableItemPrefabAsset>>)DataLoader.LoadData(SceneElementTypeEnum.None))
{
convertedList.Add(new IndexValuePair<int, EquippableItem>(loadedEquippableItemPrefarbAssetElement.Key, (EquippableItem)loadedEquippableItemPrefarbAssetElement.Value.EquippableItem));
}
// 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<int, EquippableItemPrefabAsset>> convertedList = new List<IndexValuePair<int, EquippableItemPrefabAsset>>();
foreach (IndexValuePair<int, EquippableItem> itemElement in _elements)
{
convertedList.Add(new IndexValuePair<int, EquippableItemPrefabAsset>(
itemElement.Key,
new EquippableItemPrefabAsset(
itemElement.Value.Name,
itemElement.Value.ItemModel.name,
new Vector3(0, 0, 0),
itemElement.Value
)
));
}
// 2. Pass loaded list to InventoryDataManager
DataLoader.SaveData(convertedList, type);
return true;
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
return false;
}
#endregion
}