153 lines
4.7 KiB
C#
153 lines
4.7 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
using OTGIntegrated.Core;
|
||
|
|
using OTGIntegrated.Items;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
namespace OTGIntegrated.Save
|
||
|
|
{
|
||
|
|
public sealed class SaveSystem : MonoBehaviour
|
||
|
|
{
|
||
|
|
private const string SaveKey = "OTG_Lab08_IntegratedRPG_Save";
|
||
|
|
|
||
|
|
public List<ItemData> itemCatalog = new List<ItemData>();
|
||
|
|
|
||
|
|
private GameManager gameManager;
|
||
|
|
|
||
|
|
public void Initialize(GameManager manager)
|
||
|
|
{
|
||
|
|
gameManager = manager;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Save()
|
||
|
|
{
|
||
|
|
if (gameManager == null)
|
||
|
|
{
|
||
|
|
gameManager = GameManager.Instance;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager == null)
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
GameSaveData data = new GameSaveData();
|
||
|
|
|
||
|
|
if (gameManager.playerController != null)
|
||
|
|
{
|
||
|
|
data.playerPosition = gameManager.playerController.transform.position;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.playerStats != null)
|
||
|
|
{
|
||
|
|
data.currentHealth = gameManager.playerStats.CurrentHealth;
|
||
|
|
data.currentMana = gameManager.playerStats.CurrentMana;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.levelSystem != null)
|
||
|
|
{
|
||
|
|
data.level = gameManager.levelSystem.level;
|
||
|
|
data.currentExp = gameManager.levelSystem.currentExp;
|
||
|
|
data.expToNextLevel = gameManager.levelSystem.expToNextLevel;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.talentManager != null)
|
||
|
|
{
|
||
|
|
data.talentPoints = gameManager.talentManager.availableTalentPoints;
|
||
|
|
data.learnedTalentIds = new List<string>(gameManager.talentManager.LearnedTalentIds);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.questManager != null)
|
||
|
|
{
|
||
|
|
data.quests = gameManager.questManager.SaveData();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.inventory != null)
|
||
|
|
{
|
||
|
|
data.inventory = gameManager.inventory.SaveData();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.weaponManager != null)
|
||
|
|
{
|
||
|
|
data.currentWeaponId = gameManager.weaponManager.SaveData();
|
||
|
|
}
|
||
|
|
|
||
|
|
PlayerPrefs.SetString(SaveKey, JsonUtility.ToJson(data));
|
||
|
|
PlayerPrefs.Save();
|
||
|
|
GameEvents.RaiseNotification("Прогресс сохранён");
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Load()
|
||
|
|
{
|
||
|
|
if (!PlayerPrefs.HasKey(SaveKey))
|
||
|
|
{
|
||
|
|
GameEvents.RaiseNotification("Сохранение не найдено");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager == null)
|
||
|
|
{
|
||
|
|
gameManager = GameManager.Instance;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager == null)
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
GameSaveData data = JsonUtility.FromJson<GameSaveData>(PlayerPrefs.GetString(SaveKey));
|
||
|
|
if (data == null)
|
||
|
|
{
|
||
|
|
GameEvents.RaiseNotification("Сохранение повреждено");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.playerController != null)
|
||
|
|
{
|
||
|
|
CharacterController controller = gameManager.playerController.GetComponent<CharacterController>();
|
||
|
|
if (controller != null) controller.enabled = false;
|
||
|
|
gameManager.playerController.transform.position = data.playerPosition;
|
||
|
|
if (controller != null) controller.enabled = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.levelSystem != null)
|
||
|
|
{
|
||
|
|
gameManager.levelSystem.SetProgress(data.level, data.currentExp, data.expToNextLevel);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.inventory != null)
|
||
|
|
{
|
||
|
|
gameManager.inventory.LoadData(data.inventory, itemCatalog);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.talentManager != null)
|
||
|
|
{
|
||
|
|
gameManager.talentManager.LoadProgress(data.talentPoints, data.learnedTalentIds);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.questManager != null)
|
||
|
|
{
|
||
|
|
gameManager.questManager.LoadData(data.quests);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.weaponManager != null)
|
||
|
|
{
|
||
|
|
gameManager.weaponManager.LoadData(data.currentWeaponId);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (gameManager.playerStats != null)
|
||
|
|
{
|
||
|
|
gameManager.playerStats.SetRuntimeVitals(data.currentHealth, data.currentMana);
|
||
|
|
}
|
||
|
|
|
||
|
|
GameEvents.RaiseNotification("Прогресс загружен");
|
||
|
|
}
|
||
|
|
|
||
|
|
public void ResetSave()
|
||
|
|
{
|
||
|
|
PlayerPrefs.DeleteKey(SaveKey);
|
||
|
|
PlayerPrefs.Save();
|
||
|
|
GameEvents.RaiseNotification("Сохранение сброшено");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|