-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveManager.cs
48 lines (37 loc) · 1.46 KB
/
SaveManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.IO;
using System.Text.Json;
namespace Generic_Text_Based_RPG_Epic_Edition
{
public class SaveManager
{
public static JsonSerializerOptions serializeOptions = new()
{
Converters = { new ItemConverter(), new EnemyConverter() },
WriteIndented = true
};
public static SaveVarStorage SaveVarStorage(SaveVarStorage saveVarStorage)
{
saveVarStorage.Player = Program.CurrentPlayer;
saveVarStorage.Enemy = Program.CurrentEnemy;
return saveVarStorage;
}
public static void LoadVarStorage(SaveVarStorage saveVarStorage)
{
Program.CurrentPlayer = saveVarStorage.Player;
Program.CurrentEnemy = saveVarStorage.Enemy;
}
public static void SaveGame(SaveVarStorage saveVarStorage)
{
saveVarStorage = SaveVarStorage(saveVarStorage);
File.WriteAllText(Program.FullPath + Path.DirectorySeparatorChar + "save.json", JsonSerializer.Serialize(saveVarStorage, serializeOptions));
}
public static SaveVarStorage LoadGame()
{
FileStream save = File.OpenRead(Program.FullPath + Path.DirectorySeparatorChar + "save.json");
SaveVarStorage saveVarStorage = JsonSerializer.Deserialize<SaveVarStorage>(save, serializeOptions);
LoadVarStorage(saveVarStorage);
save.Close();
return saveVarStorage;
}
}
}