1
0

FultonSystem.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Numerics;
  2. using Content.Shared.Salvage.Fulton;
  3. using Robust.Shared.Spawners;
  4. using JetBrains.Annotations;
  5. using Robust.Client.Animations;
  6. using Robust.Client.GameObjects;
  7. using Robust.Client.Graphics;
  8. using Robust.Shared.Animations;
  9. using Robust.Shared.Serialization.Manager;
  10. using Robust.Shared.Utility;
  11. using TimedDespawnComponent = Robust.Shared.Spawners.TimedDespawnComponent;
  12. namespace Content.Client.Salvage;
  13. public sealed class FultonSystem : SharedFultonSystem
  14. {
  15. [Dependency] private readonly ISerializationManager _serManager = default!;
  16. [Dependency] private readonly AnimationPlayerSystem _player = default!;
  17. private static readonly TimeSpan AnimationDuration = TimeSpan.FromSeconds(0.4);
  18. private static readonly Animation InitialAnimation = new()
  19. {
  20. Length = AnimationDuration,
  21. AnimationTracks =
  22. {
  23. new AnimationTrackSpriteFlick
  24. {
  25. LayerKey = FultonVisualLayers.Base,
  26. KeyFrames =
  27. {
  28. new AnimationTrackSpriteFlick.KeyFrame(new RSI.StateId("fulton_expand"), 0f),
  29. new AnimationTrackSpriteFlick.KeyFrame(new RSI.StateId("fulton_balloon"), 0.4f),
  30. }
  31. }
  32. }
  33. };
  34. private static readonly Animation FultonAnimation = new()
  35. {
  36. Length = TimeSpan.FromSeconds(0.8f),
  37. AnimationTracks =
  38. {
  39. new AnimationTrackComponentProperty()
  40. {
  41. ComponentType = typeof(SpriteComponent),
  42. Property = nameof(SpriteComponent.Offset),
  43. KeyFrames =
  44. {
  45. new AnimationTrackProperty.KeyFrame(Vector2.Zero, 0f),
  46. new AnimationTrackProperty.KeyFrame(new Vector2(0f, -0.3f), 0.3f),
  47. new AnimationTrackProperty.KeyFrame(new Vector2(0f, 20f), 0.5f),
  48. }
  49. }
  50. }
  51. };
  52. public override void Initialize()
  53. {
  54. base.Initialize();
  55. SubscribeLocalEvent<FultonedComponent, AfterAutoHandleStateEvent>(OnHandleState);
  56. SubscribeNetworkEvent<FultonAnimationMessage>(OnFultonMessage);
  57. }
  58. private void OnFultonMessage(FultonAnimationMessage ev)
  59. {
  60. var entity = GetEntity(ev.Entity);
  61. var coordinates = GetCoordinates(ev.Coordinates);
  62. if (Deleted(entity) || !TryComp<SpriteComponent>(entity, out var entSprite))
  63. return;
  64. var animationEnt = Spawn(null, coordinates);
  65. // TODO: Spawn fulton layer
  66. var sprite = AddComp<SpriteComponent>(animationEnt);
  67. _serManager.CopyTo(entSprite, ref sprite, notNullableOverride: true);
  68. if (TryComp<AppearanceComponent>(entity, out var entAppearance))
  69. {
  70. var appearance = AddComp<AppearanceComponent>(animationEnt);
  71. _serManager.CopyTo(entAppearance, ref appearance, notNullableOverride: true);
  72. }
  73. sprite.NoRotation = true;
  74. var effectLayer = sprite.AddLayer(new SpriteSpecifier.Rsi(new ResPath("Objects/Tools/fulton_balloon.rsi"), "fulton_balloon"));
  75. sprite.LayerSetOffset(effectLayer, EffectOffset + new Vector2(0f, 0.5f));
  76. var despawn = AddComp<TimedDespawnComponent>(animationEnt);
  77. despawn.Lifetime = 1.5f;
  78. _player.Play(animationEnt, FultonAnimation, "fulton-animation");
  79. }
  80. private void OnHandleState(EntityUid uid, FultonedComponent component, ref AfterAutoHandleStateEvent args)
  81. {
  82. UpdateAppearance(uid, component);
  83. }
  84. protected override void UpdateAppearance(EntityUid uid, FultonedComponent component)
  85. {
  86. if (!component.Effect.IsValid())
  87. return;
  88. var startTime = component.NextFulton - component.FultonDuration;
  89. var elapsed = Timing.CurTime - startTime;
  90. if (elapsed >= AnimationDuration)
  91. {
  92. return;
  93. }
  94. _player.Play(component.Effect, InitialAnimation, "fulton");
  95. }
  96. [UsedImplicitly]
  97. public enum FultonVisualLayers : byte
  98. {
  99. Base,
  100. }
  101. }