Scriptum/Assets/Scripts/REFACTORING/Application/Panel/Chest/SceneChestDataManager.cs

160 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[Serializable]
public class SceneChestDataManager : SceneBaseDataManager<ChestPrefabAsset>
{
protected override string OBJECT_FOLDER_NAME { get { return "Chest"; } }
protected override string OBJECT_LIST_NAME { get { return "ChestList"; } }
//protected new SceneBaseDataLoader<ChestPrefabAsset> DataLoader { get; set; }
protected SceneChestBuilder ChestBuilder { get; set; }
[SerializeField] Chest CurrentChest;
[SerializeField] protected new ChestUIManager UiManager;
[SerializeField] public List<ChestPrefabAssetData> ElementsToBuildOnSceneList = new List<ChestPrefabAssetData>();
[SerializeField] List<ChestPrefabAsset> convertedData = new List<ChestPrefabAsset>();
public override void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
public override void Start()
{
Debug.Log("Start SceneChestData manager");
//TaskUIManager.FindOrCreateInstance();
UiManager = ChestUIManager.Instance;
if (UiManager == null)
throw new NullReferenceException("ChestUIManager not found!!!");
DynamicDataList = (new ChestDataListManager()).SetUiManager(ref UiManager);
DataLoader = new SceneChestDataLoader(OBJECT_LIST_NAME, OBJECT_FOLDER_NAME);
ChestBuilder = new SceneChestBuilder();
base.Start();
}
// TODO - save ChestPrefabAsset (we need to save coords...) but in manager handle Chest data..
//
protected override void AfterStart()
{
if(OnMapAppearanceMethod.GameStatus == GameStatus.NewGame)
{
if (OnMapAppearanceMethod.Gateway != OnMapAppearanceMethodEnum.NewGame)
UseDynamicSettings(); // if there is nothing saved we will use UseDefaultSettings
else
UseDefaultSettings();
}else
{
UseDynamicSettings();
}
}
protected SceneBaseDataManager<ChestPrefabAsset> GetObjectType()
{
return GameObject.FindObjectOfType<SceneChestDataManager>();
}
protected SceneBaseDataManager<ChestPrefabAsset> CreateInstance(ref GameObject managerGameObject)
{
return managerGameObject.AddComponent<SceneChestDataManager>();
}
protected override void UseDefaultSettings()
{
foreach (ChestPrefabAssetData asset in ElementsToBuildOnSceneList) { convertedData.Add((ChestPrefabAsset)asset.MapDataToPrefabAssetModel()); }
ChestBuilder.ElementsToBuildOnSceneList = convertedData;
ChestBuilder.BuildList();
DynamicDataList.SetList(convertedData);
List<Chest> tmp = new List<Chest>();
foreach (ChestPrefabAsset asset in ChestBuilder.ElementsToBuildOnSceneList) { tmp.Add(asset.Chest); }
UiManager.SetList(tmp);
}
protected override void UseDynamicSettings()
{
// 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
LoadDynamicData();
// VERy VERY provisional SOLUTION
if (DynamicDataList.GetList().Count == 0)
{
UseDefaultSettings();
return;
}
ChestBuilder.ElementsToBuildOnSceneList = DynamicDataList.GetList();
ChestBuilder.BuildList();
List<Chest> tmp = new List<Chest>();
foreach (ChestPrefabAsset asset in ChestBuilder.ElementsToBuildOnSceneList) { tmp.Add(asset.Chest); }
UiManager.SetList(tmp);
}
// TODO manager api
// open panel extend function
// close panel extend function
// TODO chest api
// add to chest - find chest by its name np
// remove from chest - select chest by its name -
// chesk item in chest - check chest by tis name
public override bool SaveDynamicData()
{
// TODO
// how to get list
// 1. List in UI manager should be synchronized with list in this manager
foreach (Chest chest in UiManager.GetList())
{
DynamicDataList.GetList().Where(el => el.Name == chest.Name).ToList().ForEach(slot => slot.Chest = chest);
}
//DynamicDataList.SetList(tmp);
// approach:
// 1. get from outside, update local list && us it
// 2. Handle list synchronized all the time & pass local list
return SaveData(DynamicDataList.GetList(), SceneElementTypeEnum.Dynamic);
}
}