Add quest saving manager system for user tasks

Add base auto saving when change scene
This commit is contained in:
kabix09 2022-06-19 02:56:04 +02:00
parent 602c94726a
commit 306c2f85c3
13 changed files with 405 additions and 11 deletions

View File

@ -84,7 +84,7 @@
<Compile Include="Assets\Scripts\FloatValue.cs" />
<Compile Include="Assets\Scripts\SceneManager\SaveItem\SaveEquitableItemSystem.cs" />
<Compile Include="Assets\Scripts\Item\EquippableItem.cs" />
<Compile Include="Assets\Scripts\SceneManager\LocalSceneManager.cs" />
<Compile Include="Assets\Scripts\SceneManager\SceneEquippableItemManager.cs" />
<Compile Include="Assets\PlayerName.cs" />
<Compile Include="Assets\breakable.cs" />
<Compile Include="Assets\Scripts\Task\TaskBox.cs" />

View File

@ -23441,6 +23441,18 @@ MonoBehaviour:
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
- m_Target: {fileID: 105457317}
m_TargetAssemblyTypeName: SaveController, Assembly-CSharp
m_MethodName: SaveQuests
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
@ -50717,6 +50729,7 @@ GameObject:
m_Component:
- component: {fileID: 251828923}
- component: {fileID: 251828922}
- component: {fileID: 251828924}
m_Layer: 0
m_Name: LocalSceneManager
m_TagString: Untagged
@ -50757,6 +50770,24 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 17
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &251828924
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: b8a65f0b147a2e44e8f244301ff0de57, type: 3}
m_Name:
m_EditorClassIdentifier:
TaskElements: []
isNewGame: 1
isContinued: 0
MapName:
ElementFolderName: Quest
ItemsListName: QuestsList
--- !u!1 &253761061
GameObject:
m_ObjectHideFlags: 0
@ -114332,6 +114363,18 @@ MonoBehaviour:
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
- m_Target: {fileID: 933251618}
m_TargetAssemblyTypeName: SaveController, Assembly-CSharp
m_MethodName: SaveQuests
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
@ -126190,6 +126233,7 @@ GameObject:
- component: {fileID: 1193017324}
- component: {fileID: 1193017323}
- component: {fileID: 1193017322}
- component: {fileID: 1193017326}
m_Layer: 0
m_Name: Door
m_TagString: SceneTransition
@ -126210,6 +126254,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
SceneName: SampleScene
SaveController: {fileID: 1193017321}
--- !u!50 &1193017323
Rigidbody2D:
serializedVersion: 4
@ -126271,6 +126316,18 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1193017326
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1193017321}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f8dbef2798cc8064f86e56d2bc02d2fa, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1193056560
GameObject:
m_ObjectHideFlags: 0

View File

