AnomalySystem.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Numerics;
  2. using Content.Client.Gravity;
  3. using Content.Shared.Anomaly;
  4. using Content.Shared.Anomaly.Components;
  5. using Robust.Client.GameObjects;
  6. using Robust.Shared.Timing;
  7. namespace Content.Client.Anomaly;
  8. public sealed class AnomalySystem : SharedAnomalySystem
  9. {
  10. [Dependency] private readonly IGameTiming _timing = default!;
  11. [Dependency] private readonly FloatingVisualizerSystem _floating = default!;
  12. /// <inheritdoc/>
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<AnomalyComponent, AppearanceChangeEvent>(OnAppearanceChanged);
  17. SubscribeLocalEvent<AnomalyComponent, ComponentStartup>(OnStartup);
  18. SubscribeLocalEvent<AnomalyComponent, AnimationCompletedEvent>(OnAnimationComplete);
  19. SubscribeLocalEvent<AnomalySupercriticalComponent, ComponentShutdown>(OnShutdown);
  20. }
  21. private void OnStartup(EntityUid uid, AnomalyComponent component, ComponentStartup args)
  22. {
  23. _floating.FloatAnimation(uid, component.FloatingOffset, component.AnimationKey, component.AnimationTime);
  24. }
  25. private void OnAnimationComplete(EntityUid uid, AnomalyComponent component, AnimationCompletedEvent args)
  26. {
  27. if (args.Key != component.AnimationKey)
  28. return;
  29. _floating.FloatAnimation(uid, component.FloatingOffset, component.AnimationKey, component.AnimationTime);
  30. }
  31. private void OnAppearanceChanged(EntityUid uid, AnomalyComponent component, ref AppearanceChangeEvent args)
  32. {
  33. if (args.Sprite is not { } sprite)
  34. return;
  35. if (!Appearance.TryGetData<bool>(uid, AnomalyVisuals.IsPulsing, out var pulsing, args.Component))
  36. pulsing = false;
  37. if (Appearance.TryGetData<bool>(uid, AnomalyVisuals.Supercritical, out var super, args.Component) && super)
  38. pulsing = super;
  39. if (HasComp<AnomalySupercriticalComponent>(uid))
  40. pulsing = true;
  41. if (!sprite.LayerMapTryGet(AnomalyVisualLayers.Base, out var layer) ||
  42. !sprite.LayerMapTryGet(AnomalyVisualLayers.Animated, out var animatedLayer))
  43. return;
  44. sprite.LayerSetVisible(layer, !pulsing);
  45. sprite.LayerSetVisible(animatedLayer, pulsing);
  46. }
  47. public override void Update(float frameTime)
  48. {
  49. base.Update(frameTime);
  50. var query = EntityQueryEnumerator<AnomalySupercriticalComponent, SpriteComponent>();
  51. while (query.MoveNext(out var super, out var sprite))
  52. {
  53. var completion = 1f - (float) ((super.EndTime - _timing.CurTime) / super.SupercriticalDuration);
  54. var scale = completion * (super.MaxScaleAmount - 1f) + 1f;
  55. sprite.Scale = new Vector2(scale, scale);
  56. var transparency = (byte) (65 * (1f - completion) + 190);
  57. if (transparency < sprite.Color.AByte)
  58. {
  59. sprite.Color = sprite.Color.WithAlpha(transparency);
  60. }
  61. }
  62. }
  63. private void OnShutdown(Entity<AnomalySupercriticalComponent> ent, ref ComponentShutdown args)
  64. {
  65. if (!TryComp<SpriteComponent>(ent, out var sprite))
  66. return;
  67. sprite.Scale = Vector2.One;
  68. sprite.Color = sprite.Color.WithAlpha(1f);
  69. }
  70. }