-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveManager.cs
More file actions
169 lines (141 loc) · 4.02 KB
/
SaveManager.cs
File metadata and controls
169 lines (141 loc) · 4.02 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Threading.Tasks;
using UnityEngine;
public class SaveManager : MonoBehaviour
{
[SerializeField] private string saveDirectoryName = "MyGameSaves";
private IDataOperationService dataOps;
// Public properties for easy access
public PlayerData PlayerData => dataOps?.GetDataPack<PlayerDataPack>()?.Data;
public WorldData WorldData => dataOps?.GetDataPack<WorldDataPack>()?.Data;
public SettingsData SettingsData => dataOps?.GetDataPack<SettingsDataPack>()?.Data;
void Awake()
{
InitializeSaveSystem();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
SaveGameData();
}
if (Input.GetKeyDown(KeyCode.D))
{
LoadGameData();
}
if (Input.GetKeyDown(KeyCode.S))
{
LevelUp();
}
if (Input.GetKeyDown(KeyCode.F))
{
SetWorldFlag("flag1", true);
}
if (Input.GetKeyDown(KeyCode.G))
{
SetWorldFlag("flag2", false);
}
}
private async void InitializeSaveSystem()
{
// Initialize the save system
var dataService = new FileDataService(saveDirectoryName);
dataOps = new DataOperationService(dataService);
// Load all data on start
await LoadGameDataAsync();
Debug.Log("Save system initialized!");
}
void OnApplicationPause(bool pauseStatus)
{
if (pauseStatus)
{
// Save when app goes to background
SaveGameData();
}
}
void OnApplicationFocus(bool hasFocus)
{
if (!hasFocus)
{
// Save when app loses focus
SaveGameData();
}
}
private async Task LoadGameDataAsync()
{
await dataOps.LoadAsync();
if (dataOps.IsAllLoaded())
{
Debug.Log("All data loaded successfully!");
Debug.Log($"Player level: {PlayerData.Level}");
Debug.Log($"Completed quests: {WorldData.CompletedQuests.Count}");
}
else
{
Debug.LogWarning("Some data packs failed to load, using defaults");
}
}
private async Task SaveGameDataAsync()
{
await dataOps.SaveAsync();
Debug.Log("Game saved successfully!");
}
// Public methods for UI and game systems
public void SaveGameData()
{
dataOps.Save();
Debug.Log("Game saved!");
}
public void LoadGameData()
{
dataOps.Load();
Debug.Log("Game loaded!");
}
public void SaveSettings()
{
dataOps.Save<SettingsDataPack>();
Debug.Log("Settings saved!");
}
public void ResetPlayerData()
{
dataOps.Reset<PlayerDataPack>();
Debug.Log("Player data reset!");
}
public void ResetAllData()
{
dataOps.ResetAll();
Debug.Log("All data reset!");
}
// Example usage methods
public void LevelUp()
{
if (PlayerData != null)
{
PlayerData.Level++;
PlayerData.Experience = 0;
// Save player data immediately
dataOps.Save<PlayerDataPack>();
Debug.Log($"Player leveled up to {PlayerData.Level}!");
}
}
public void CompleteQuest(string questId)
{
if (WorldData != null && !WorldData.CompletedQuests.Contains(questId))
{
WorldData.CompletedQuests.Add(questId);
// Save world data
dataOps.Save<WorldDataPack>();
Debug.Log($"Quest {questId} completed!");
}
}
public void SetWorldFlag(string flagName, bool value)
{
if (WorldData != null)
{
WorldData.WorldFlags[flagName] = value;
// Save world data
dataOps.Save<WorldDataPack>();
Debug.Log($"World flag {flagName} set to {value}");
}
}
}