@ -1012,6 +1012,7 @@ MonoBehaviour:
_tmpSecondPanel: {fileID: 0}
_tmpPanelAbove: {fileID: 0}
obj: {fileID: 2102416356}
isAllowedToShowPanels: 1
--- !u!58 &132185249
CircleCollider2D:
m_ObjectHideFlags: 0
@ -1579,11 +1580,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 4835b3bfd0c275b4b99ac740c4034acb, type: 3}
m_Name:
m_EditorClassIdentifier:
Panel: {fileID: 626405956}
EPanel: {fileID: 0}
SpacePanel: {fileID: 0}
EPanel1: {fileID: 0}
SpacePanel1: {fileID: 0}
Panels: []
actionPanels:
- E-InteractivePanel
- E-InteractivePanelAbove
- Spacebar-InteractivePanel
- Spacebar-InteractivePanelAbove
--- !u!1 &174581753
GameObject:
m_ObjectHideFlags: 0
@ -7444,6 +7446,7 @@ MonoBehaviour:
_tmpSecondPanel: {fileID: 0}
_tmpPanelAbove: {fileID: 0}
obj: {fileID: 158729764}
isAllowedToShowPanels: 1
--- !u!58 &277639225
CircleCollider2D:
m_ObjectHideFlags: 0
@ -30835,6 +30838,7 @@ MonoBehaviour:
_tmpSecondPanel: {fileID: 0}
_tmpPanelAbove: {fileID: 0}
obj: {fileID: 1027343184}
isAllowedToShowPanels: 1
--- !u!58 &584661740
CircleCollider2D:
m_ObjectHideFlags: 0
@ -41124,6 +41128,7 @@ GameObject:
m_Component:
- component: {fileID: 805783479}
- component: {fileID: 805783478}
- component: {fileID: 805783480}
m_Layer: 0
m_Name: LocalSceneManager
m_TagString: Untagged
@ -41164,6 +41169,24 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 17
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &805783480
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: b8a65f0b147a2e44e8f244301ff0de57, type: 3}
m_Name:
m_EditorClassIdentifier:
TaskElements: []
isNewGame: 1
isContinued: 0
MapName:
ElementFolderName: Quest
ItemsListName: QuestsList
--- !u!1 &805898524
GameObject:
m_ObjectHideFlags: 0
@ -41217,6 +41240,7 @@ MonoBehaviour:
_tmpSecondPanel: {fileID: 0}
_tmpPanelAbove: {fileID: 0}
obj: {fileID: 1265465282}
isAllowedToShowPanels: 1
--- !u!58 &805898527
CircleCollider2D:
m_ObjectHideFlags: 0
@ -41587,6 +41611,7 @@ MonoBehaviour:
_tmpSecondPanel: {fileID: 0}
_tmpPanelAbove: {fileID: 0}
obj: {fileID: 641696201}
isAllowedToShowPanels: 1
--- !u!58 &875629809
CircleCollider2D:
m_ObjectHideFlags: 0
@ -41753,6 +41778,7 @@ MonoBehaviour:
_tmpSecondPanel: {fileID: 0}
_tmpPanelAbove: {fileID: 0}
obj: {fileID: 889215474}
isAllowedToShowPanels: 1
--- !u!58 &887082961
CircleCollider2D:
m_ObjectHideFlags: 0
@ -42190,6 +42216,18 @@ MonoBehaviour:
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
- m_Target: {fileID: 897697754}
m_TargetAssemblyTypeName: SaveController, Assembly-CSharp
m_MethodName: SaveQuests
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
@ -43026,6 +43064,7 @@ MonoBehaviour:
_tmpSecondPanel: {fileID: 0}
_tmpPanelAbove: {fileID: 0}
obj: {fileID: 8620305}
isAllowedToShowPanels: 1
--- !u!58 &1013260100
CircleCollider2D:
m_ObjectHideFlags: 0
@ -43390,6 +43429,7 @@ MonoBehaviour:
_tmpSecondPanel: {fileID: 0}
_tmpPanelAbove: {fileID: 0}
obj: {fileID: 68076400}
isAllowedToShowPanels: 1
--- !u!58 &1102795099
CircleCollider2D:
m_ObjectHideFlags: 0
@ -44032,6 +44072,7 @@ MonoBehaviour:
_tmpSecondPanel: {fileID: 0}
_tmpPanelAbove: {fileID: 0}
obj: {fileID: 242009510}
isAllowedToShowPanels: 1
--- !u!58 &1202265634
CircleCollider2D:
m_ObjectHideFlags: 0
@ -136872,6 +136913,7 @@ MonoBehaviour:
_tmpSecondPanel: {fileID: 0}
_tmpPanelAbove: {fileID: 0}
obj: {fileID: 926459323}
isAllowedToShowPanels: 1
--- !u!58 &1698721849
CircleCollider2D:
m_ObjectHideFlags: 0
@ -137794,6 +137836,7 @@ GameObject:
- component: {fileID: 2020454685}
- component: {fileID: 2020454684}
- component: {fileID: 2020454683}
- component: {fileID: 2020454687}
m_Layer: 0
m_Name: Door
m_TagString: SceneTransition
@ -137814,6 +137857,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
SceneName: CaveEntrance
SaveController: {fileID: 2020454682}
--- !u!50 &2020454684
Rigidbody2D:
serializedVersion: 4
@ -137875,6 +137919,18 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 10
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &2020454687
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2020454682}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f8dbef2798cc8064f86e56d2bc02d2fa, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &2042296513
GameObject:
m_ObjectHideFlags: 0
@ -138318,6 +138374,7 @@ MonoBehaviour:
_tmpSecondPanel: {fileID: 0}
_tmpPanelAbove: {fileID: 0}
obj: {fileID: 682072029}
isAllowedToShowPanels: 1
--- !u!58 &2132075801
CircleCollider2D:
m_ObjectHideFlags: 0
@ -138641,6 +138698,18 @@ MonoBehaviour:
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
- m_Target: {fileID: 2146696734}
m_TargetAssemblyTypeName: SaveController, Assembly-CSharp
m_MethodName: SaveQuests
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

View File

@ -8,6 +8,7 @@ public class DoorBehaviour : MonoBehaviour
public static DoorBehaviour Instance;
public string SceneName = "SampleScene";
public GameObject SaveController;
private void Awake()
{
if(Instance == null)
@ -33,6 +34,11 @@ public class DoorBehaviour : MonoBehaviour
public void ScenetToMoveTo()
{
// 1. Save all befor change scene
SaveController.GetComponent<SaveController>().SaveItems();
SaveController.GetComponent<SaveController>().SaveQuests();
// 2. Change scene
SceneManager.LoadScene(this.SceneName);
}
}

View File

@ -23,4 +23,9 @@ public class SaveController : MonoBehaviour
{
SceneEquippableItemManager.Instance.SaveEquippableItems();
}
public void SaveQuests()
{
SceneTaskManager.Instance.SaveQuests();
}
}

View File

