1+ using System ;
2+ using DeadshotGameManager = Deadshot . GameManager ;
3+ using Deadshot . UI . Menus ;
4+ using HarmonyLib ;
5+
6+ namespace DeadshotModAPI ;
7+
8+ public static class EventManager
9+ {
10+ /// <summary>
11+ /// Updates all events that require per-frame processing.
12+ /// </summary>
13+ public static void Update ( )
14+ {
15+ LevelEvent . Update ( ) ;
16+ }
17+
18+ public static class LevelEvent
19+ {
20+ private static float _lastLevelTime ;
21+
22+ /// <summary>
23+ /// Occurs whenever the current level playtime changes.
24+ /// </summary>
25+ public static event Action < float > LevelTimeChanged ;
26+
27+ /// <summary>
28+ /// Occurs when the level completion menu is enabled.
29+ /// </summary>
30+ public static event Action < LevelCompleteScreen > LevelCompleted ;
31+
32+ /// <summary>
33+ /// Updates level-related events that require per-frame processing.
34+ /// </summary>
35+ internal static void Update ( )
36+ {
37+ LevelPlaytimeEvent ( ) ;
38+ }
39+
40+ /// <summary>
41+ /// Checks the current level playtime and invokes
42+ /// <see cref="LevelTimeChanged"/> when the playtime has changed.
43+ /// </summary>
44+ internal static void LevelPlaytimeEvent ( )
45+ {
46+ if ( DeadshotGameManager . INSTANCE == null )
47+ return ;
48+
49+ float levelTime = DeadshotGameManager . INSTANCE . CompletionTime ;
50+
51+ if ( levelTime == _lastLevelTime )
52+ return ;
53+
54+ _lastLevelTime = levelTime ;
55+
56+ LevelTimeChanged ? . Invoke ( levelTime ) ;
57+ }
58+
59+ /// <summary>
60+ /// Gets the current levels playtime.
61+ /// </summary>
62+ /// <returns>The playtime as a float.</returns>
63+ public static float GetLevelPlaytime ( )
64+ {
65+ return DeadshotGameManager . INSTANCE . CompletionTime ;
66+ }
67+
68+ /// <summary>
69+ /// Invokes <see cref="LevelCompleted"/> when the game's
70+ /// level completion menu is enabled.
71+ /// </summary>
72+ internal static void LevelCompletedEvent ( LevelCompleteScreen menu )
73+ {
74+ LevelCompleted ? . Invoke ( menu ) ;
75+ }
76+ }
77+
78+ /// <summary>
79+ /// Harmony patches used by the event manager.
80+ /// </summary>
81+ [ HarmonyPatch ( typeof ( LevelCompleteMenu ) ) ]
82+ private static class LevelCompleteMenuPatch
83+ {
84+ /// <summary>
85+ /// Detects when the level completion menu is enabled.
86+ /// </summary>
87+ [ HarmonyPostfix ]
88+ [ HarmonyPatch ( nameof ( LevelCompleteMenu . OnEnable ) ) ]
89+ private static void OnEnable ( LevelCompleteMenu __instance )
90+ {
91+ var screen = new LevelCompleteScreen ( __instance ) ;
92+
93+ LevelEvent . LevelCompletedEvent ( screen ) ;
94+ }
95+ }
96+ }
0 commit comments