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

163 lines
4.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2022-11-27 21:28:55 +01:00
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;
2022-11-27 21:28:55 +01:00
[SerializeField] protected new ChestUIManager UiManager;
2022-12-21 17:12:47 +01:00
[SerializeField] public List<ChestBuildModel> ChestsToBuildOnScene = new List<ChestBuildModel>();
2022-11-27 21:28:55 +01:00
[SerializeField] List<ChestPrefabAsset> convertedData = new List<ChestPrefabAsset>();
public override void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
}
2022-11-27 21:28:55 +01:00
public override void Start()
{
Debug.Log("Start SceneChestData manager");
//TaskUIManager.FindOrCreateInstance();
2022-11-27 21:28:55 +01:00
UiManager = ChestUIManager.Instance;
2022-11-27 21:28:55 +01:00
if (UiManager == null)
throw new NullReferenceException("ChestUIManager not found!!!");
2022-11-27 21:28:55 +01:00
DynamicDataList = (new ChestDataListManager()).SetUiManager(ref UiManager);
DataLoader = new SceneChestDataLoader(OBJECT_LIST_NAME, OBJECT_FOLDER_NAME);
ChestBuilder = new SceneChestBuilder();
2022-11-27 21:28:55 +01:00
base.Start();
}
2022-11-27 21:28:55 +01:00
// TODO - save ChestPrefabAsset (we need to save coords...) but in manager handle Chest data..
//
2022-12-06 01:20:01 +01:00
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();
}
}
2022-11-27 21:28:55 +01:00
protected SceneBaseDataManager<ChestPrefabAsset> GetObjectType()
{
return GameObject.FindObjectOfType<SceneChestDataManager>();
}
2022-11-27 21:28:55 +01:00
protected SceneBaseDataManager<ChestPrefabAsset> CreateInstance(ref GameObject managerGameObject)
{
return managerGameObject.AddComponent<SceneChestDataManager>();
}
2022-11-27 21:28:55 +01:00
protected override void UseDefaultSettings()
{
2022-12-21 17:12:47 +01:00
ChestBuilder.ElementsToBuildOnSceneList = ChestsToBuildOnScene;
2022-11-27 21:28:55 +01:00
ChestBuilder.BuildList();
2022-12-21 17:12:47 +01:00
foreach(ChestBuildModel ChestToBuild in ChestsToBuildOnScene)
{
convertedData.Add(ChestToBuild.MapBuildModelToPrefabAssetModel());
}
2022-11-27 21:28:55 +01:00
DynamicDataList.SetList(convertedData);
2022-11-27 21:28:55 +01:00
List<Chest> tmp = new List<Chest>();
2022-12-21 17:12:47 +01:00
foreach (ChestPrefabAsset asset in convertedData) { tmp.Add(asset.Chest); }
2022-11-27 21:28:55 +01:00
UiManager.SetList(tmp);
}
2022-11-27 21:28:55 +01:00
protected override void UseDynamicSettings()
2022-12-06 01:20:01 +01:00
{
2022-11-27 21:28:55 +01:00
// BuildList();
2022-11-27 21:28:55 +01:00
// 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
2022-12-06 01:20:01 +01:00
2022-11-27 21:28:55 +01:00
LoadDynamicData();
2022-11-27 21:28:55 +01:00
// VERy VERY provisional SOLUTION
if (DynamicDataList.GetList().Count == 0)
{
UseDefaultSettings();
return;
}
2022-12-21 23:32:51 +01:00
ChestBuilder.ElementsToBuildOnSceneList = ChestBuildModel.ConvertPrefabAssetListToBuildModelList(DynamicDataList.GetList());
2022-11-27 21:28:55 +01:00
ChestBuilder.BuildList();
2022-11-27 21:28:55 +01:00
List<Chest> tmp = new List<Chest>();
2022-12-21 23:32:51 +01:00
foreach (ChestPrefabAsset asset in DynamicDataList.GetList()) { tmp.Add(asset.Chest); }
2022-11-27 21:28:55 +01:00
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
2022-11-27 21:28:55 +01:00
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);
}
}