-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathPrecursorKeyTerminalTrigger_OnTriggerExit_Patch.cs
More file actions
61 lines (51 loc) · 2 KB
/
PrecursorKeyTerminalTrigger_OnTriggerExit_Patch.cs
File metadata and controls
61 lines (51 loc) · 2 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
using System.Collections.Generic;
using System.Reflection;
using NitroxClient.GameLogic.PlayerLogic;
using UnityEngine;
namespace NitroxPatcher.Patches.Dynamic;
/// <summary>
/// Prevents precursor key terminal pedestals from closing when a player leaves if other players are still nearby.
/// </summary>
public sealed partial class PrecursorKeyTerminalTrigger_OnTriggerExit_Patch : NitroxPatch, IDynamicPatch
{
private static readonly MethodInfo TARGET_METHOD = Reflect.Method((PrecursorKeyTerminalTrigger t) => t.OnTriggerExit(default));
/// <summary>
/// Tracks the number of players currently in each trigger zone.
/// </summary>
private static readonly Dictionary<int, int> playerCountByTrigger = [];
public static void IncrementPlayerCount(PrecursorKeyTerminalTrigger trigger)
{
int id = trigger.GetInstanceID();
playerCountByTrigger.TryGetValue(id, out int count);
playerCountByTrigger[id] = count + 1;
}
public static int GetPlayerCount(PrecursorKeyTerminalTrigger trigger)
{
int id = trigger.GetInstanceID();
playerCountByTrigger.TryGetValue(id, out int count);
return count;
}
public static bool Prefix(PrecursorKeyTerminalTrigger __instance, Collider col)
{
bool isLocalPlayer = col.gameObject.Equals(Player.main.gameObject);
bool isRemotePlayer = col.GetComponentInParent<RemotePlayerIdentifier>() != null;
if (!isLocalPlayer && !isRemotePlayer)
{
return false;
}
// Decrement player count (only if we have a count for this trigger)
int id = __instance.GetInstanceID();
int newCount = 0;
if (playerCountByTrigger.TryGetValue(id, out int count) && count > 0)
{
newCount = count - 1;
playerCountByTrigger[id] = newCount;
}
// Only close if no players remain
if (newCount <= 0)
{
__instance.SendMessageUpwards("CloseDeck");
}
return false;
}
}