Scriptum/Assets/Scripts/SceneManager/SceneEquippableItemManager.cs
kabix09 602c94726a Add scene manager for items
Start rebuild Save & Load game system
2022-06-19 01:25:49 +02:00

348 lines
12 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
using System.IO;
using System.Linq;
// only for eqipptable items
public class SceneEquippableItemManager : MonoBehaviour
{
private const string GameObjectLocalization = "Assets/Items/";
private const string AssetLocalization = "Assets/Items/";
private const string DYNAMIC_ELEMENT = "/DynamicElements/";
private const string STATIC_ELEMENT = "/StaticElements/";
// content skrzyni (docelowo skrzynia + jej content)
/// <Summary>
/// Handle manually setuped object properties
/// eg. npc, minions, chest content
/// </Summary>
[SerializeField]
public List<EquippableItemPrefarbAsset> StaticElements;
/// <Summary>
/// Handle dynamic object properties
/// eg. dropped items
/// </Summary>
// PrefarbAsset
[SerializeField]
public List<EquippableItemPrefarbAsset> DynamicElements;
public bool isNewGame = true;
public bool isContinued = false;
public string MapName;
public string ElementFolderName = "EquippableItem";
public string ItemsListName = "EquippableItemList";
public static SceneEquippableItemManager Instance;
public void Awake()
{
if(Instance == null)
{
this.MapName = SceneManager.GetActiveScene().name;
if(isNewGame) // in new game dynamicItemsList is defaulty empty
{
BuildItems(StaticElements);
}else if(isContinued)
{
LoadEquippableItems();
BuildItems(DynamicElements);
}
Instance = this;
}else if (Instance != this)
{
Destroy(gameObject);
}
}
public void Start()
{
// BuildDynamicItems();
//SaveEquippableItems();
LoadEquippableItems();
BuildItems(StaticElements);
BuildItems(DynamicElements);
}
// public void AddDynamicItem(GameObject object) {
// //EquippableItem item = object.GetComponent<PickableController>().Item;
// //EquippableItemPrefarbAsset equippableItemPrefarbAsset = new EquippableItemPrefarbAsset(item.name, object.name, object.transform.position, item);
// //this.dynamicItems.Add(equippableItemPrefarbAsset);
// }
public int AddDynamicItem(GameObject dynamicObject)
{
EquippableItem item = dynamicObject.GetComponent<PickableController>().item;
EquippableItemPrefarbAsset equippableItemPrefarbAsset = new EquippableItemPrefarbAsset(item.name, dynamicObject.name, dynamicObject.transform.position, item);
this.DynamicElements.Add(equippableItemPrefarbAsset);
return this.DynamicElements.Count - 1;
}
public void RemoveDynamicItem(string name)
{
// 1. Fetch all matched items
List<EquippableItemPrefarbAsset> equippableItemPrefarbAssetList = this.DynamicElements.Where(eqItemAss => eqItemAss.name == name).ToList();
// 2. Remove them
this.DynamicElements.RemoveAll(eqItemAss => eqItemAss.name == name);
}
public void RemoveDynamicEquippableItem(EquippableItemPrefarbAsset equippableItemPrefarbAsset)
{
}
public void BuildItems(List<EquippableItemPrefarbAsset> equippableItemPrefarbAssetList)
{
foreach(EquippableItemPrefarbAsset equippableItemPrefarbAsset in equippableItemPrefarbAssetList)
{
GameObject newEquippableItemObject = (GameObject)AssetDatabase.LoadAssetAtPath(GameObjectLocalization + equippableItemPrefarbAsset.prefarbAssetName + ".prefab", typeof(GameObject));
if(!newEquippableItemObject)
{
Debug.Log("Can't find prefarb by name " + equippableItemPrefarbAsset.prefarbAssetName);
break;
}
GameObject globalGUI = GameObject.FindGameObjectsWithTag("GUI")[0];
if(globalGUI)
{
// 1. Create gameObject by handled prefarb
// 2. SetUp
// 2.1 Set position
GameObject equippableItem = Instantiate(newEquippableItemObject, equippableItemPrefarbAsset.position, Quaternion.identity, globalGUI.transform);
// 2.2 Set name
equippableItem.name = equippableItemPrefarbAsset.name;
equippableItem.transform.SetParent(globalGUI.transform);
// 2.3 Set pransform
//equippableItem.transform.localScale = new Vector3(0.4f, 0.4f, 1);
// Debug.Log(equippableItemPrefarbAsset.position);
//equippableItem.transform.localPosition = equippableItemPrefarbAsset.position;
// 3. SetUp object properties
// 3.1 find object
string[] assetNames = AssetDatabase.FindAssets("t:EquippableItem", new[] { AssetLocalization });
foreach (string SOName in assetNames)
{
var SOpath = AssetDatabase.GUIDToAssetPath(SOName);
if(SOpath.Contains(equippableItemPrefarbAsset.prefarbAssetName + ".asset"))
{
// 3.2 set EquippableItem object
equippableItem.GetComponent<PickableController>().item = AssetDatabase.LoadAssetAtPath<EquippableItem>(SOpath);
break;
}
}
} else {
Debug.Log("Can't find global GUI object!!!");
break;
}
}
}
/// Save both list of EquippableItems
public void SaveEquippableItems()
{
// 1. staticElements
this.SaveStaticEquippableItemsList();
// 2. dynamicItems
this.SaveDynamicEquippableItemsList();
/// Load One element example
// foreach(EquippableItemPrefarbAsset equippableItemPrefarbAsset in equippableItemPrefarbAssetList)
// {
// SaveEquippableItemSystem.SaveEquitableItem(equippableItemPrefarbAsset, this.MapName + "/" + this.ElementFolderName); // change to SaveEquippableItemSystem for EquippableItemPrefarbAsset
// }
}
#region Static list of EquippableItem Save
private void SaveStaticEquippableItemsList()
{
// Case I - if we remember all list
// 1) if after removed item form DynamicList is empty - remove all file
// 2) if after removed item form DynamciList there are another one - save updated list again
if(this.StaticElements.Count > 0) {
SaveEquippableItemSystem.SaveEquitableItemList(this.StaticElements, this.MapName + STATIC_ELEMENT, this.ItemsListName); // change to SaveEquippableItemSystem for EquippableItemPrefarbAsset
} else {
string _path = SaveSystem.GetSavePath(this.MapName + STATIC_ELEMENT) + "/" + this.ItemsListName + ".fun";
try
{
Debug.Log("File to remove: " + _path);
if(File.Exists(_path))
{
File.Delete(_path);
}
}
catch (IOException ioExp)
{
Debug.LogError(ioExp.Message);
}
}
// Case II - if we rememenber object per file
// 1) remove specyfic file
//
// Unfortunatelly we don't use this way of saving items yet :D
}
#endregion
#region Dynamic list of EquippableItem Save
private void SaveDynamicEquippableItemsList()
{
// Case I - if we remember all list
// 1) if after removed item form DynamicList is empty - remove all file
// 2) if after removed item form DynamciList there are another one - save updated list again
if(this.DynamicElements.Count > 0) {
SaveEquippableItemSystem.SaveEquitableItemList(this.DynamicElements, this.MapName + DYNAMIC_ELEMENT, this.ItemsListName); // change to SaveEquippableItemSystem for EquippableItemPrefarbAsset
} else {
string _path = SaveSystem.GetSavePath(this.MapName + DYNAMIC_ELEMENT) + "/" + this.ItemsListName + ".fun";
try
{
Debug.Log("File to remove: " + _path);
if(File.Exists(_path))
{
File.Delete(_path);
}
}
catch (IOException ioExp)
{
Debug.LogError(ioExp.Message);
}
}
// Case II - if we rememenber object per file
// 1) remove specyfic file
//
// Unfortunatelly we don't use this way of saving items yet :D
}
#endregion
public void LoadEquippableItems()
{
// if continue -> search files with content in save path
// if new game -> build objects from default configuration
this.LoadDynamicEquippableItemsList();
/// <summary>
/// DID WE REALLY NEED TO LOAD (or even remembered) THIS LIST????
/// </summary>
//this.LoadStaticEquippableItemsList();
}
#region Dynamic list of EquippableItem Loader
public void LoadDynamicEquippableItem()
{
string path = SaveSystem.GetSavePath(this.MapName + DYNAMIC_ELEMENT + this.ElementFolderName);
if (!Directory.Exists(path)) // if not exists thats mean there was nothing saved yet - nothing to load
return;
FileInfo[] fileInfo = new DirectoryInfo(path).GetFiles();
foreach(FileInfo file in fileInfo)
{
Debug.Log(file.FullName);
EquippableItemPrefarbAssetData equippableItemPrefarbAssetData = SaveEquippableItemSystem.LoadEquitableItem(file.Name, this.MapName + DYNAMIC_ELEMENT + this.ElementFolderName);
DynamicElements.Add(equippableItemPrefarbAssetData.MapDataToEquippableItemPrefarbAsset());
}
}
public void LoadDynamicEquippableItemsList()
{
string path = SaveSystem.GetSavePath(this.MapName + DYNAMIC_ELEMENT);
if (!Directory.Exists(path)) // if not exists thats mean there was nothing saved yet - nothing to load
return;
FileInfo[] fileInfo = new DirectoryInfo(path).GetFiles();
foreach(FileInfo file in fileInfo)
{
if(file.Name != this.ItemsListName + ".fun")
continue;
List<EquippableItemPrefarbAssetData> equippableItemPrefarbAssetDataList = SaveEquippableItemSystem.LoadEquitableItemList(this.MapName + DYNAMIC_ELEMENT, this.ItemsListName);
foreach(EquippableItemPrefarbAssetData equippableItemPrefarbAssetData in equippableItemPrefarbAssetDataList)
DynamicElements.Add(equippableItemPrefarbAssetData.MapDataToEquippableItemPrefarbAsset());
}
}
#endregion
#region Static list of EquippableItem Loader
public void LoadStaticEquippableItem()
{
string path = SaveSystem.GetSavePath(this.MapName + STATIC_ELEMENT + this.ElementFolderName);
if (!Directory.Exists(path)) // if not exists thats mean there was nothing saved yet - nothing to load
return;
FileInfo[] fileInfo = new DirectoryInfo(path).GetFiles();
foreach(FileInfo file in fileInfo)
{
Debug.Log(file.FullName);
EquippableItemPrefarbAssetData equippableItemPrefarbAssetData = SaveEquippableItemSystem.LoadEquitableItem(file.Name, this.MapName + STATIC_ELEMENT + this.ElementFolderName);
StaticElements.Add(equippableItemPrefarbAssetData.MapDataToEquippableItemPrefarbAsset());
}
}
public void LoadStaticEquippableItemsList()
{
string path = SaveSystem.GetSavePath(this.MapName + STATIC_ELEMENT);
if (!Directory.Exists(path)) // if not exists thats mean there was nothing saved yet - nothing to load
return;
FileInfo[] fileInfo = new DirectoryInfo(path).GetFiles();
foreach(FileInfo file in fileInfo)
{
Debug.Log(file.FullName);
List<EquippableItemPrefarbAssetData> equippableItemPrefarbAssetDataList = SaveEquippableItemSystem.LoadEquitableItemList(this.MapName + STATIC_ELEMENT, this.ItemsListName);
foreach(EquippableItemPrefarbAssetData equippableItemPrefarbAssetData in equippableItemPrefarbAssetDataList)
StaticElements.Add(equippableItemPrefarbAssetData.MapDataToEquippableItemPrefarbAsset());
}
}
#endregion
}