Fix entities getting stuck red (#28981)

This commit is contained in:
metalgearsloth
2024-06-20 17:15:40 +10:00
committed by GitHub
parent 40bcf66fc7
commit d438925d3b
2 changed files with 36 additions and 34 deletions

View File

@@ -2,8 +2,10 @@ using Content.Shared.Effects;
using Robust.Client.Animations; using Robust.Client.Animations;
using Robust.Client.GameObjects; using Robust.Client.GameObjects;
using Robust.Shared.Animations; using Robust.Shared.Animations;
using Robust.Shared.Collections;
using Robust.Shared.Player; using Robust.Shared.Player;
using Robust.Shared.Timing; using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Client.Effects; namespace Content.Client.Effects;
@@ -11,13 +13,13 @@ public sealed class ColorFlashEffectSystem : SharedColorFlashEffectSystem
{ {
[Dependency] private readonly IGameTiming _timing = default!; [Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly AnimationPlayerSystem _animation = default!; [Dependency] private readonly AnimationPlayerSystem _animation = default!;
[Dependency] private readonly IComponentFactory _factory = default!;
/// <summary> /// <summary>
/// It's a little on the long side but given we use multiple colours denoting what happened it makes it easier to register. /// It's a little on the long side but given we use multiple colours denoting what happened it makes it easier to register.
/// </summary> /// </summary>
private const float AnimationLength = 0.30f; private const float AnimationLength = 0.30f;
private const string AnimationKey = "color-flash-effect"; private const string AnimationKey = "color-flash-effect";
private ValueList<EntityUid> _toRemove = new();
public override void Initialize() public override void Initialize()
{ {
@@ -44,8 +46,28 @@ public sealed class ColorFlashEffectSystem : SharedColorFlashEffectSystem
{ {
sprite.Color = component.Color; sprite.Color = component.Color;
} }
}
RemCompDeferred<ColorFlashEffectComponent>(uid); public override void Update(float frameTime)
{
base.Update(frameTime);
var query = AllEntityQuery<ColorFlashEffectComponent>();
_toRemove.Clear();
// Can't use deferred removal on animation completion or it will cause issues.
while (query.MoveNext(out var uid, out _))
{
if (_animation.HasRunningAnimation(uid, AnimationKey))
continue;
_toRemove.Add(uid);
}
foreach (var ent in _toRemove)
{
RemComp<ColorFlashEffectComponent>(ent);
}
} }
private Animation? GetDamageAnimation(EntityUid uid, Color color, SpriteComponent? sprite = null) private Animation? GetDamageAnimation(EntityUid uid, Color color, SpriteComponent? sprite = null)
@@ -82,51 +104,31 @@ public sealed class ColorFlashEffectSystem : SharedColorFlashEffectSystem
{ {
var ent = GetEntity(nent); var ent = GetEntity(nent);
if (Deleted(ent)) if (Deleted(ent) || !TryComp(ent, out SpriteComponent? sprite))
{ {
continue; continue;
} }
if (!TryComp(ent, out AnimationPlayerComponent? player)) #if DEBUG
if (!TryComp(ent, out ColorFlashEffectComponent? comp))
{ {
player = (AnimationPlayerComponent) _factory.GetComponent(typeof(AnimationPlayerComponent)); DebugTools.Assert(!_animation.HasRunningAnimation(ent, AnimationKey));
player.Owner = ent;
player.NetSyncEnabled = false;
AddComp(ent, player);
}
// Need to stop the existing animation first to ensure the sprite color is fixed.
// Otherwise we might lerp to a red colour instead.
if (_animation.HasRunningAnimation(ent, player, AnimationKey))
{
_animation.Stop(ent, player, AnimationKey);
}
if (!TryComp<SpriteComponent>(ent, out var sprite))
{
continue;
}
if (TryComp<ColorFlashEffectComponent>(ent, out var effect))
{
sprite.Color = effect.Color;
} }
#endif
_animation.Stop(ent, AnimationKey);
var animation = GetDamageAnimation(ent, color, sprite); var animation = GetDamageAnimation(ent, color, sprite);
if (animation == null) if (animation == null)
continue;
if (!TryComp(ent, out ColorFlashEffectComponent? comp))
{ {
comp = (ColorFlashEffectComponent) _factory.GetComponent(typeof(ColorFlashEffectComponent)); continue;
comp.Owner = ent;
comp.NetSyncEnabled = false;
AddComp(ent, comp);
} }
comp = EnsureComp<ColorFlashEffectComponent>(ent);
comp.NetSyncEnabled = false;
comp.Color = sprite.Color; comp.Color = sprite.Color;
_animation.Play((ent, player), animation, AnimationKey);
_animation.Play(ent, animation, AnimationKey);
} }
} }
} }

View File

@@ -57,6 +57,6 @@ public sealed class FloatingVisualizerSystem : SharedFloatingVisualizerSystem
if (args.Key != component.AnimationKey) if (args.Key != component.AnimationKey)
return; return;
FloatAnimation(uid, component.Offset, component.AnimationKey, component.AnimationTime, !component.CanFloat); FloatAnimation(uid, component.Offset, component.AnimationKey, component.AnimationTime, stop: !component.CanFloat);
} }
} }