using System; using System.Collections.Generic; using UnityEngine; [Serializable] public class SceneInventoryDataManager : SceneBaseDataManager> { protected override string OBJECT_FOLDER_NAME { get { return "Inventory"; } } protected override string OBJECT_LIST_NAME { get { return "InventoryList"; } } protected new SceneBaseDataLoader> DataLoader { get; set; } public new static SceneBaseDataManager> 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> GetObjectType() { return GameObject.FindObjectOfType(); } protected SceneBaseDataManager> CreateInstance(ref GameObject managerGameObject) { return managerGameObject.AddComponent(); } 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> dataListManager) { // 1. Convert EquippableItemPrefabAsset to EquippableItem list List> convertedList = new List>(); foreach (IndexValuePair loadedEquippableItemPrefarbAssetElement in (List>)DataLoader.LoadData(SceneElementTypeEnum.None)) { convertedList.Add(new IndexValuePair(loadedEquippableItemPrefarbAssetElement.Key, (EquippableItem)loadedEquippableItemPrefarbAssetElement.Value.EquippableItem)); } // 2. Pass loaded list to InventoryDataManager dataListManager.SetList(convertedList); return true; return false; } protected override bool SaveData(List> _elements, SceneElementTypeEnum type) { try { // 1. Convert EquippableItem to EquippableItemPrefabAsset list List> convertedList = new List>(); foreach (IndexValuePair itemElement in _elements) { convertedList.Add(new IndexValuePair( 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 }