-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameManager.cs
More file actions
250 lines (202 loc) · 6.36 KB
/
GameManager.cs
File metadata and controls
250 lines (202 loc) · 6.36 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class GameManager : MonoBehaviour {
public bool restoreData = false;
public CanvasGroup menu;
public Button loadGameBtn;
public CanvasGroup loading;
public Image loadingImage;
public Text loadingDescription;
public Slider loadingTracker;
private static bool _isLoading = false;
private bool _reloading = false;
private static GameManager instance;
public static GameManager Instance {
get {
if(instance == null) {
instance = GameObject.FindObjectOfType<GameManager>();
}
return instance;
}
}
void Start(){
DontDestroyOnLoad(gameObject);
DisplayLoadingScreen(false);
if(CurrentSceneName == "intro"){
UIManager.DisplayCursor(true);
DisplayStartMenu(true);
} else {
DisplayStartMenu(false);
}
#if UNITY_WEBGL
loadGameBtn.interactable = false;
#endif
}
void OnEnable(){
SceneManager.sceneLoaded += AutoSave;
}
void OnDisable(){
SceneManager.sceneLoaded -= AutoSave;
}
public static bool IsLoading{
get { return _isLoading; }
}
void AutoSave(Scene scene, LoadSceneMode loadMode){
if(!_reloading){
if(scene.name != "intro")
StartCoroutine(SaveAfterNewLevel());
} else {
_reloading = false;
}
}
public void NewGame(){
DisplayStartMenu(false);
LoadScene ("Montreal", "busStation");
}
public void LoadFromMenu(){
LoadFromFile();
DisplayStartMenu(false);
}
public static string CurrentSceneName{
get { return SceneManager.GetActiveScene().name; }
}
public static Scene CurrentScene{
get {return SceneManager.GetActiveScene();}
}
// public static string GetSceneName(int level){
// return SceneManager.GetSceneAt(level).name;
// }
public void Reload(){
_reloading = true;
LoadFromFile();
}
public void LoadFromFile() {
// Debug.Log("LoadFromFile, _isLoading: " + _isLoading);
if(!_isLoading){
_isLoading = true;
if(File.Exists(Application.persistentDataPath + "/playerInfo.dat")){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
Globals.Instance.playerData = (PlayerData)bf.Deserialize(file);
file.Close();
}
LoadScene(Globals.Instance.playerData.levelName);
StartCoroutine(ApplyPlayerData());
}
}
public void LoadScene(string level, string spawn = "Start") {
if(level != "") {
// if(NPCManager.Instance != null)
// NPCManager.Instance.KillNPCs();
if(GameSettings.Instance != null)
GameSettings.Instance._spawn = spawn;
StartCoroutine(LoadAndTrack(level));
}
}
IEnumerator LoadAndTrack(string levelName) {
DisplayLoadingScreen(true);
LoadingScreen newLoadingScreen = GameModel.Instance.GetRandomLoadingScreen();
loadingImage.sprite = newLoadingScreen.screen;
loadingDescription.text = newLoadingScreen.description;
AsyncOperation async = SceneManager.LoadSceneAsync(levelName);
while(!async.isDone){
loadingTracker.value = async.progress;
yield return async;
}
DisplayLoadingScreen(false);
}
IEnumerator SaveAfterNewLevel(){
yield return new WaitForSeconds(1f);
Save();
}
public void Save() {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = Globals.Instance.playerData;
BaseCharacter baseChar = GameSettings.Instance.player.GetComponent<BaseCharacter>();
//Adding non-stats data
data.levelName = SceneManager.GetActiveScene().name;
data.positionX = baseChar.transform.position.x;
data.positionY = baseChar.transform.position.y;
data.positionZ = baseChar.transform.position.z;
data.time = DayNightCycle.Instance.CurrentTime;
data.charKind = baseChar.charKind.ToString();
// data.charOrigin = GameSettings.Instance.player.GetComponent<BaseCharacter>().charOrigin.ToString();
if(baseChar.spectrum1 != null)
data.spectrum1 = baseChar.spectrum1.ToString();
bf.Serialize(file, data);
file.Close();
}
IEnumerator ApplyPlayerData(){
yield return new WaitForSeconds(.5f);
BaseCharacter baseChar = GameSettings.Instance.player.GetComponent<BaseCharacter>();
//Applying data from save to playerGO
baseChar.transform.position = new Vector3(Globals.Instance.playerData.positionX, Globals.Instance.playerData.positionY, Globals.Instance.playerData.positionZ);
switch(Globals.Instance.playerData.charKind){
case "vampire":
baseChar.kind = BaseCharacter.Kind.vampire;
UIManager.EnableBloodUI(true);
break;
case "ghoul":
baseChar.kind = BaseCharacter.Kind.ghoul;
break;
default:
baseChar.kind = BaseCharacter.Kind.human;
UIManager.EnableBloodUI(false);
break;
}
Discipline spectrum;
// Debug.Log(Globals.Instance.playerData.spectrum1Level);
if(Globals.Instance.playerData.spectrum1 != ""){
spectrum = PowerManager.Instance.GetSpectrum(Globals.Instance.playerData.spectrum1);
if(spectrum != null){
baseChar.AddSpectrum(spectrum, Globals.Instance.playerData.spectrum1Level);}
}
if(Globals.Instance.playerData.spectrum2 != ""){
spectrum = PowerManager.Instance.GetSpectrum(Globals.Instance.playerData.spectrum2);
if(spectrum != null){
baseChar.AddSpectrum(spectrum, Globals.Instance.playerData.spectrum2Level);}
}
if(Globals.Instance.playerData.spectrum3 != ""){
spectrum = PowerManager.Instance.GetSpectrum(Globals.Instance.playerData.spectrum3);
if(spectrum != null){
baseChar.AddSpectrum(spectrum, Globals.Instance.playerData.spectrum3Level);}
}
switch(Globals.Instance.playerData.charOrigin){
case "Egyptian":
baseChar.charOrigin = OriginManager.OriginEnum.Egyptian;
break;
case "Greek":
baseChar.charOrigin = OriginManager.OriginEnum.Greek;
break;
case "Hebrew":
baseChar.charOrigin = OriginManager.OriginEnum.Hebrew;
break;
case "Indian":
baseChar.charOrigin = OriginManager.OriginEnum.Indian;
break;
default:
baseChar.charOrigin = OriginManager.OriginEnum.Romanian;
break;
}
// UIManager.Instance.UpdateUI();
_isLoading = false;
}
void DisplayStartMenu(bool on){
if(menu != null){
menu.alpha = on ? 1f : 0f;
menu.interactable = menu.blocksRaycasts = on;
}
}
void DisplayLoadingScreen(bool on){
if(loading != null){
loading.alpha = on ? 1f : 0f;
loading.interactable = loading.blocksRaycasts = on;
}
}
}