Skip to content

Commit 403f286

Browse files
committed
Fixed issue where player speed didn’t restore after exiting Fear Pulse Area
1 parent 3767cd6 commit 403f286

1 file changed

Lines changed: 55 additions & 26 deletions

File tree

NewMod/Components/FearPulseArea.cs

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@ namespace NewMod.Components
1313
public class FearPulseArea(IntPtr ptr) : MonoBehaviour(ptr)
1414
{
1515
public byte ownerId;
16-
float _radius, _duration, _speedMul, _t;
16+
17+
float _radius;
18+
float _duration;
19+
float _speedMul;
20+
float _t;
21+
1722
readonly Dictionary<byte, float> _origSpeed = new();
1823
readonly HashSet<byte> _insideNow = new();
24+
1925
public static readonly HashSet<byte> AffectedPlayers = new();
2026
public static readonly HashSet<byte> _speedNotifShown = new();
2127
public static readonly HashSet<byte> _visionNotifShown = new();
28+
2229
public AudioClip _enterClip;
2330
public AudioClip _heartbeatClip;
2431
public bool _pulsingHb;
2532

33+
bool _restored;
34+
2635
public void Init(byte ownerId, float radius, float duration, float speedMul)
2736
{
2837
this.ownerId = ownerId;
@@ -35,6 +44,8 @@ public void Init(byte ownerId, float radius, float duration, float speedMul)
3544

3645
public void Update()
3746
{
47+
if (_restored) return;
48+
3849
_t += Time.deltaTime;
3950
if (_t > _duration)
4051
{
@@ -89,12 +100,13 @@ public void Update()
89100
{
90101
_visionNotifShown.Add(p.PlayerId);
91102
var notif = Helpers.CreateAndShowNotification(
92-
"You have entered the Fear Pulse Area. Your vision is reduced!",
93-
new Color(1f, 0.8f, 0.2f),
94-
spr: NewModAsset.VisionDebuff.LoadAsset()
95-
);
103+
"You have entered the Fear Pulse Area. Your vision is reduced!",
104+
new Color(1f, 0.8f, 0.2f),
105+
spr: NewModAsset.VisionDebuff.LoadAsset()
106+
);
96107
notif.Text.SetOutlineThickness(0.36f);
97108
}
109+
98110
Coroutines.Start(Utils.CoShakeCamera(Camera.main.GetComponent<FollowerCamera>(), 0.5f));
99111
}
100112

@@ -112,40 +124,57 @@ public void Update()
112124
var toRestore = _origSpeed.Keys.Where(id => !_insideNow.Contains(id)).ToList();
113125
foreach (var id in toRestore)
114126
{
115-
var p = Utils.PlayerById(id);
116-
if (p) p.MyPhysics.Speed = _origSpeed[id];
117-
_origSpeed.Remove(id);
127+
RestorePlayer(id);
128+
}
129+
}
130+
}
131+
132+
public void RestorePlayer(byte playerId)
133+
{
134+
if (_origSpeed.TryGetValue(playerId, out var originalSpeed))
135+
{
136+
var p = Utils.PlayerById(playerId);
137+
138+
if (!p.Data.IsDead || !p.Data.Disconnected)
139+
{
140+
p.MyPhysics.Speed = originalSpeed;
118141

119142
if (p.AmOwner)
120143
{
121-
AffectedPlayers.Remove(p.PlayerId);
122144
p.lightSource.lightChild.SetActive(true);
123-
_speedNotifShown.Remove(p.PlayerId);
124-
_visionNotifShown.Remove(p.PlayerId);
125-
Helpers.CreateAndShowNotification("Your vision is restored.", new Color(0.8f, 1f, 0.8f));
145+
146+
Helpers.CreateAndShowNotification(
147+
"Your speed and vision are restored.",
148+
new Color(0.8f, 1f, 0.8f)
149+
);
126150
}
127151
}
152+
153+
_origSpeed.Remove(playerId);
128154
}
155+
156+
AffectedPlayers.Remove(playerId);
157+
_speedNotifShown.Remove(playerId);
158+
_visionNotifShown.Remove(playerId);
129159
}
160+
130161
public void RestoreAll()
131162
{
132-
foreach (var kv in _origSpeed)
163+
if (_restored) return;
164+
_restored = true;
165+
166+
var ids = _origSpeed.Keys.ToList();
167+
foreach (var id in ids)
133168
{
134-
var p = Utils.PlayerById(kv.Key);
135-
if (p) p.MyPhysics.Speed = kv.Value;
169+
RestorePlayer(id);
136170
}
137-
_origSpeed.Clear();
138-
AffectedPlayers.Clear();
139-
140-
var lp = PlayerControl.LocalPlayer;
141-
142-
if (AffectedPlayers.Contains(lp.PlayerId))
143-
AffectedPlayers.Remove(lp.PlayerId);
144171

145-
lp.lightSource.lightChild.SetActive(true);
172+
_insideNow.Clear();
173+
}
146174

147-
_speedNotifShown.Remove(lp.PlayerId);
148-
_visionNotifShown.Remove(lp.PlayerId);
175+
public void OnDestroy()
176+
{
177+
RestoreAll();
149178
}
150179
}
151-
}
180+
}

0 commit comments

Comments
 (0)