Replace Speech bubble time accumulator with TimeSpan (#38241)

* Replace SpeechBubble time accumulator with TimeSpan

* CurTime -> RealTime
This commit is contained in:
Tayrtahn
2025-06-10 20:25:43 -04:00
committed by GitHub
parent 640ec3e031
commit 4a34759514

View File

@@ -14,6 +14,7 @@ namespace Content.Client.Chat.UI
{ {
public abstract class SpeechBubble : Control public abstract class SpeechBubble : Control
{ {
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IEyeManager _eyeManager = default!; [Dependency] private readonly IEyeManager _eyeManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!; [Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] protected readonly IConfigurationManager ConfigManager = default!; [Dependency] protected readonly IConfigurationManager ConfigManager = default!;
@@ -30,12 +31,12 @@ namespace Content.Client.Chat.UI
/// <summary> /// <summary>
/// The total time a speech bubble stays on screen. /// The total time a speech bubble stays on screen.
/// </summary> /// </summary>
private const float TotalTime = 4; private static readonly TimeSpan TotalTime = TimeSpan.FromSeconds(4);
/// <summary> /// <summary>
/// The amount of time at the end of the bubble's life at which it starts fading. /// The amount of time at the end of the bubble's life at which it starts fading.
/// </summary> /// </summary>
private const float FadeTime = 0.25f; private static readonly TimeSpan FadeTime = TimeSpan.FromSeconds(0.25f);
/// <summary> /// <summary>
/// The distance in world space to offset the speech bubble from the center of the entity. /// The distance in world space to offset the speech bubble from the center of the entity.
@@ -50,7 +51,10 @@ namespace Content.Client.Chat.UI
private readonly EntityUid _senderEntity; private readonly EntityUid _senderEntity;
private float _timeLeft = TotalTime; /// <summary>
/// The time at which this bubble will die.
/// </summary>
private TimeSpan _deathTime;
public float VerticalOffset { get; set; } public float VerticalOffset { get; set; }
private float _verticalOffsetAchieved; private float _verticalOffsetAchieved;
@@ -99,6 +103,7 @@ namespace Content.Client.Chat.UI
bubble.Measure(Vector2Helpers.Infinity); bubble.Measure(Vector2Helpers.Infinity);
ContentSize = bubble.DesiredSize; ContentSize = bubble.DesiredSize;
_verticalOffsetAchieved = -ContentSize.Y; _verticalOffsetAchieved = -ContentSize.Y;
_deathTime = _timing.RealTime + TotalTime;
} }
protected abstract Control BuildBubble(ChatMessage message, string speechStyleClass, Color? fontColor = null); protected abstract Control BuildBubble(ChatMessage message, string speechStyleClass, Color? fontColor = null);
@@ -107,8 +112,8 @@ namespace Content.Client.Chat.UI
{ {
base.FrameUpdate(args); base.FrameUpdate(args);
_timeLeft -= args.DeltaSeconds; var timeLeft = (float)(_deathTime - _timing.RealTime).TotalSeconds;
if (_entityManager.Deleted(_senderEntity) || _timeLeft <= 0) if (_entityManager.Deleted(_senderEntity) || timeLeft <= 0)
{ {
// Timer spawn to prevent concurrent modification exception. // Timer spawn to prevent concurrent modification exception.
Timer.Spawn(0, Die); Timer.Spawn(0, Die);
@@ -131,10 +136,10 @@ namespace Content.Client.Chat.UI
return; return;
} }
if (_timeLeft <= FadeTime) if (timeLeft <= FadeTime.TotalSeconds)
{ {
// Update alpha if we're fading. // Update alpha if we're fading.
Modulate = Color.White.WithAlpha(_timeLeft / FadeTime); Modulate = Color.White.WithAlpha(timeLeft / (float)FadeTime.TotalSeconds);
} }
else else
{ {
@@ -144,7 +149,7 @@ namespace Content.Client.Chat.UI
var baseOffset = 0f; var baseOffset = 0f;
if (_entityManager.TryGetComponent<SpeechComponent>(_senderEntity, out var speech)) if (_entityManager.TryGetComponent<SpeechComponent>(_senderEntity, out var speech))
baseOffset = speech.SpeechBubbleOffset; baseOffset = speech.SpeechBubbleOffset;
var offset = (-_eyeManager.CurrentEye.Rotation).ToWorldVec() * -(EntityVerticalOffset + baseOffset); var offset = (-_eyeManager.CurrentEye.Rotation).ToWorldVec() * -(EntityVerticalOffset + baseOffset);
@@ -175,9 +180,9 @@ namespace Content.Client.Chat.UI
/// </summary> /// </summary>
public void FadeNow() public void FadeNow()
{ {
if (_timeLeft > FadeTime) if (_deathTime > _timing.RealTime)
{ {
_timeLeft = FadeTime; _deathTime = _timing.RealTime + FadeTime;
} }
} }