155 lines
4.5 KiB
C#
155 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using Data.ScriptableObjects;
|
|
using Types;
|
|
using UnityEngine;
|
|
using Utils;
|
|
using Random = UnityEngine.Random;
|
|
|
|
namespace Managers
|
|
{
|
|
[Serializable]
|
|
public class SaveManager : MonoBehaviour
|
|
{
|
|
public static SaveManager Instance;
|
|
public ItemDatabase itemDatabase;
|
|
|
|
public GamePlayerSo defaultPlayerData;
|
|
|
|
public string currentSaveID;
|
|
|
|
public GameState GameState;
|
|
public List<Save> Saves = new();
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
GameState = new GameState();
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
Saves = GetSaves();
|
|
}
|
|
|
|
public void SaveGame()
|
|
{
|
|
GameState.Players[0].CurrentMap = MapManager.Instance.currentMapSo.name;
|
|
// gameState.playerData.currentPosition = playerManager.transform.position;
|
|
|
|
var save = new Save
|
|
{
|
|
GameState = GameState,
|
|
ID = currentSaveID,
|
|
Name = GameState.Players[0].PlayerName
|
|
};
|
|
|
|
var json = JsonUtility.ToJson(save);
|
|
File.WriteAllText(Application.persistentDataPath + "/save/" + save.ID + ".json", json);
|
|
}
|
|
|
|
public void LoadGame(Save save)
|
|
{
|
|
currentSaveID = save.ID;
|
|
GameState = save.GameState;
|
|
// 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()
|
|
{
|
|
// create new instance of player data
|
|
var newPlayerData = new GamePlayer(defaultPlayerData);
|
|
GameState.Players.Add(0, newPlayerData);
|
|
// add items to inventory
|
|
|
|
|
|
if (itemDatabase == null)
|
|
itemDatabase = Resources.Load<ItemDatabase>("GameItems");
|
|
|
|
if (itemDatabase == null)
|
|
{
|
|
GameLogger.LogError("ItemDatabase is null");
|
|
//TODO: remove?
|
|
return;
|
|
}
|
|
|
|
if (itemDatabase.AllItems.Count == 0)
|
|
itemDatabase.LoadAllItems();
|
|
|
|
GameLogger.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.Players[0].AddItemToInventory(item);
|
|
}
|
|
|
|
var guid = Guid.NewGuid();
|
|
var save = new Save
|
|
{
|
|
GameState = GameState,
|
|
ID = guid.ToString(),
|
|
Name = GameState.Players[0].PlayerName
|
|
};
|
|
|
|
|
|
LoadGame(save);
|
|
}
|
|
|
|
public List<Save> GetSaves()
|
|
{
|
|
// if saves is not empty return it
|
|
if (Saves.Count > 0) return Saves;
|
|
|
|
var saves = new List<Save>();
|
|
var path = Application.persistentDataPath + "/saves";
|
|
if (Directory.Exists(path))
|
|
{
|
|
var files = Directory.GetFiles(path);
|
|
foreach (var file in files)
|
|
{
|
|
var save = JsonUtility.FromJson<Save>(File.ReadAllText(file));
|
|
saves.Add(save);
|
|
}
|
|
}
|
|
|
|
return saves;
|
|
}
|
|
|
|
public Save GetSave(string id)
|
|
{
|
|
var saves = GetSaves();
|
|
foreach (var save in saves)
|
|
if (save.ID == id)
|
|
return save;
|
|
return null;
|
|
}
|
|
}
|
|
} |