-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameManager.cs
337 lines (287 loc) · 9.94 KB
/
GameManager.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System;
using System.IO;
using CHARK.GameManagement.Assets;
using CHARK.GameManagement.Entities;
using CHARK.GameManagement.Messaging;
using CHARK.GameManagement.Serialization;
using CHARK.GameManagement.Settings;
using CHARK.GameManagement.Storage;
using CHARK.GameManagement.Systems;
using CHARK.GameManagement.Utilities;
using UnityEngine;
namespace CHARK.GameManagement
{
[DefaultExecutionOrder(-1)]
public abstract partial class GameManager : MonoBehaviour
{
private static GameManager currentGameManager;
#if UNITY_EDITOR
private static IStorage currentEditorStorage;
#endif
private static IStorage EditorStorage
{
get
{
#if UNITY_EDITOR
if (currentEditorStorage != default)
{
return currentEditorStorage;
}
var projectAssetDirPath = Application.dataPath;
var projectDirPath = Path.GetDirectoryName(projectAssetDirPath);
// Project dir is used as id for editor keys, useful when using Parallel sync or similar.
var projectId = string.IsNullOrWhiteSpace(projectAssetDirPath)
? Application.productName
: Path.GetFileName(projectDirPath);
currentEditorStorage = new EditorPrefsStorage(
serializer: DefaultSerializer.Instance,
pathPrefix: $"{nameof(GameManager)}.{projectId}."
);
return currentEditorStorage;
#else
return NullStorage.Instance;
#endif
}
}
private static GameManagerSettings Settings => GameManagerSettings.Instance;
private static bool isDebuggingEnabled;
private bool isSystemsInitialized;
private ISerializer serializer;
private IStorage runtimeStorage;
private IResourceLoader resourceLoader;
private IEntityManager entityManager;
private IMessageBus messageBus;
private void Awake()
{
if (currentGameManager && currentGameManager != this)
{
var gameManagerTypeName = GetType().Name;
Logging.LogWarning($"{gameManagerTypeName} is already initialized", this);
Destroy(gameObject);
return;
}
#if UNITY_EDITOR
if (TryReadEditorData(nameof(IsDebuggingEnabled), out bool value))
{
isDebuggingEnabled = value;
}
#else
isDebuggingEnabled = Debug.isDebugBuild;
#endif
var isInitialized = currentGameManager == true;
currentGameManager = this;
if (isInitialized == false)
{
InitializeGameManager();
var profile = Settings.ActiveProfile;
if (profile.IsVerboseLogging)
{
Logging.LogDebug($"{GetGameManagerName()} initialized", this);
}
if (profile.IsDontDestroyOnLoad)
{
DontDestroyOnLoad(gameObject);
}
}
}
private void FixedUpdate()
{
NotifyFixedUpdateListeners();
}
private void Update()
{
NotifyUpdateListeners();
}
private void OnDestroy()
{
OnBeforeDestroy();
if (entityManager != null)
{
var systems = entityManager.GetEntities<ISystem>();
foreach (var system in systems)
{
RemoveSystem(system);
}
}
var profile = Settings.ActiveProfile;
if (profile.IsVerboseLogging)
{
Logging.LogDebug($"{GetGameManagerName()} disposed", this);
}
}
/// <summary>
/// Called when systems are about to initialize and should be added to the game.
/// </summary>
protected virtual void OnBeforeInitializeSystems()
{
}
/// <summary>
/// Called when all systems are initialized.
/// </summary>
protected virtual void OnAfterInitializeSystems()
{
}
/// <summary>
/// Called the game manager is about to be destroyed.
/// </summary>
protected virtual void OnBeforeDestroy()
{
}
/// <returns>
/// Name of this game manager.
/// </returns>
protected virtual string GetGameManagerName()
{
return GetType().Name;
}
/// <summary>
/// Add <paramref name="system"/> to <see cref="entityManager"/>.
/// </summary>
protected void AddSystem(ISystem system)
{
if (entityManager.AddEntity(system) && isSystemsInitialized)
{
system.OnInitialized();
}
}
/// <summary>
/// Remove <paramref name="system"/> from <see cref="entityManager"/>.
/// </summary>
protected void RemoveSystem(ISystem system)
{
if (entityManager.RemoveEntity(system))
{
system.OnDisposed();
}
}
/// <returns>
/// New <see cref="ISerializer"/> instance.
/// </returns>
protected virtual ISerializer CreateSerializer()
{
return DefaultSerializer.Instance;
}
/// <returns>
/// New <see cref="IStorage"/> instance.
/// </returns>
protected virtual IStorage CreateRuntimeStorage()
{
return new DefaultFileStorage(
serializer: serializer,
profile: Settings.ActiveProfile,
persistentDataPath: Application.persistentDataPath,
pathPrefix: "Data" + Path.DirectorySeparatorChar
);
}
/// <returns>
/// New <see cref="IResourceLoader"/> instance.
/// </returns>
protected virtual IResourceLoader CreateResourceLoader()
{
return new DefaultResourceLoader(serializer);
}
/// <returns>
/// New <see cref="IEntityManager"/> instance.
/// </returns>
protected virtual IEntityManager CreateEntityManager()
{
var profile = Settings.ActiveProfile;
return new DefaultEntityManager(profile);
}
/// <returns>
/// New <see cref="IMessageBus"/> instance.
/// </returns>
protected virtual IMessageBus CreateMessageBus()
{
return new DefaultMessageBus();
}
private void InitializeGameManager()
{
InitializeCore();
OnBeforeInitializeSystems();
InitializeSystems();
OnAfterInitializeSystems();
}
private void InitializeCore()
{
name = GetGameManagerName();
serializer = CreateSerializer();
runtimeStorage = CreateRuntimeStorage();
resourceLoader = CreateResourceLoader();
entityManager = CreateEntityManager();
messageBus = CreateMessageBus();
}
private void InitializeSystems()
{
var entities = entityManager.Entities;
// ReSharper disable once ForCanBeConvertedToForeach
for (var index = 0; index < entities.Count; index++)
{
var entity = entities[index];
if (!(entity is ISystem system))
{
continue;
}
var profile = Settings.ActiveProfile;
if (profile.IsVerboseLogging)
{
var systemType = entity.GetType();
var systemName = systemType.Name;
Logging.LogDebug($"Initializing system {systemName}", this);
}
system.OnInitialized();
}
isSystemsInitialized = true;
}
private static GameManager GetGameManager()
{
#if UNITY_EDITOR
if (GameManagerUtilities.IsApplicationQuitting)
{
// If application is quitting, we don't care about warnings.
return currentGameManager;
}
if (currentGameManager == false)
{
throw new Exception(
$"{nameof(GameManager)} is not initialized."
+ $" Did you forget to add it to one of your scenes?"
+ $" If you're using automatic instantiation "
+ $" ({nameof(GameManagerSettingsProfile.IsInstantiateAutomatically)}),"
+ $" make sure the {nameof(GameManagerSettings)} contains a valid and active"
+ $" profile with a set {nameof(GameManager)} prefab."
);
}
#endif
return currentGameManager;
}
private void NotifyFixedUpdateListeners()
{
var deltaTime = Time.deltaTime;
var entities = entityManager.Entities;
// ReSharper disable once ForCanBeConvertedToForeach
for (var index = 0; index < entities.Count; index++)
{
var entity = entities[index];
if (entity is IFixedUpdateListener fixedUpdateListener)
{
fixedUpdateListener.OnFixedUpdated(deltaTime);
}
}
}
private void NotifyUpdateListeners()
{
var deltaTime = Time.deltaTime;
var entities = entityManager.Entities;
// ReSharper disable once ForCanBeConvertedToForeach
for (var index = 0; index < entities.Count; index++)
{
var entity = entities[index];
if (entity is IUpdateListener updateListener)
{
updateListener.OnUpdated(deltaTime);
}
}
}
}
}