70 lines
1.9 KiB
C#
70 lines
1.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using Managers;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
[Serializable]
|
|
public class GameState
|
|
{
|
|
public PlayerData playerData;
|
|
}
|
|
|
|
public class SaveManager : MonoBehaviour
|
|
{
|
|
public GameState gameState;
|
|
public PlayerData defaultPlayerData;
|
|
|
|
private void Awake()
|
|
{
|
|
gameState = new GameState();
|
|
}
|
|
|
|
public void SaveGame()
|
|
{
|
|
gameState.playerData.currentMap = MapManager.Instance.currentMap.name;
|
|
gameState.playerData.currentPosition = PlayerManager.Instance.transform.position;
|
|
|
|
var json = JsonUtility.ToJson(gameState);
|
|
File.WriteAllText(Application.persistentDataPath + "/gameState.json", json);
|
|
}
|
|
|
|
public void LoadGame(GameState gs)
|
|
{
|
|
gameState = gs;
|
|
PlayerManager.Instance.LoadPlayerData(gameState.playerData);
|
|
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
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
// set slot to random item
|
|
var item = GameItemManager.Instance.gameItems[
|
|
Random.Range(0, GameItemManager.Instance.gameItems.Count - 1)];
|
|
gameState.playerData.AddItemToInventory(item);
|
|
}
|
|
|
|
LoadGame(gameState);
|
|
}
|
|
} |