Scriptum/Assets/Scripts/REFACTORING/Application/Panel/Inventory/SceneInventoryDataManager.cs

148 lines
4.8 KiB
C#
Raw Normal View History

2022-11-06 21:34:17 +01:00
using System;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
2022-11-24 03:03:30 +01:00
public class SceneInventoryDataManager : SceneBaseDataManager<IndexValuePair<int, EquippableItem>>
2022-11-06 21:34:17 +01:00
{
protected override string OBJECT_FOLDER_NAME { get { return "Inventory"; } }
protected override string OBJECT_LIST_NAME { get { return "InventoryList"; } }
2022-11-24 03:03:30 +01:00
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);
}
}
2022-11-06 21:34:17 +01:00
private void Start()
{
Debug.Log("Start SceneInventoryData manager");
//TaskUIManager.FindOrCreateInstance();
var inventoryManager = InventoryUIManager.Instance;
if (inventoryManager == null)
throw new NullReferenceException("InventoryUIManager not found!!!");
StaticDataList = (new InventoryDataListManager()).SetUiManager(ref inventoryManager);
// DynamicDataList = (new TaskDataListManager()).SetUiManager(ref taskManager);
DataLoader = new SceneInventoryDataLoader(OBJECT_LIST_NAME, OBJECT_FOLDER_NAME);
//SaveData(StaticDataList.GetList(), SceneElementTypeEnum.None);
// LoadData(SceneElementTypeEnum.None, ref StaticDataList);
2022-11-06 21:34:17 +01:00
// LoadDynamicData();
//inventoryManager.SetList(StaticDataList.GetList());
2022-11-06 21:34:17 +01:00
//inventoryManager.OpenPanel();
2022-11-06 21:34:17 +01:00
}
#region override load & save
2022-11-24 03:03:30 +01:00
protected override bool LoadData(SceneElementTypeEnum type, ref DataListManager<IndexValuePair<int, EquippableItem>> dataListManager)
2022-11-06 21:34:17 +01:00
{
try
{
// 1. Convert EquippableItemPrefabAsset to EquippableItem list
2022-11-24 03:03:30 +01:00
List<IndexValuePair<int, EquippableItem>> convertedList = new List<IndexValuePair<int, EquippableItem>>();
2022-11-06 21:34:17 +01:00
2022-11-24 03:03:30 +01:00
foreach(IndexValuePair<int, EquippableItemPrefabAsset> loadedEquippableItemPrefarbAssetElement in (List<IndexValuePair<int, EquippableItemPrefabAsset>>)DataLoader.LoadData(SceneElementTypeEnum.None))
2022-11-06 21:34:17 +01:00
{
2022-11-24 03:03:30 +01:00
convertedList.Add(new IndexValuePair<int, EquippableItem>(loadedEquippableItemPrefarbAssetElement.Key, (EquippableItem)loadedEquippableItemPrefarbAssetElement.Value.EquippableItem));
2022-11-06 21:34:17 +01:00
}
// 2. Pass loaded list to InventoryDataManager
dataListManager.SetList(convertedList);
return true;
}
2022-11-06 21:34:17 +01:00
catch (Exception e)
{
Debug.LogError(e.Message);
2022-11-06 21:34:17 +01:00
}
return false;
}
2022-11-24 03:03:30 +01:00
protected override bool SaveData(List<IndexValuePair<int, EquippableItem>> _elements, SceneElementTypeEnum type)
2022-11-06 21:34:17 +01:00
{
try
{
// 1. Convert EquippableItem to EquippableItemPrefabAsset list
2022-11-24 03:03:30 +01:00
List<IndexValuePair<int, EquippableItemPrefabAsset>> convertedList = new List<IndexValuePair<int, EquippableItemPrefabAsset>>();
2022-11-06 21:34:17 +01:00
2022-11-24 03:03:30 +01:00
foreach (IndexValuePair<int, EquippableItem> itemElement in _elements)
2022-11-06 21:34:17 +01:00
{
2022-11-24 03:03:30 +01:00
convertedList.Add(new IndexValuePair<int, EquippableItemPrefabAsset>(
2022-11-06 21:34:17 +01:00
itemElement.Key,
new EquippableItemPrefabAsset(
itemElement.Value.Name,
itemElement.Value.ItemModel.name,
new Vector3(0, 0, 0),
2022-11-24 03:03:30 +01:00
itemElement.Value
2022-11-06 21:34:17 +01:00
)
));
}
// 2. Pass loaded list to InventoryDataManager
DataLoader.SaveData(convertedList, type);
return true;
}
catch (Exception e)
{
Debug.LogError(e.Message);
2022-11-06 21:34:17 +01:00
}
return false;
}
#endregion
2022-11-24 03:03:30 +01:00
protected SceneBaseDataManager<IndexValuePair<int, EquippableItem>> GetObjectType()
2022-11-06 21:34:17 +01:00
{
return GameObject.FindObjectOfType<SceneInventoryDataManager>();
}
2022-11-24 03:03:30 +01:00
protected SceneBaseDataManager<IndexValuePair<int, EquippableItem>> CreateInstance(ref GameObject managerGameObject)
2022-11-06 21:34:17 +01:00
{
return managerGameObject.AddComponent<SceneInventoryDataManager>();
}
// 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;
}*/
}