Scriptum/Assets/Scripts/SceneManager/SceneChestManager.cs

274 lines
9.4 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 SceneChestManager : MonoBehaviour
{
private const string GameObjectLocalization = "Assets/Items/Chest/"; // for prefarb
private const string AssetLocalization = "Assets/Items/"; // for Equitabble asset
//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<ChestPrefarbAsset> StaticElements;
/// <Summary>
/// Handle dynamic object properties
/// eg. dropped items
/// </Summary>
// PrefarbAsset
[SerializeField]
public List<ChestPrefarbAsset> DynamicElements;
public bool isNewGame = true;
public bool isContinued = false;
public string MapName;
public string ElementFolderName = "Chest";
public string ItemsListName = "ChestList";
public static SceneChestManager Instance;
public void Awake()
{
if(Instance == null)
{
this.MapName = SceneManager.GetActiveScene().name;
if(isNewGame) // in new game dynamicItemsList is defaulty empty
{
//BuildChests(StaticElements);
}else if(isContinued)
{
//LoadChests();
//BuildChests(DynamicElements);
}
Instance = this;
}else if (Instance != this)
{
Destroy(gameObject);
}
}
public void Start()
{
// EXAMPLE HOW TO ADD ELEMENTS TO CHEST
//
// this.StaticElements.Add(
// new ChestPrefarbAsset("test chest", "Wooden Chest", new Vector3(0, 0, 0),
// new Dictionary<int, EquippableItemPrefarbAsset>{
// {1, new EquippableItemPrefarbAsset("Pickaxe test", "Pickaxe", new Vector3(0,0,0), "Pickaxe")},
// {4, new EquippableItemPrefarbAsset("Gold ore test", "Gold Ore", new Vector3(0,0,0), "Gold Ore")},
// })
// );
LoadChests();
BuildChests(StaticElements);
}
public int AddItemToChest(string chestName, int keyPosition, EquippableItem addedItem){
// 1. Find Chest in List and update value
this.StaticElements.Where(chestPrefarbAsset => chestPrefarbAsset.name == chestName).ToList().ForEach(chest => {
chest.content[keyPosition] = new EquippableItemPrefarbAsset(addedItem.name, addedItem.name, new Vector3(0,0,0), addedItem);
Debug.Log("Added element " + addedItem.name + " to chest (" + chestName + ")");
});
return keyPosition;
}
public void RemoveItemFromChest(string chestName, int keyPosition)
{
// 1. Find Chest in List - baically ther should be one chest with this name but who know :D
this.StaticElements.Where(chestPrefarbAsset => chestPrefarbAsset.name == chestName).ToList().ForEach(chest => {
chest.content.Remove(keyPosition);
Debug.Log("Removed element on position -" + keyPosition + " - from chest (" + chestName + ")");
});
}
// public void RemoveDynamicEquippableItem(EquippableItemPrefarbAsset equippableItemPrefarbAsset)
// {
// }
public void BuildChests(List<ChestPrefarbAsset> chestPrefarbAssetList)
{
foreach(ChestPrefarbAsset chestPrefarbAsset in chestPrefarbAssetList)
{
GameObject newChestObject = (GameObject)AssetDatabase.LoadAssetAtPath(GameObjectLocalization + chestPrefarbAsset.prefarbAssetName + ".prefab", typeof(GameObject));
if(!newChestObject)
{
Debug.Log("Can't find prefarb by name " + chestPrefarbAsset.prefarbAssetName);
break;
}
GameObject globalGUI = GameObject.FindGameObjectsWithTag("GUI")[0];
if(globalGUI)
{
// 1. Create gameObject by handled prefarb
// 2. SetUp
// 2.1 Set position
GameObject chest = Instantiate(newChestObject, chestPrefarbAsset.position, Quaternion.identity, globalGUI.transform);
// 2.2 Set name
chest.name = chestPrefarbAsset.name;
//chest.transform.SetParent(globalGUI.transform);
// 2.3 Set pransform
chest.transform.localScale = new Vector3(0.5f, 0.5f, 1);
// 3. SetUp EqippableItems list
Dictionary<int, EquippableItemPrefarbAsset> itemsOfChestList = chestPrefarbAsset.content;
string[] assetNames = AssetDatabase.FindAssets("t:EquippableItem", new[] { AssetLocalization });
foreach(KeyValuePair<int, EquippableItemPrefarbAsset> itemsOfChestEntry in itemsOfChestList)
{
// 3.1 find object
foreach (string SOName in assetNames)
{
var SOpath = AssetDatabase.GUIDToAssetPath(SOName);
if(SOpath.Contains(itemsOfChestEntry.Value.prefarbAssetName + ".asset"))
{
// 3.2 set EquippableItem object
chest.GetComponent<ChestController>().SetupItemInChest(
itemsOfChestEntry.Key,
AssetDatabase.LoadAssetAtPath<EquippableItem>(SOpath)
);
break;
}
}
}
} else {
Debug.Log("Can't find global GUI object!!!");
break;
}
}
}
// public void BuildChestItems(Dictionary<int, EquippableItemPrefarbAsset> itemsList)
// {
// // pass to InventoryManager singleton
// foreach(KeyValuePair<int, EquippableItemPrefarbAsset> itemEntry in itemsList)
// InventoryManager.Instance.SetupItemInInventory(itemEntry.Key, itemEntry.Value.equippableItem);
// }
/// Save both list of EquippableItems
public void SaveChests()
{
// 1. staticElements
this.SaveStaticChestsList();
}
#region Static list of EquippableItem Save
private void SaveStaticChestsList()
{
// 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) {
SaveChestSystem.SaveChestsList(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
public void LoadChests()
{
// if continue -> search files with content in save path
// if new game -> build objects from default configuration
Debug.Log("Load chest");
this.LoadStaticChestsList();
}
#region Static list of Chest Loader
public void LoadStaticChestsList()
{
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<ChestPrefarbAsset> chestPrefarbAssetList = new List<ChestPrefarbAsset>();
List<ChestPrefarbAssetData> chestPrefarbAssetDataList = SaveChestSystem.LoadChestsList(this.MapName + STATIC_ELEMENT, this.ItemsListName);
foreach(ChestPrefarbAssetData chestPrefarbAssetData in chestPrefarbAssetDataList)
chestPrefarbAssetList.Add(chestPrefarbAssetData.MapDataToChestPrefarbAsset());
Debug.Log("---- Load Chest");
foreach(ChestPrefarbAsset chest in chestPrefarbAssetList)
{
Debug.Log(chest.name);
foreach(KeyValuePair<int, EquippableItemPrefarbAsset> equippableItemPrefarbAsset in chest.content)
{
// Debug.Log("item on position " + equippableItemPrefarbAsset.Key +
// " " + equippableItemPrefarbAsset.Value.equippableItem.name);
}
}
this.StaticElements = chestPrefarbAssetList;
}
}
#endregion
}