您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
105 行
2.6 KiB
105 行
2.6 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
|
|
public class SaveSystem : ScriptableObject
|
|
{
|
|
[SerializeField] private LoadEventChannelSO _loadLocation = default;
|
|
[SerializeField] private InventorySO _playerInventory = default;
|
|
[SerializeField]
|
|
private QuestManagerSO _questManagerSO = default;
|
|
public string saveFilename = "save.chop";
|
|
public string backupSaveFilename = "save.chop.bak";
|
|
public Save saveData = new Save();
|
|
|
|
void OnEnable()
|
|
{
|
|
_loadLocation.OnLoadingRequested += CacheLoadLocations;
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
_loadLocation.OnLoadingRequested -= CacheLoadLocations;
|
|
}
|
|
|
|
private void CacheLoadLocations(GameSceneSO locationToLoad, bool showLoadingScreen, bool fadeScreen)
|
|
{
|
|
LocationSO locationSO = locationToLoad as LocationSO;
|
|
if (locationSO)
|
|
{
|
|
saveData._locationId = locationSO.Guid;
|
|
}
|
|
|
|
SaveDataToDisk();
|
|
}
|
|
|
|
public bool LoadSaveDataFromDisk()
|
|
{
|
|
if (FileManager.LoadFromFile(saveFilename, out var json))
|
|
{
|
|
saveData.LoadFromJson(json);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public IEnumerator LoadSavedInventory()
|
|
{
|
|
_playerInventory.Items.Clear();
|
|
foreach (var serializedItemStack in saveData._itemStacks)
|
|
{
|
|
var loadItemOperationHandle = Addressables.LoadAssetAsync<ItemSO>(serializedItemStack.itemGuid);
|
|
yield return loadItemOperationHandle;
|
|
if (loadItemOperationHandle.Status == AsyncOperationStatus.Succeeded)
|
|
{
|
|
var itemSO = loadItemOperationHandle.Result;
|
|
_playerInventory.Add(itemSO, serializedItemStack.amount);
|
|
}
|
|
}
|
|
}
|
|
public void LoadSavedQuestlineStatus()
|
|
{
|
|
_questManagerSO.SetFinishedQuestlineItemsFromSave(saveData._finishedQuestlineItemsGUIds);
|
|
|
|
}
|
|
|
|
public void SaveDataToDisk()
|
|
{
|
|
saveData._itemStacks.Clear();
|
|
foreach (var itemStack in _playerInventory.Items)
|
|
{
|
|
saveData._itemStacks.Add(new SerializedItemStack(itemStack.Item.Guid, itemStack.Amount));
|
|
}
|
|
saveData._finishedQuestlineItemsGUIds.Clear();
|
|
|
|
foreach (var item in _questManagerSO.GetFinishedQuestlineItemsGUIds())
|
|
{
|
|
saveData._finishedQuestlineItemsGUIds.Add(item);
|
|
|
|
}
|
|
if (FileManager.MoveFile(saveFilename, backupSaveFilename))
|
|
{
|
|
if (FileManager.WriteToFile(saveFilename, saveData.ToJson()))
|
|
{
|
|
Debug.Log("Save successful");
|
|
}
|
|
}
|
|
}
|
|
|
|
public void WriteEmptySaveFile()
|
|
{
|
|
FileManager.WriteToFile(saveFilename, "");
|
|
|
|
}
|
|
public void SetNewGameData()
|
|
{
|
|
FileManager.WriteToFile(saveFilename, "");
|
|
_playerInventory.Init();
|
|
_questManagerSO.ResetQuestlines();
|
|
SaveDataToDisk();
|
|
|
|
}
|
|
}
|