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

153 lines
4.8 KiB
C#
Raw Permalink 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
2022-11-27 21:28:55 +01:00
public override void Start()
2022-11-06 21:34:17 +01:00
{
Debug.Log("Start SceneInventoryData manager");
//TaskUIManager.FindOrCreateInstance();
2022-11-27 21:28:55 +01:00
UiManager = InventoryUIManager.Instance;
2022-11-06 21:34:17 +01:00
2022-11-27 21:28:55 +01:00
if (UiManager == null)
2022-11-06 21:34:17 +01:00
throw new NullReferenceException("InventoryUIManager not found!!!");
2022-11-27 21:28:55 +01:00
//StaticDataList = (new InventoryDataListManager()).SetUiManager(ref inventoryManager);
DynamicDataList = (new InventoryDataListManager()).SetUiManager(ref UiManager);
2022-11-06 21:34:17 +01:00
DataLoader = new SceneInventoryDataLoader(OBJECT_LIST_NAME, OBJECT_FOLDER_NAME);
2022-11-27 21:28:55 +01:00
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>();
}
2022-11-06 21:34:17 +01:00
2022-11-27 21:28:55 +01:00
protected override void UseDefaultSettings()
{
return; // at the beginning we want to have inventory empty
}
2022-11-06 21:34:17 +01:00
2022-11-27 21:28:55 +01:00
protected override void UseDynamicSettings()
{
LoadDynamicData();
2022-11-06 21:34:17 +01:00
2022-11-27 21:28:55 +01:00
UiManager.SetList(DynamicDataList.GetList());
2022-11-06 21:34:17 +01:00
}
2022-11-27 21:28:55 +01:00
// 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;
}
*/
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
{
2022-12-06 01:58:32 +01:00
2022-11-06 21:34:17 +01:00
// 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-27 21:28:55 +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-12-06 01:58:32 +01:00
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
}