Add inventory saving manager system for user items
Fix inventory slots bug
This commit is contained in:
parent
306c2f85c3
commit
ce358dafcb
@ -48,6 +48,7 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Assets\Scripts\RespawnScript.cs" />
|
||||
<Compile Include="Assets\Scripts\SceneManager\SceneTaskManager.cs" />
|
||||
<Compile Include="Assets\Scripts\SettingsButton.cs" />
|
||||
<Compile Include="Assets\Scripts\UI\ISlot.cs" />
|
||||
<Compile Include="Assets\Scripts\Enemies' Scprits\Enemy.cs" />
|
||||
@ -62,6 +63,7 @@
|
||||
<Compile Include="Assets\NewGame.cs" />
|
||||
<Compile Include="Assets\Scripts\Knockback.cs" />
|
||||
<Compile Include="Assets\Scripts\Inventory\InventoryManager.cs" />
|
||||
<Compile Include="Assets\Scripts\SceneManager\SaveTask\SaveQuestSystem.cs" />
|
||||
<Compile Include="Assets\Scripts\Dialogue.cs" />
|
||||
<Compile Include="Assets\Scripts\SceneManager\SaveItem\ItemData\ItemData.cs" />
|
||||
<Compile Include="Assets\Scripts\Item\ItemSlot.cs" />
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -37,7 +37,7 @@ public class DoorBehaviour : MonoBehaviour
|
||||
// 1. Save all befor change scene
|
||||
SaveController.GetComponent<SaveController>().SaveItems();
|
||||
SaveController.GetComponent<SaveController>().SaveQuests();
|
||||
|
||||
SaveController.GetComponent<SaveController>().SaveInventory();
|
||||
// 2. Change scene
|
||||
SceneManager.LoadScene(this.SceneName);
|
||||
}
|
||||
|
@ -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<MAX_ITEMS; slotNumber++)
|
||||
{
|
||||
if(!this._items.ContainsKey(slotNumber))
|
||||
{
|
||||
this._items[slotNumber] = pickable;
|
||||
return slotNumber;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
///<summary>
|
||||
/// Function for placed item in inventory on SPECYFIC position
|
||||
/// Used by SceneInventoryManager for load saved items on their positions
|
||||
///</summary>
|
||||
public void SetupItemInInventory(int key, EquippableItem pickable)
|
||||
{
|
||||
this._items[key] = pickable;
|
||||
}
|
||||
|
||||
protected override void SetupPanel()
|
||||
{
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,4 +28,9 @@ public class SaveController : MonoBehaviour
|
||||
{
|
||||
SceneTaskManager.Instance.SaveQuests();
|
||||
}
|
||||
|
||||
public void SaveInventory()
|
||||
{
|
||||
SceneInventoryManager.Instance.SaveInventoryItems();
|
||||
}
|
||||
}
|
||||
|
8
Assets/Scripts/SceneManager/SaveInventory.meta
Normal file
8
Assets/Scripts/SceneManager/SaveInventory.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1fdd6687dd658574da87271e9677e290
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -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<int, EquippableItemPrefarbAsset> 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<int, EquippableItemPrefarbAssetData> data = new Dictionary<int, EquippableItemPrefarbAssetData>();
|
||||
|
||||
foreach(KeyValuePair<int, EquippableItemPrefarbAsset> equitabbleItemEntry in equitabbleItemList)
|
||||
{
|
||||
data[equitabbleItemEntry.Key] = new EquippableItemPrefarbAssetData(equitabbleItemEntry.Value);
|
||||
}
|
||||
|
||||
formatter.Serialize(stream, data);
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
private static void Save()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static Dictionary<int, EquippableItemPrefarbAssetData> 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<int, EquippableItemPrefarbAssetData> equitabbleItemList = formatter.Deserialize(stream) as Dictionary<int, EquippableItemPrefarbAssetData>;
|
||||
stream.Close();
|
||||
|
||||
return equitabbleItemList;
|
||||
} else {
|
||||
Debug.Log("Save file not found in " + path);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a7279a76f3009541b3e4b3f71cdddf0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -75,10 +75,10 @@ Debug.Log("Saved Quest at " + path);
|
||||
BinaryFormatter formatter = new BinaryFormatter();
|
||||
FileStream stream = new FileStream(path, FileMode.Open);
|
||||
|
||||
List<Task> equitabbleItemList = formatter.Deserialize(stream) as List<Task>;
|
||||
List<Task> questsList = formatter.Deserialize(stream) as List<Task>;
|
||||
stream.Close();
|
||||
|
||||
return equitabbleItemList;
|
||||
return questsList;
|
||||
} else {
|
||||
Debug.Log("Save file not found in " + path);
|
||||
return null;
|
||||
|
156
Assets/Scripts/SceneManager/SceneInventoryManager.cs
Normal file
156
Assets/Scripts/SceneManager/SceneInventoryManager.cs
Normal file
@ -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<int, EquippableItemPrefarbAsset> ItemsElements = new Dictionary<int, EquippableItemPrefarbAsset>();
|
||||
|
||||
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<int, EquippableItemPrefarbAsset> ItemsElements in ItemsElements)
|
||||
{
|
||||
Debug.Log("Content:" + ItemsElements.Value.equippableItem.Name);
|
||||
}
|
||||
BuildInventoryItems(ItemsElements); // pass them to InventoryManager
|
||||
}
|
||||
|
||||
///<summary>
|
||||
/// Pass quest to taskManager after load data
|
||||
///</summary>
|
||||
public void BuildInventoryItems(Dictionary<int, EquippableItemPrefarbAsset> itemsList)
|
||||
{
|
||||
// pass to InventoryManager singleton
|
||||
foreach(KeyValuePair<int, EquippableItemPrefarbAsset> 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<int> 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);
|
||||
}
|
||||
}
|
||||
|
||||
///<summary>
|
||||
/// Use TaskManaager list to save user quests - TaskManager.Instance.taskList
|
||||
///</summary>
|
||||
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<int, Item>' to 'Dictionary<int, EquippableItemPrefarbAsset>'
|
||||
Dictionary<int, EquippableItemPrefarbAsset> mappedList = new Dictionary<int, EquippableItemPrefarbAsset>();
|
||||
|
||||
foreach(KeyValuePair<int, Item> 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
|
||||
}
|
||||
|
||||
///<summary>
|
||||
/// Load quests to local list
|
||||
/// Pass this to taskManager in outside method
|
||||
///</summary>
|
||||
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<int, EquippableItemPrefarbAssetData> equippableItemsList = SaveInventorySystem.LoadInventoryItemsList(this.ItemsListName);
|
||||
|
||||
// itemEntry.Value.MapDataToEquippableItem() - map data format to object
|
||||
|
||||
// save object
|
||||
foreach(KeyValuePair<int, EquippableItemPrefarbAssetData> itemEntry in equippableItemsList)
|
||||
{
|
||||
InventoryManager.Instance.SetupItemInInventory(itemEntry.Key, itemEntry.Value.MapDataToEquippableItemPrefarbAsset().equippableItem);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/SceneManager/SceneInventoryManager.cs.meta
Normal file
11
Assets/Scripts/SceneManager/SceneInventoryManager.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ef71482e4d2df8440a1117e181018706
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user