From ce358dafcb4fee24ea6fad7d4f5c17b62ed6e228 Mon Sep 17 00:00:00 2001 From: kabix09 Date: Sun, 19 Jun 2022 14:38:22 +0200 Subject: [PATCH] Add inventory saving manager system for user items Fix inventory slots bug --- Assembly-CSharp.csproj | 2 + Assets/Scenes/CaveEntrance.unity | 42 +++++ Assets/Scenes/SampleScene.unity | 42 +++++ Assets/Scripts/DoorBehaviour.cs | 2 +- Assets/Scripts/Inventory/InventoryManager.cs | 24 ++- Assets/Scripts/Item/PickableController.cs | 18 +- Assets/Scripts/SaveController.cs | 5 + .../Scripts/SceneManager/SaveInventory.meta | 8 + .../SaveInventory/SaveInventorySystem.cs | 58 +++++++ .../SaveInventory/SaveInventorySystem.cs.meta | 11 ++ .../SceneManager/SaveTask/SaveQuestSystem.cs | 4 +- .../SceneManager/SceneInventoryManager.cs | 156 ++++++++++++++++++ .../SceneInventoryManager.cs.meta | 11 ++ .../Scripts/SceneManager/SceneTaskManager.cs | 3 + 14 files changed, 373 insertions(+), 13 deletions(-) create mode 100644 Assets/Scripts/SceneManager/SaveInventory.meta create mode 100644 Assets/Scripts/SceneManager/SaveInventory/SaveInventorySystem.cs create mode 100644 Assets/Scripts/SceneManager/SaveInventory/SaveInventorySystem.cs.meta create mode 100644 Assets/Scripts/SceneManager/SceneInventoryManager.cs create mode 100644 Assets/Scripts/SceneManager/SceneInventoryManager.cs.meta diff --git a/Assembly-CSharp.csproj b/Assembly-CSharp.csproj index e65723f5..d0272706 100644 --- a/Assembly-CSharp.csproj +++ b/Assembly-CSharp.csproj @@ -48,6 +48,7 @@ + @@ -62,6 +63,7 @@ + diff --git a/Assets/Scenes/CaveEntrance.unity b/Assets/Scenes/CaveEntrance.unity index 09111251..95b8d1ad 100644 --- a/Assets/Scenes/CaveEntrance.unity +++ b/Assets/Scenes/CaveEntrance.unity @@ -23453,6 +23453,18 @@ MonoBehaviour: m_StringArgument: m_BoolArgument: 0 m_CallState: 2 + - m_Target: {fileID: 105457317} + m_TargetAssemblyTypeName: SaveController, Assembly-CSharp + m_MethodName: SaveInventory + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 --- !u!114 &105457320 MonoBehaviour: m_ObjectHideFlags: 0 @@ -50730,6 +50742,7 @@ GameObject: - component: {fileID: 251828923} - component: {fileID: 251828922} - component: {fileID: 251828924} + - component: {fileID: 251828925} m_Layer: 0 m_Name: LocalSceneManager m_TagString: Untagged @@ -50788,6 +50801,23 @@ MonoBehaviour: MapName: ElementFolderName: Quest ItemsListName: QuestsList +--- !u!114 &251828925 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 251828921} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ef71482e4d2df8440a1117e181018706, type: 3} + m_Name: + m_EditorClassIdentifier: + isNewGame: 1 + isContinued: 0 + MapName: + ElementFolderName: InventoryItem + ItemsListName: InventoryItemList --- !u!1 &253761061 GameObject: m_ObjectHideFlags: 0 @@ -114375,6 +114405,18 @@ MonoBehaviour: m_StringArgument: m_BoolArgument: 0 m_CallState: 2 + - m_Target: {fileID: 933251618} + m_TargetAssemblyTypeName: SaveController, Assembly-CSharp + m_MethodName: SaveInventory + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 --- !u!114 &933251620 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 35290ab4..0dcd6166 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -41129,6 +41129,7 @@ GameObject: - component: {fileID: 805783479} - component: {fileID: 805783478} - component: {fileID: 805783480} + - component: {fileID: 805783481} m_Layer: 0 m_Name: LocalSceneManager m_TagString: Untagged @@ -41187,6 +41188,23 @@ MonoBehaviour: MapName: ElementFolderName: Quest ItemsListName: QuestsList +--- !u!114 &805783481 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 805783477} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ef71482e4d2df8440a1117e181018706, type: 3} + m_Name: + m_EditorClassIdentifier: + isNewGame: 1 + isContinued: 0 + MapName: + ElementFolderName: InventoryItem + ItemsListName: InventoryItemList --- !u!1 &805898524 GameObject: m_ObjectHideFlags: 0 @@ -42228,6 +42246,18 @@ MonoBehaviour: m_StringArgument: m_BoolArgument: 0 m_CallState: 2 + - m_Target: {fileID: 897697754} + m_TargetAssemblyTypeName: SaveController, Assembly-CSharp + m_MethodName: SaveInventory + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 --- !u!114 &897697754 MonoBehaviour: m_ObjectHideFlags: 0 @@ -138710,6 +138740,18 @@ MonoBehaviour: m_StringArgument: m_BoolArgument: 0 m_CallState: 2 + - m_Target: {fileID: 2146696734} + m_TargetAssemblyTypeName: SaveController, Assembly-CSharp + m_MethodName: SaveInventory + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 --- !u!114 &2146696734 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/DoorBehaviour.cs b/Assets/Scripts/DoorBehaviour.cs index 13c79ffb..b236033e 100644 --- a/Assets/Scripts/DoorBehaviour.cs +++ b/Assets/Scripts/DoorBehaviour.cs @@ -37,7 +37,7 @@ public class DoorBehaviour : MonoBehaviour // 1. Save all befor change scene SaveController.GetComponent().SaveItems(); SaveController.GetComponent().SaveQuests(); - + SaveController.GetComponent().SaveInventory(); // 2. Change scene SceneManager.LoadScene(this.SceneName); } diff --git a/Assets/Scripts/Inventory/InventoryManager.cs b/Assets/Scripts/Inventory/InventoryManager.cs index 90d789cc..3326f420 100644 --- a/Assets/Scripts/Inventory/InventoryManager.cs +++ b/Assets/Scripts/Inventory/InventoryManager.cs @@ -33,16 +33,32 @@ public class InventoryManager : BaseWarehouseController } } - public void AddToInventory(EquippableItem pickable) + public int AddToInventory(EquippableItem pickable) { if(this._items.Count <= MAX_ITEMS) { - this._items[this._items.Count] = pickable; - }else { - Debug.Log("Cent add - Inventory is full"); + for(int slotNumber=0; slotNumber + /// Function for placed item in inventory on SPECYFIC position + /// Used by SceneInventoryManager for load saved items on their positions + /// + public void SetupItemInInventory(int key, EquippableItem pickable) + { + this._items[key] = pickable; + } protected override void SetupPanel() { diff --git a/Assets/Scripts/Item/PickableController.cs b/Assets/Scripts/Item/PickableController.cs index 52ae50f6..afc99bd4 100644 --- a/Assets/Scripts/Item/PickableController.cs +++ b/Assets/Scripts/Item/PickableController.cs @@ -39,14 +39,20 @@ public class PickableController : MonoBehaviour { if (Input.GetKeyDown(KeyCode.E)) { - InventoryManager.Instance.AddToInventory(this.item); - isPicked = 1; - //PlayerPrefs.SetInt(name, isPicked); - //gameObject.SetActive(false); + if(InventoryManager.Instance.AddToInventory(this.item) >= 0) + { + isPicked = 1; + //PlayerPrefs.SetInt(name, isPicked); + //gameObject.SetActive(false); - SceneEquippableItemManager.Instance.RemoveDynamicItem(item.name); // remove + SceneEquippableItemManager.Instance.RemoveDynamicItem(item.name); // remove + + Destroy(gameObject); + }else + { + Debug.LogError("Can't pick item - Your inventory is full"); + } - Destroy(gameObject); } } } diff --git a/Assets/Scripts/SaveController.cs b/Assets/Scripts/SaveController.cs index b2dc29bb..2b502261 100644 --- a/Assets/Scripts/SaveController.cs +++ b/Assets/Scripts/SaveController.cs @@ -28,4 +28,9 @@ public class SaveController : MonoBehaviour { SceneTaskManager.Instance.SaveQuests(); } + + public void SaveInventory() + { + SceneInventoryManager.Instance.SaveInventoryItems(); + } } diff --git a/Assets/Scripts/SceneManager/SaveInventory.meta b/Assets/Scripts/SceneManager/SaveInventory.meta new file mode 100644 index 00000000..cf5b1f17 --- /dev/null +++ b/Assets/Scripts/SceneManager/SaveInventory.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1fdd6687dd658574da87271e9677e290 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/SceneManager/SaveInventory/SaveInventorySystem.cs b/Assets/Scripts/SceneManager/SaveInventory/SaveInventorySystem.cs new file mode 100644 index 00000000..5c9a3077 --- /dev/null +++ b/Assets/Scripts/SceneManager/SaveInventory/SaveInventorySystem.cs @@ -0,0 +1,58 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; + +public class SaveInventorySystem : MonoBehaviour +{ + public static void SaveInventoryItemsList(Dictionary equitabbleItemList, string ElementName) + { + BinaryFormatter formatter = new BinaryFormatter(); + // todo: add in scene name folder + string path = SaveSystem.GetSavePath(); +Debug.Log("Saved Inventory at " + path); + if (!Directory.Exists(path)) + Directory.CreateDirectory(path); + + path += "/" + ElementName + ".fun"; + + FileStream stream = new FileStream(path, FileMode.Create); + + Dictionary data = new Dictionary(); + + foreach(KeyValuePair equitabbleItemEntry in equitabbleItemList) + { + data[equitabbleItemEntry.Key] = new EquippableItemPrefarbAssetData(equitabbleItemEntry.Value); + } + + formatter.Serialize(stream, data); + stream.Close(); + } + + private static void Save() + { + + } + + public static Dictionary LoadInventoryItemsList(string ElementName) + { + string path = SaveSystem.GetSavePath() + "/" + ElementName + ".fun"; + + if(File.Exists(path)) + { + + BinaryFormatter formatter = new BinaryFormatter(); + FileStream stream = new FileStream(path, FileMode.Open); + + Dictionary equitabbleItemList = formatter.Deserialize(stream) as Dictionary; + stream.Close(); + + return equitabbleItemList; + } else { + Debug.Log("Save file not found in " + path); + return null; + } + } + +} diff --git a/Assets/Scripts/SceneManager/SaveInventory/SaveInventorySystem.cs.meta b/Assets/Scripts/SceneManager/SaveInventory/SaveInventorySystem.cs.meta new file mode 100644 index 00000000..8465e6b2 --- /dev/null +++ b/Assets/Scripts/SceneManager/SaveInventory/SaveInventorySystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0a7279a76f3009541b3e4b3f71cdddf0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/SceneManager/SaveTask/SaveQuestSystem.cs b/Assets/Scripts/SceneManager/SaveTask/SaveQuestSystem.cs index d57f6b32..bbb6f1d7 100644 --- a/Assets/Scripts/SceneManager/SaveTask/SaveQuestSystem.cs +++ b/Assets/Scripts/SceneManager/SaveTask/SaveQuestSystem.cs @@ -75,10 +75,10 @@ Debug.Log("Saved Quest at " + path); BinaryFormatter formatter = new BinaryFormatter(); FileStream stream = new FileStream(path, FileMode.Open); - List equitabbleItemList = formatter.Deserialize(stream) as List; + List questsList = formatter.Deserialize(stream) as List; stream.Close(); - return equitabbleItemList; + return questsList; } else { Debug.Log("Save file not found in " + path); return null; diff --git a/Assets/Scripts/SceneManager/SceneInventoryManager.cs b/Assets/Scripts/SceneManager/SceneInventoryManager.cs new file mode 100644 index 00000000..05eaf390 --- /dev/null +++ b/Assets/Scripts/SceneManager/SceneInventoryManager.cs @@ -0,0 +1,156 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEditor; +using UnityEngine.SceneManagement; +using System.IO; +using System.Linq; + +public class SceneInventoryManager : MonoBehaviour +{ + private const string DYNAMIC_ELEMENT = "/DynamicElements/"; + private const string STATIC_ELEMENT = "/StaticElements/"; + + [SerializeField] + public Dictionary ItemsElements = new Dictionary(); + + public bool isNewGame = true; + public bool isContinued = false; + public string MapName; + public string ElementFolderName = "InventoryItem"; + public string ItemsListName = "InventoryItemList"; + + public static SceneInventoryManager Instance; + + public void Awake() + { + if(Instance == null) + { + this.MapName = SceneManager.GetActiveScene().name; + + Instance = this; + + + }else if (Instance != this) + { + Destroy(gameObject); + } + } + + public void Start() + { + LoadInventoryItems(); // load user items + + foreach(KeyValuePair ItemsElements in ItemsElements) + { + Debug.Log("Content:" + ItemsElements.Value.equippableItem.Name); + } + BuildInventoryItems(ItemsElements); // pass them to InventoryManager + } + + /// + /// Pass quest to taskManager after load data + /// + public void BuildInventoryItems(Dictionary itemsList) + { + // pass to InventoryManager singleton + foreach(KeyValuePair itemEntry in itemsList) + InventoryManager.Instance.SetupItemInInventory(itemEntry.Key, itemEntry.Value.equippableItem); + } + + public void RemoveInventoryItems(string _name) + { + // 1. Fetch all matched quests - we search by title which shoudl be unique but who know :D + List itemsList = this.ItemsElements.Where(item => item.Value.name == _name).Select(item => item.Key).ToList(); + + // 2. Remove them + foreach(var item in this.ItemsElements.Where(item => item.Value.name == _name).ToList()) + { + this.ItemsElements.Remove(item.Key); + } + } + + /// + /// Use TaskManaager list to save user quests - TaskManager.Instance.taskList + /// + public void SaveInventoryItems() + { + // 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(InventoryManager.Instance._items.Count > 0) { + // 1. Map From Dictionary' to 'Dictionary' + Dictionary mappedList = new Dictionary(); + + foreach(KeyValuePair itemElement in InventoryManager.Instance._items) + { + mappedList[itemElement.Key] = new EquippableItemPrefarbAsset( + itemElement.Value.Name, + itemElement.Value.ItemModel.name, + new Vector3(0,0,0), + (EquippableItem) itemElement.Value + ); + } + + // 2. Save Items + SaveInventorySystem.SaveInventoryItemsList(mappedList, this.ItemsListName); + } else { + string _path = SaveSystem.GetSavePath() + "/" + 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 + } + + /// + /// Load quests to local list + /// Pass this to taskManager in outside method + /// + public void LoadInventoryItems() + { + this.LoadInventoryItemsList(); + } + + public void LoadInventoryItemsList() + { + string path = SaveSystem.GetSavePath(); + + 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; + + Dictionary equippableItemsList = SaveInventorySystem.LoadInventoryItemsList(this.ItemsListName); + + // itemEntry.Value.MapDataToEquippableItem() - map data format to object + + // save object + foreach(KeyValuePair itemEntry in equippableItemsList) + { + InventoryManager.Instance.SetupItemInInventory(itemEntry.Key, itemEntry.Value.MapDataToEquippableItemPrefarbAsset().equippableItem); + } + + } + } +} diff --git a/Assets/Scripts/SceneManager/SceneInventoryManager.cs.meta b/Assets/Scripts/SceneManager/SceneInventoryManager.cs.meta new file mode 100644 index 00000000..c8c42e43 --- /dev/null +++ b/Assets/Scripts/SceneManager/SceneInventoryManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ef71482e4d2df8440a1117e181018706 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/SceneManager/SceneTaskManager.cs b/Assets/Scripts/SceneManager/SceneTaskManager.cs index aa441234..fb237213 100644 --- a/Assets/Scripts/SceneManager/SceneTaskManager.cs +++ b/Assets/Scripts/SceneManager/SceneTaskManager.cs @@ -62,6 +62,9 @@ public class SceneTaskManager : MonoBehaviour return this.TaskElements.Count - 1; } + // NIE UŻYWANA !!! + // TODO - usuwane mają być elementy z listy tasków z Taskmanagera - pozbyć sie tej listy najepiej tu + // TA LISTA SŁUŻY TYLKO DO ODCZYTU Z PLIKÓW I PRZEKAZANIU WARTOŚCI DO MANAGERA - NIR DO KASOWANIA public void RemoveQuest(string _title) { // 1. Fetch all matched quests - we search by title which shoudl be unique but who know :D