-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add slowdown to low temperatures (#29692)
* TemperatureSpeed component * temperature slowdown prediction (done right) * remove unnecessary changes * that too * get in line * make it readonly * auto pause
- Loading branch information
1 parent
50408da
commit b3a64d4
Showing
9 changed files
with
143 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
Content.Shared/Temperature/Components/TemperatureSpeedComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Content.Shared.Temperature.Systems; | ||
using Robust.Shared.GameStates; | ||
|
||
namespace Content.Shared.Temperature.Components; | ||
|
||
/// <summary> | ||
/// This is used for an entity that varies in speed based on current temperature. | ||
/// </summary> | ||
[RegisterComponent, NetworkedComponent, Access(typeof(SharedTemperatureSystem)), AutoGenerateComponentState, AutoGenerateComponentPause] | ||
public sealed partial class TemperatureSpeedComponent : Component | ||
{ | ||
/// <summary> | ||
/// Pairs of temperature thresholds to applied slowdown values. | ||
/// </summary> | ||
[DataField] | ||
public Dictionary<float, float> Thresholds = new(); | ||
|
||
/// <summary> | ||
/// The current speed modifier from <see cref="Thresholds"/> we reached. | ||
/// Stored and networked so that the client doesn't mispredict temperature | ||
/// </summary> | ||
[DataField, AutoNetworkedField] | ||
public float? CurrentSpeedModifier; | ||
|
||
/// <summary> | ||
/// The time at which the temperature slowdown is updated. | ||
/// </summary> | ||
[DataField, AutoNetworkedField, AutoPausedField] | ||
public TimeSpan? NextSlowdownUpdate; | ||
} |
80 changes: 80 additions & 0 deletions
80
Content.Shared/Temperature/Systems/SharedTemperatureSystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Linq; | ||
using Content.Shared.Movement.Components; | ||
using Content.Shared.Movement.Systems; | ||
using Content.Shared.Temperature.Components; | ||
using Robust.Shared.Timing; | ||
|
||
namespace Content.Shared.Temperature.Systems; | ||
|
||
/// <summary> | ||
/// This handles predicting temperature based speedup. | ||
/// </summary> | ||
public sealed class SharedTemperatureSystem : EntitySystem | ||
{ | ||
[Dependency] private readonly IGameTiming _timing = default!; | ||
[Dependency] private readonly MovementSpeedModifierSystem _movementSpeedModifier = default!; | ||
|
||
/// <summary> | ||
/// Band-aid for unpredicted atmos. Delays the application for a short period so that laggy clients can get the replicated temperature. | ||
/// </summary> | ||
private static readonly TimeSpan SlowdownApplicationDelay = TimeSpan.FromSeconds(1f); | ||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
|
||
SubscribeLocalEvent<TemperatureSpeedComponent, OnTemperatureChangeEvent>(OnTemperatureChanged); | ||
SubscribeLocalEvent<TemperatureSpeedComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovementSpeedModifiers); | ||
} | ||
|
||
private void OnTemperatureChanged(Entity<TemperatureSpeedComponent> ent, ref OnTemperatureChangeEvent args) | ||
{ | ||
foreach (var (threshold, modifier) in ent.Comp.Thresholds) | ||
{ | ||
if (args.CurrentTemperature < threshold && args.LastTemperature > threshold || | ||
args.CurrentTemperature > threshold && args.LastTemperature < threshold) | ||
{ | ||
ent.Comp.NextSlowdownUpdate = _timing.CurTime + SlowdownApplicationDelay; | ||
ent.Comp.CurrentSpeedModifier = modifier; | ||
Dirty(ent); | ||
break; | ||
} | ||
} | ||
|
||
var maxThreshold = ent.Comp.Thresholds.Max(p => p.Key); | ||
if (args.CurrentTemperature > maxThreshold && args.LastTemperature < maxThreshold) | ||
{ | ||
ent.Comp.NextSlowdownUpdate = _timing.CurTime + SlowdownApplicationDelay; | ||
ent.Comp.CurrentSpeedModifier = null; | ||
Dirty(ent); | ||
} | ||
} | ||
|
||
private void OnRefreshMovementSpeedModifiers(Entity<TemperatureSpeedComponent> ent, ref RefreshMovementSpeedModifiersEvent args) | ||
{ | ||
// Don't update speed and mispredict while we're compensating for lag. | ||
if (ent.Comp.NextSlowdownUpdate != null || ent.Comp.CurrentSpeedModifier == null) | ||
return; | ||
|
||
args.ModifySpeed(ent.Comp.CurrentSpeedModifier.Value, ent.Comp.CurrentSpeedModifier.Value); | ||
} | ||
|
||
public override void Update(float frameTime) | ||
{ | ||
base.Update(frameTime); | ||
|
||
var query = EntityQueryEnumerator<TemperatureSpeedComponent, MovementSpeedModifierComponent>(); | ||
while (query.MoveNext(out var uid, out var temp, out var movement)) | ||
{ | ||
if (temp.NextSlowdownUpdate == null) | ||
continue; | ||
|
||
if (_timing.CurTime < temp.NextSlowdownUpdate) | ||
continue; | ||
|
||
temp.NextSlowdownUpdate = null; | ||
_movementSpeedModifier.RefreshMovementSpeedModifiers(uid, movement); | ||
Dirty(uid, temp); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters