2022-06-17 22:22:19 +02:00
using System.Collections ;
using System.Collections.Generic ;
using UnityEngine ;
using UnityEditor ;
using UnityEngine.SceneManagement ;
using System.IO ;
using System.Linq ;
2023-01-06 03:44:07 +01:00
using System ;
2022-06-17 22:22:19 +02:00
// only for eqipptable items
public class SceneEquippableItemManager : MonoBehaviour
{
2022-11-27 21:28:55 +01:00
private const string GameObjectLocalization = "Items/" ;
2022-06-17 22:22:19 +02:00
2022-11-27 21:28:55 +01:00
private const string AssetLocalization = "Items/" ;
2022-06-17 22:22:19 +02:00
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]
2022-11-19 17:02:31 +01:00
public List < EquippableItemPrefabAsset > StaticElements ;
2022-06-17 22:22:19 +02:00
/// <Summary>
/// Handle dynamic object properties
/// eg. dropped items
/// </Summary>
// PrefarbAsset
[SerializeField]
2022-11-19 17:02:31 +01:00
public List < EquippableItemPrefabAsset > DynamicElements ;
2022-06-17 22:22:19 +02:00
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 ;
2022-12-06 01:20:01 +01:00
if ( OnMapAppearanceMethod . IsNewGame ( ) ) // in new game dynamicItemsList is defaulty empty
2022-06-17 22:22:19 +02:00
{
BuildItems ( StaticElements ) ;
2022-11-27 21:28:55 +01:00
} else
2022-06-17 22:22:19 +02:00
{
2023-01-06 03:44:07 +01:00
Debug . Log ( "SceneEquippableItemManager - Load" ) ;
2022-06-17 22:22:19 +02:00
LoadEquippableItems ( ) ;
BuildItems ( DynamicElements ) ;
}
Instance = this ;
} else if ( Instance ! = this )
{
Destroy ( gameObject ) ;
}
}
public void Start ( )
{
// BuildDynamicItems();
//SaveEquippableItems();
2022-12-06 01:20:01 +01:00
//LoadEquippableItems();
//BuildItems(StaticElements);
//BuildItems(DynamicElements);
2022-06-17 22:22:19 +02:00
}
// public void AddDynamicItem(GameObject object) {
// //EquippableItem item = object.GetComponent<PickableController>().Item;
2022-11-19 17:02:31 +01:00
// //EquippableItemPrefabAsset equippableItemPrefarbAsset = new EquippableItemPrefabAsset(item.name, object.name, object.transform.position, item);
2022-06-17 22:22:19 +02:00
// //this.dynamicItems.Add(equippableItemPrefarbAsset);
// }
public int AddDynamicItem ( GameObject dynamicObject )
{
2022-12-06 01:58:32 +01:00
EquippableItem item = new EquippableItem ( dynamicObject . GetComponent < PickableController > ( ) . item ) ;
2022-11-19 17:02:31 +01:00
EquippableItemPrefabAsset equippableItemPrefarbAsset = new EquippableItemPrefabAsset ( item . name , dynamicObject . name , dynamicObject . transform . position , item ) ;
2022-06-17 22:22:19 +02:00
this . DynamicElements . Add ( equippableItemPrefarbAsset ) ;
return this . DynamicElements . Count - 1 ;
}
public void RemoveDynamicItem ( string name )
{
// 1. Fetch all matched items
2022-11-19 17:02:31 +01:00
List < EquippableItemPrefabAsset > equippableItemPrefarbAssetList = this . DynamicElements . Where ( eqItemAss = > eqItemAss . Name = = name ) . ToList ( ) ;
2022-06-17 22:22:19 +02:00
// 2. Remove them
2022-11-19 17:02:31 +01:00
this . DynamicElements . RemoveAll ( eqItemAss = > eqItemAss . Name = = name ) ;
2022-06-17 22:22:19 +02:00
}
2022-11-19 17:02:31 +01:00
public void RemoveDynamicEquippableItem ( EquippableItemPrefabAsset equippableItemPrefarbAsset )
2022-06-17 22:22:19 +02:00
{
}
2022-11-19 17:02:31 +01:00
public void BuildItems ( List < EquippableItemPrefabAsset > equippableItemPrefarbAssetList )
2022-06-17 22:22:19 +02:00
{
2022-11-19 17:02:31 +01:00
foreach ( EquippableItemPrefabAsset equippableItemPrefarbAsset in equippableItemPrefarbAssetList )
2022-06-17 22:22:19 +02:00
{
2022-11-27 21:28:55 +01:00
2023-01-06 03:44:07 +01:00
Debug . Log ( GameObjectLocalization + equippableItemPrefarbAsset . PrefabAssetName ) ;
2022-11-27 21:28:55 +01:00
GameObject newEquippableItemObject = Resources . Load < GameObject > ( GameObjectLocalization + equippableItemPrefarbAsset . PrefabAssetName ) ;
if ( ! newEquippableItemObject )
2022-06-17 22:22:19 +02:00
{
2023-01-06 03:44:07 +01:00
string trimmed = String . Concat ( equippableItemPrefarbAsset . PrefabAssetName . Where ( c = > ! Char . IsWhiteSpace ( c ) ) ) ;
newEquippableItemObject = Resources . Load < GameObject > ( GameObjectLocalization + trimmed ) ;
if ( ! newEquippableItemObject )
{
Debug . Log ( "Can't find prefarb by name " + equippableItemPrefarbAsset . PrefabAssetName ) ;
break ;
}
2022-06-17 22:22:19 +02:00
}
2023-01-06 03:44:07 +01:00
GameObject globalGUI = GameObject . FindGameObjectWithTag ( "ItemCollection" ) ;
2022-06-17 22:22:19 +02:00
if ( globalGUI )
{
// 1. Create gameObject by handled prefarb
// 2. SetUp
// 2.1 Set position
2022-11-19 17:02:31 +01:00
GameObject equippableItem = Instantiate ( newEquippableItemObject , equippableItemPrefarbAsset . Position , Quaternion . identity , globalGUI . transform ) ;
2022-06-17 22:22:19 +02:00
// 2.2 Set name
2022-11-19 17:02:31 +01:00
equippableItem . name = equippableItemPrefarbAsset . Name ;
2022-06-17 22:22:19 +02:00
2023-01-06 03:44:07 +01:00
equippableItem . transform . localScale = new Vector3 ( 1 , 1 , 1 ) ;
2022-06-17 22:22:19 +02:00
equippableItem . transform . SetParent ( globalGUI . transform ) ;
2023-01-06 03:44:07 +01:00
2022-06-17 22:22:19 +02:00
// 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
2022-11-27 21:28:55 +01:00
// 3.2 set EquippableItem object
2023-01-06 03:44:07 +01:00
// DONT DO IT
//equippableItem.GetComponent<PickableController>().item = Resources.Load<EquippableItem>("Items/"+equippableItemPrefarbAsset.PrefabAssetName);
2022-11-27 21:28:55 +01:00
2022-06-17 22:22:19 +02:00
2023-01-06 03:44:07 +01:00
}
else {
2022-06-17 22:22:19 +02:00
Debug . Log ( "Can't find global GUI object!!!" ) ;
break ;
}
}
}
/// Save both list of EquippableItems
public void SaveEquippableItems ( )
{
2023-01-06 03:44:07 +01:00
// 1. staticElements - uselles
//this.SaveStaticEquippableItemsList();
2022-06-17 22:22:19 +02:00
// 2. dynamicItems
this . SaveDynamicEquippableItemsList ( ) ;
/// Load One element example
2022-11-19 17:02:31 +01:00
// foreach(EquippableItemPrefabAsset equippableItemPrefarbAsset in equippableItemPrefarbAssetList)
2022-06-17 22:22:19 +02:00
// {
2022-11-19 17:02:31 +01:00
// SaveEquippableItemSystem.SaveEquitableItem(equippableItemPrefarbAsset, this.MapName + "/" + this.ElementFolderName); // change to SaveEquippableItemSystem for EquippableItemPrefabAsset
2022-06-17 22:22:19 +02:00
// }
}
#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 ) {
2022-11-19 17:02:31 +01:00
SaveEquippableItemSystem . SaveEquitableItemList ( this . StaticElements , this . MapName + STATIC_ELEMENT , this . ItemsListName ) ; // change to SaveEquippableItemSystem for EquippableItemPrefabAsset
2022-06-17 22:22:19 +02:00
} 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 ) {
2022-11-19 17:02:31 +01:00
SaveEquippableItemSystem . SaveEquitableItemList ( this . DynamicElements , this . MapName + DYNAMIC_ELEMENT , this . ItemsListName ) ; // change to SaveEquippableItemSystem for EquippableItemPrefabAsset
2022-06-17 22:22:19 +02:00
} 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 ) ;
2022-11-19 17:02:31 +01:00
EquippableItemPrefabAssetData equippableItemPrefarbAssetData = SaveEquippableItemSystem . LoadEquitableItem ( file . Name , this . MapName + DYNAMIC_ELEMENT + this . ElementFolderName ) ;
2022-06-17 22:22:19 +02:00
2022-11-19 17:02:31 +01:00
DynamicElements . Add ( ( EquippableItemPrefabAsset ) ( ( EquippableItemPrefabAssetData ) equippableItemPrefarbAssetData ) . MapDataToPrefabAssetModel ( ) ) ;
2022-06-17 22:22:19 +02:00
}
}
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 ;
2022-11-19 17:02:31 +01:00
List < EquippableItemPrefabAssetData > equippableItemPrefarbAssetDataList = SaveEquippableItemSystem . LoadEquitableItemList ( this . MapName + DYNAMIC_ELEMENT , this . ItemsListName ) ;
2022-06-17 22:22:19 +02:00
2022-11-19 17:02:31 +01:00
foreach ( EquippableItemPrefabAssetData equippableItemPrefarbAssetData in equippableItemPrefarbAssetDataList )
DynamicElements . Add ( ( EquippableItemPrefabAsset ) ( ( EquippableItemPrefabAssetData ) equippableItemPrefarbAssetData ) . MapDataToPrefabAssetModel ( ) ) ;
2022-06-17 22:22:19 +02:00
}
}
#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 ) ;
2022-11-19 17:02:31 +01:00
EquippableItemPrefabAssetData equippableItemPrefarbAssetData = SaveEquippableItemSystem . LoadEquitableItem ( file . Name , this . MapName + STATIC_ELEMENT + this . ElementFolderName ) ;
2022-06-17 22:22:19 +02:00
2022-11-19 17:02:31 +01:00
StaticElements . Add ( ( EquippableItemPrefabAsset ) ( ( EquippableItemPrefabAssetData ) equippableItemPrefarbAssetData ) . MapDataToPrefabAssetModel ( ) ) ;
2022-06-17 22:22:19 +02:00
}
}
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 ) ;
2022-11-19 17:02:31 +01:00
List < EquippableItemPrefabAssetData > equippableItemPrefarbAssetDataList = SaveEquippableItemSystem . LoadEquitableItemList ( this . MapName + STATIC_ELEMENT , this . ItemsListName ) ;
2022-06-17 22:22:19 +02:00
2022-11-19 17:02:31 +01:00
foreach ( EquippableItemPrefabAssetData equippableItemPrefarbAssetData in equippableItemPrefarbAssetDataList )
StaticElements . Add ( ( EquippableItemPrefabAsset ) ( ( EquippableItemPrefabAssetData ) equippableItemPrefarbAssetData ) . MapDataToPrefabAssetModel ( ) ) ;
2022-06-17 22:22:19 +02:00
}
}
#endregion
}