-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameEventsBroadcaster.cs
463 lines (383 loc) · 10.9 KB
/
GameEventsBroadcaster.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
using System;
using System.Collections.Generic;
namespace Net.Xeophin.Utils
{
/// <summary>
/// Class serves as a template for a notification hub that can be used within the
/// game to trigger specific events.
/// </summary>
///
/// \version 0.2.0
/// \author Kaspar Manz <[email protected]>
/// \date 2014-07-03
///
///
/// \version 0.3.0
/// \author Kaspar Manz <[email protected]>
/// \date 2014-09-26
/// <remarks>Redesigned class to use a dictionary for collecting all possible
/// event handlers, so I don't need to use that massive switch statement.
/// </remarks>
public class GameEventsBroadcaster<T>:IGameEventProvider where T : GameEventsBroadcaster<T>, new()
{
#region Singleton
static readonly T instance = new T ();
protected GameEventsBroadcaster ()
{
// Setup the game state dictionary with all possible values
var allStates = (GameState[])Enum.GetValues (typeof(GameState));
foreach (var item in allStates)
gameStateEvents.Add (item, null);
}
public static T Instance { get { return instance; } }
#endregion
/// <summary>
/// The game state events dictionary
/// - this collects all event handlers for the complete enum list.
/// </summary>
Dictionary<GameState,EventHandler<GameStateEventArgs>> gameStateEvents = new Dictionary<GameState, EventHandler<GameStateEventArgs>> ();
#region Game State Changes
/// <summary>
/// Occurs when a general game state change occurs. Catch-all event, use when you
/// look for several events at once or are not really sure what you're
/// looking for.
/// </summary>
public event EventHandler<GameStateEventArgs> GameStateChanged;
protected virtual void OnGameStateChanged (object sender, GameStateEventArgs e)
{
var handler = GameStateChanged;
if (handler != null)
handler (this, e);
}
/// <summary>
/// Raises the game state changed event. This is the main entry point for all
/// game state event changes.
/// </summary>
/// <param name="sender">Sender object.</param>
/// <param name="e">The event arguments.</param>
/// <remarks>
/// This handles all game state changes, so you can just use one function to
/// call all the events.
///
/// This method will then call more specific event invocators, so that objects
/// dependent on those specific events can listen to those, and don't have
/// to check whether they have to do something themselves.
/// </remarks>
public void RaiseGameStateChanged (object sender, GameStateEventArgs e)
{
// General Game state event.
OnGameStateChanged (sender, e);
// Call more specific events
var handler = gameStateEvents [e.NewState];
if (handler != null)
handler (sender, e);
}
#endregion
#region Pausing the Game
public event EventHandler<GameStateEventArgs> PauseGame {
add {
lock (gameStateEvents) {
gameStateEvents [GameState.EnterPause] += value;
}
}
remove {
lock (gameStateEvents) {
gameStateEvents [GameState.EnterPause] -= value;
}
}
}
public event EventHandler<GameStateEventArgs> UnpauseGame {
add {
lock (gameStateEvents) {
gameStateEvents [GameState.ExitPause] += value;
}
}
remove {
lock (gameStateEvents) {
gameStateEvents [GameState.ExitPause] -= value;
}
}
}
#endregion
#region Control over the Game
public event EventHandler<GameStateEventArgs> PlayerReceivesControl {
add {
lock (gameStateEvents) {
gameStateEvents [GameState.PlayerReceivesControl] += value;
}
}
remove {
lock (gameStateEvents) {
gameStateEvents [GameState.PlayerReceivesControl] -= value;
}
}
}
public event EventHandler<GameStateEventArgs> PlayerCedesControl {
add {
lock (gameStateEvents) {
gameStateEvents [GameState.PlayerCedesControl] += value;
}
}
remove {
lock (gameStateEvents) {
gameStateEvents [GameState.PlayerCedesControl] -= value;
}
}
}
#endregion
#region Setup Complete
public event EventHandler<GameStateEventArgs> SetupComplete {
add {
lock (gameStateEvents) {
gameStateEvents [GameState.SetupCompleted] += value;
}
}
remove {
lock (gameStateEvents) {
gameStateEvents [GameState.SetupCompleted] -= value;
}
}
}
#endregion
#region Shutdown
public event EventHandler<GameStateEventArgs> PrepareShutdown {
add {
lock (gameStateEvents) {
gameStateEvents [GameState.PrepareShutdown] += value;
}
}
remove {
lock (gameStateEvents) {
gameStateEvents [GameState.PrepareShutdown] -= value;
}
}
}
#endregion
#region Level Loading
public event EventHandler<GameStateEventArgs> LevelLoadingStarts {
add {
Add (GameState.LevelLoadingStarts, value);
}
remove {
Remove (GameState.LevelLoadingStarts, value);
}
}
public event EventHandler<GameStateEventArgs> LevelLoadingComplete {
add {
Add (GameState.LevelLoadingComplete, value);
}
remove {
Remove (GameState.LevelLoadingComplete, value);
}
}
#endregion
#region Timer
public event EventHandler<GameStateEventArgs> TimerStarted {
add {
Add (GameState.TimerStarted, value);
}
remove {
Remove (GameState.TimerStarted, value);
}
}
public event EventHandler<GameStateEventArgs> TimerCompleted {
add {
Add (GameState.TimerCompleted, value);
}
remove {
Remove (GameState.TimerCompleted, value);
}
}
#endregion
#region Fades
public event EventHandler<GameStateEventArgs> FadeOutComplete {
add {
lock (gameStateEvents) {
gameStateEvents [GameState.FadeOutComplete] += value;
}
}
remove {
lock (gameStateEvents) {
gameStateEvents [GameState.FadeOutComplete] -= value;
}
}
}
public event EventHandler<GameStateEventArgs> ReadyForFadeOut {
add {
lock (gameStateEvents) {
gameStateEvents [GameState.ReadyForFadeOut] += value;
}
}
remove {
lock (gameStateEvents) {
gameStateEvents [GameState.ReadyForFadeOut] -= value;
}
}
}
public event EventHandler<GameStateEventArgs> FadeInComplete {
add {
lock (gameStateEvents) {
gameStateEvents [GameState.FadeInComplete] += value;
}
}
remove {
lock (gameStateEvents) {
gameStateEvents [GameState.FadeInComplete] -= value;
}
}
}
public event EventHandler<GameStateEventArgs> ReadyForFadeIn {
add {
lock (gameStateEvents) {
gameStateEvents [GameState.ReadyForFadeIn] += value;
}
}
remove {
lock (gameStateEvents) {
gameStateEvents [GameState.ReadyForFadeIn] -= value;
}
}
}
#endregion
//TODO More events should come here.
#region Helper classes
void Add (GameState state, EventHandler<GameStateEventArgs> handler)
{
lock (gameStateEvents) {
gameStateEvents [state] += handler;
}
}
void Remove (GameState state, EventHandler<GameStateEventArgs> handler)
{
lock (gameStateEvents) {
gameStateEvents [state] -= handler;
}
}
#endregion
}
public class GameStateEventArgs : EventArgs
{
public readonly GameState NewState;
public readonly GameState OldState;
public readonly object OriginalSender;
public GameStateEventArgs (object originalSender)
{
this.OriginalSender = originalSender;
}
public GameStateEventArgs (GameState newState, object originalSender)
{
this.NewState = newState;
this.OriginalSender = originalSender;
}
public GameStateEventArgs (GameState newState)
{
this.NewState = newState;
}
public GameStateEventArgs (GameState newState, GameState oldState)
{
this.NewState = newState;
this.OldState = oldState;
}
}
/// <summary>
/// Various states the game can be in.
/// </summary>
public enum GameState
{
/// <summary>
/// Default (empty) state.
/// </summary>
Init,
/// <summary>
/// The game loading complete.
/// </summary>
GameLoadingComplete,
/// <summary>
/// Game entered a menu state.
/// </summary>
EnterMenu,
/// <summary>
/// Game exited from a menu state.
/// </summary>
ExitMenu,
/// <summary>
/// Game entered a level scene.
/// </summary>
EnterLevel,
/// <summary>
/// Game exited from a level scene.
/// </summary>
ExitLevel,
/// <summary>
/// Begin transition to pause.
/// </summary>
EnterPause,
/// <summary>
/// Begin transition to end pause.
/// </summary>
ExitPause,
/// <summary>
/// Scene has finished loading, ready to be faded in.
/// </summary>
ReadyForFadeIn,
/// <summary>
/// Scene has finished fading in.
/// </summary>
FadeInComplete,
/// <summary>
/// Scene is ready to be faded out.
/// </summary>
ReadyForFadeOut,
/// <summary>
/// Scene has faded out.
/// </summary>
FadeOutComplete,
/// <summary>
/// Level loading starts.
/// </summary>
LevelLoadingStarts,
/// <summary>
/// Level loading complete.
/// </summary>
LevelLoadingComplete,
/// <summary>
/// The player receives control over the game.
/// </summary>
PlayerReceivesControl,
/// <summary>
/// The player cedes control over the game.
/// </summary>
///<remarks>
/// This can also mean a "Pause" or menu screen, when the player
/// actually still has control over what happens.
/// </remarks>
PlayerCedesControl,
/// <summary>
/// The setup of the game has been completed, the settings should
/// now be finalised and actual gameplay will begin shortly.
/// </summary>
SetupCompleted,
/// <summary>
/// The player has indicated that they want to quit the game. All
/// data should be cleaned up, saved to the appropriate places and
/// the game prepared for shutdown.
/// </summary>
PrepareShutdown,
/// <summary>
/// Execute the actual shutdown - gives another hook to do stuff when shutting down.
/// </summary>
Shutdown,
/// <summary>
/// The timer started.
/// </summary>
TimerStarted,
/// <summary>
/// A timer has run out of time.
/// </summary>
TimerCompleted
}
public interface IGameEventProvider
{
event EventHandler<GameStateEventArgs> PrepareShutdown;
}
}