Skip to content

Commit e08993d

Browse files
Added Events
Added events for when level is completed and for getting the levels current playtime.
1 parent 1952319 commit e08993d

6 files changed

Lines changed: 135 additions & 14 deletions

File tree

‎Class1.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BepInEx;
22
using BepInEx.Unity.IL2CPP;
3+
using HarmonyLib;
34
using UnityEngine;
45

56
namespace DeadshotModAPI;
@@ -11,9 +12,13 @@ namespace DeadshotModAPI;
1112
)]
1213
public class Plugin : BasePlugin
1314
{
15+
private static Harmony _harmony;
16+
1417
public override void Load()
1518
{
1619
Debug.Log("Deadshot Mod API loaded.");
20+
_harmony = new Harmony("DeadshotModAPI");
21+
_harmony.PatchAll();
1722

1823
AddComponent<ModLoader>();
1924
}

‎DeadshotModAPI.csproj‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,53 @@
2121
</Reference>
2222

2323
<Reference Include="Il2Cppmscorlib">
24-
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\libIl2Cppmscorlib.dll</HintPath>
24+
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\lib\Il2Cppmscorlib.dll</HintPath>
2525
</Reference>
2626

2727
<Reference Include="Assembly-CSharp">
28-
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\libAssembly-CSharp.dll</HintPath>
28+
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\lib\Assembly-CSharp.dll</HintPath>
2929
</Reference>
3030

3131
<Reference Include="UnityEngine.CoreModule">
32-
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\libUnityEngine.CoreModule.dll</HintPath>
32+
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\lib\UnityEngine.CoreModule.dll</HintPath>
3333
</Reference>
3434

3535
<Reference Include="Unity.InputSystem">
36-
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\libUnity.InputSystem.dll</HintPath>
36+
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\lib\Unity.InputSystem.dll</HintPath>
3737
</Reference>
3838

3939
<Reference Include="Unity.TextMeshPro">
40-
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\libUnity.TextMeshPro.dll</HintPath>
40+
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\lib\Unity.TextMeshPro.dll</HintPath>
4141
</Reference>
4242

4343
<Reference Include="UnityEngine.UI">
44-
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\libUnityEngine.UI.dll</HintPath>
44+
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\lib\UnityEngine.UI.dll</HintPath>
4545
</Reference>
4646

4747
<Reference Include="UnityEngine.AudioModule">
48-
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\libUnityEngine.AudioModule.dll</HintPath>
48+
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\lib\UnityEngine.AudioModule.dll</HintPath>
4949
</Reference>
5050

5151
<Reference Include="UnityEngine.DirectorModule">
52-
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\libUnityEngine.DirectorModule.dll</HintPath>
52+
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\lib\UnityEngine.DirectorModule.dll</HintPath>
5353
</Reference>
5454

5555
<Reference Include="Deadshot.Data">
56-
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\libDeadshot.Data.dll</HintPath>
56+
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\lib\Deadshot.Data.dll</HintPath>
5757
</Reference>
5858

5959
<Reference Include="UnityEngine.AssetBundleModule">
60-
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\libUnityEngine.AssetBundleModule.dll</HintPath>
60+
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\lib\UnityEngine.AssetBundleModule.dll</HintPath>
6161
</Reference>
6262

6363
<Reference Include="Il2CppSystem">
64-
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\libIl2CppSystem.dll</HintPath>
64+
<HintPath>C:\Code\DeadshotMods\DeadshotModAPI\lib\Il2CppSystem.dll</HintPath>
6565
</Reference>
6666

6767
</ItemGroup>
6868

69+
<ItemGroup>
70+
<PackageReference Include="Lib.Harmony" Version="2.4.2" />
71+
</ItemGroup>
72+
6973
</Project>

‎Events/EventManager.cs‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

‎Events/LevelCompleteScreen.cs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class LevelCompleteScreen
2+
{
3+
private readonly Deadshot.UI.Menus.LevelCompleteMenu _menu;
4+
5+
internal LevelCompleteScreen(
6+
Deadshot.UI.Menus.LevelCompleteMenu menu)
7+
{
8+
_menu = menu;
9+
}
10+
11+
public void SetTime(string text)
12+
{
13+
_menu.timeText.text = $"Time: {text}";
14+
}
15+
16+
// TODO: Add the rest
17+
}

‎GameManager/GameManager.cs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using UnityEngine;
21
using UnitySceneManager = UnityEngine.SceneManagement;
32
using Deadshot.Data;
4-
using Deadshot.Weapons;
53
using DeadshotGameManager = Deadshot.GameManager;
64
using System.IO;
75

‎Runtime/ModLoader.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public void Start()
3939
/// </summary>
4040
public void Update()
4141
{
42-
Input.CheckKeys();
42+
Input.CheckKeys(); // Checks keyboard inputs
43+
EventManager.Update(); // Handles updating events
4344
}
4445

4546
/// <summary>

0 commit comments

Comments
 (0)