@ -5,8 +5,11 @@ using System.IO;
public static class SaveSystem
{
public static string GetSavePath(string SubFolder)
public static string GetSavePath(string SubFolder = null)
{
return Application.persistentDataPath + "/" + SubFolder;
if(SubFolder != null)
return Application.persistentDataPath + "/" + SubFolder;
return Application.persistentDataPath;
}
}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 58865c060b4d23f41af849da7f95c807
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,92 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveQuestSystem
{
public static void SaveQuestItem(Task quest, string ElementFolderName)
{
BinaryFormatter formatter = new BinaryFormatter();
// todo: add in scene name folder
string path = SaveSystem.GetSavePath(ElementFolderName);
Debug.Log(path);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
path += "/" + quest.title + ".fun";
FileStream stream = new FileStream(path, FileMode.Create);
formatter.Serialize(stream, quest);
stream.Close();
}
public static void SaveQuestsList(List<Task> questsList, string ElementName)
{
BinaryFormatter formatter = new BinaryFormatter();
// todo: add in scene name folder
string path = SaveSystem.GetSavePath();
Debug.Log("Saved Quest at " + path);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
path += "/" + ElementName + ".fun";
FileStream stream = new FileStream(path, FileMode.Create);
formatter.Serialize(stream, questsList);
stream.Close();
}
private static void Save()
{
}
public static Task LoadQuestItem(string questTitle, string ElementFolderName)
{
string path = SaveSystem.GetSavePath(ElementFolderName) + "/" + questTitle;// + ".fun";
if(File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
Task quest = formatter.Deserialize(stream) as Task;
stream.Close();
return quest;
} else {
Debug.Log("Save file not found in " + path);
return null;
}
}
public static List<Task> LoadQuestsList(string ElementName)
{
string path = SaveSystem.GetSavePath() + "/" + ElementName + ".fun";
if(File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
List<Task> equitabbleItemList = formatter.Deserialize(stream) as List<Task>;
stream.Close();
return equitabbleItemList;
} else {
Debug.Log("Save file not found in " + path);
return null;
}
}
private static void Load()
{
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 24fcf11bf60d7c14685586981559ae5f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,132 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
using System.IO;
using System.Linq;
public class SceneTaskManager : MonoBehaviour
{
private const string DYNAMIC_ELEMENT = "/DynamicElements/";
private const string STATIC_ELEMENT = "/StaticElements/";
[SerializeField]
public List<Task> TaskElements;
public bool isNewGame = true;
public bool isContinued = false;
public string MapName;
public string ElementFolderName = "Quest";
public string ItemsListName = "QuestsList";
public static SceneTaskManager Instance;
public void Awake()
{
if(Instance == null)
{
this.MapName = SceneManager.GetActiveScene().name;
Instance = this;
}else if (Instance != this)
{
Destroy(gameObject);
}
}
public void Start()
{
LoadQuests(); // load user quests
BuildQuests(TaskElements); // pass them to TaskManager
}
///<summary>
/// Pass quest to taskManager after load data
///</summary>
public void BuildQuests(List<Task> questList)
{
// pass to taskManager singleton
foreach(Task quest in questList)
TaskManager.Instance.AddTask(quest);
}
public int AddQuest(GameObject dynamicObject)
{
Task quest = dynamicObject.GetComponent<TaskBox>().task;
this.TaskElements.Add(quest);
return this.TaskElements.Count - 1;
}
public void RemoveQuest(string _title)
{
// 1. Fetch all matched quests - we search by title which shoudl be unique but who know :D
List<Task> questList = this.TaskElements.Where(quest => quest.title == _title).ToList();
// 2. Remove them
this.TaskElements.RemoveAll(quest => quest.title == _title);
}
///<summary>
/// Use TaskManaager list to save user quests - TaskManager.Instance.taskList
///</summary>
public void SaveQuests()
{
// 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(TaskManager.Instance.taskList.Count > 0) {
SaveQuestSystem.SaveQuestsList(TaskManager.Instance.taskList, 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 LoadQuests()
{
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;
List<Task> taskList = SaveQuestSystem.LoadQuestsList(this.ItemsListName);
foreach(Task task in taskList)
TaskElements.Add(task);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b8a65f0b147a2e44e8f244301ff0de57
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -14,7 +14,7 @@ public class TaskManager : MonoBehaviour
public bool isOpen = false;
[Header("Tasks list")]
[SerializeField] List<Task> taskList;
[SerializeField] public List<Task> taskList;
private void Awake()
{

View File

@ -30,10 +30,10 @@ EditorUserSettings:
value: 22424703114646680e0b0227036c72111f1958072926337e38271427fb
flags: 0
RecentlyUsedScenePath-8:
value: 22424703114646680e0b0227036c6c111b07142f1f2b233e2867083debf42d
value: 22424703114646680e0b0227036c7c1100123d24383a273e2e2c5326ece92021
flags: 0
RecentlyUsedScenePath-9:
value: 22424703114646680e0b0227036c7c1100123d24383a273e2e2c5326ece92021
value: 22424703114646680e0b0227036c6c111b07142f1f2b233e2867083debf42d
flags: 0
vcSharedLogLevel:
value: 0d5e400f0650