103 lines
3.0 KiB
C#
103 lines
3.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using Data.ScriptableObjects;
|
|
using Types;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
namespace Managers
|
|
{
|
|
[Serializable]
|
|
public class GameState
|
|
{
|
|
public GamePlayer PlayerData;
|
|
}
|
|
|
|
public class SaveManager : MonoBehaviour
|
|
{
|
|
public static SaveManager Instance;
|
|
public GameState gameState;
|
|
public ItemDatabase itemDatabase;
|
|
public GamePlayer DefaultPlayerData;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
gameState = new GameState();
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
public void SaveGame(PlayerManager playerManager)
|
|
{
|
|
gameState.PlayerData.CurrentMap = MapManager.Instance.currentMapSo.name;
|
|
// gameState.playerData.currentPosition = playerManager.transform.position;
|
|
|
|
var json = JsonUtility.ToJson(gameState);
|
|
File.WriteAllText(Application.persistentDataPath + "/gameState.json", json);
|
|
}
|
|
|
|
public void LoadGame(GameState gs)
|
|
{
|
|
gameState = gs;
|
|
// playerManager.Initialize(gameState.playerData);
|
|
// todo: enable map
|
|
// MapManager.Instance.GetMap(gameState.playerData.currentMap);
|
|
|
|
/*var path = Application.persistentDataPath + "/gameState.json";
|
|
if (File.Exists(path))
|
|
{
|
|
var json = File.ReadAllText(path);
|
|
gameState = JsonUtility.FromJson<GameState>(json);
|
|
}*/
|
|
}
|
|
|
|
public void LoadGame()
|
|
{
|
|
var path = Application.persistentDataPath + "/gameState.json";
|
|
if (File.Exists(path))
|
|
{
|
|
var json = File.ReadAllText(path);
|
|
gameState = JsonUtility.FromJson<GameState>(json);
|
|
}
|
|
}
|
|
|
|
public void NewGame()
|
|
{
|
|
gameState.PlayerData = DefaultPlayerData;
|
|
// add items to inventory
|
|
|
|
if (itemDatabase == null)
|
|
itemDatabase = Resources.Load<ItemDatabase>("GameItems");
|
|
|
|
if (itemDatabase == null)
|
|
{
|
|
Debug.LogError("ItemDatabase is null");
|
|
//TODO: remove?
|
|
LoadGame(gameState);
|
|
return;
|
|
}
|
|
|
|
if (itemDatabase.AllItems.Count == 0)
|
|
itemDatabase.LoadAllItems();
|
|
|
|
Debug.Log("Item count: " + itemDatabase.AllItems.Count);
|
|
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
// set slot to random item
|
|
var item = itemDatabase.AllItems[
|
|
Random.Range(0, itemDatabase.AllItems.Count - 1)];
|
|
gameState.PlayerData.AddItemToInventory(item);
|
|
}
|
|
|
|
LoadGame(gameState);
|
|
}
|
|
}
|
|
} |