1
0

JukeboxSystem.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Content.Shared.Audio.Jukebox;
  2. using Robust.Client.Animations;
  3. using Robust.Client.GameObjects;
  4. using Robust.Shared.Prototypes;
  5. namespace Content.Client.Audio.Jukebox;
  6. public sealed class JukeboxSystem : SharedJukeboxSystem
  7. {
  8. [Dependency] private readonly IPrototypeManager _protoManager = default!;
  9. [Dependency] private readonly AnimationPlayerSystem _animationPlayer = default!;
  10. [Dependency] private readonly SharedAppearanceSystem _appearanceSystem = default!;
  11. [Dependency] private readonly SharedUserInterfaceSystem _uiSystem = default!;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. SubscribeLocalEvent<JukeboxComponent, AppearanceChangeEvent>(OnAppearanceChange);
  16. SubscribeLocalEvent<JukeboxComponent, AnimationCompletedEvent>(OnAnimationCompleted);
  17. SubscribeLocalEvent<JukeboxComponent, AfterAutoHandleStateEvent>(OnJukeboxAfterState);
  18. _protoManager.PrototypesReloaded += OnProtoReload;
  19. }
  20. public override void Shutdown()
  21. {
  22. base.Shutdown();
  23. _protoManager.PrototypesReloaded -= OnProtoReload;
  24. }
  25. private void OnProtoReload(PrototypesReloadedEventArgs obj)
  26. {
  27. if (!obj.WasModified<JukeboxPrototype>())
  28. return;
  29. var query = AllEntityQuery<JukeboxComponent, UserInterfaceComponent>();
  30. while (query.MoveNext(out var uid, out _, out var ui))
  31. {
  32. if (!_uiSystem.TryGetOpenUi<JukeboxBoundUserInterface>((uid, ui), JukeboxUiKey.Key, out var bui))
  33. continue;
  34. bui.PopulateMusic();
  35. }
  36. }
  37. private void OnJukeboxAfterState(Entity<JukeboxComponent> ent, ref AfterAutoHandleStateEvent args)
  38. {
  39. if (!_uiSystem.TryGetOpenUi<JukeboxBoundUserInterface>(ent.Owner, JukeboxUiKey.Key, out var bui))
  40. return;
  41. bui.Reload();
  42. }
  43. private void OnAnimationCompleted(EntityUid uid, JukeboxComponent component, AnimationCompletedEvent args)
  44. {
  45. if (!TryComp<SpriteComponent>(uid, out var sprite))
  46. return;
  47. if (!TryComp<AppearanceComponent>(uid, out var appearance) ||
  48. !_appearanceSystem.TryGetData<JukeboxVisualState>(uid, JukeboxVisuals.VisualState, out var visualState, appearance))
  49. {
  50. visualState = JukeboxVisualState.On;
  51. }
  52. UpdateAppearance(uid, visualState, component, sprite);
  53. }
  54. private void OnAppearanceChange(EntityUid uid, JukeboxComponent component, ref AppearanceChangeEvent args)
  55. {
  56. if (args.Sprite == null)
  57. return;
  58. if (!args.AppearanceData.TryGetValue(JukeboxVisuals.VisualState, out var visualStateObject) ||
  59. visualStateObject is not JukeboxVisualState visualState)
  60. {
  61. visualState = JukeboxVisualState.On;
  62. }
  63. UpdateAppearance(uid, visualState, component, args.Sprite);
  64. }
  65. private void UpdateAppearance(EntityUid uid, JukeboxVisualState visualState, JukeboxComponent component, SpriteComponent sprite)
  66. {
  67. SetLayerState(JukeboxVisualLayers.Base, component.OffState, sprite);
  68. switch (visualState)
  69. {
  70. case JukeboxVisualState.On:
  71. SetLayerState(JukeboxVisualLayers.Base, component.OnState, sprite);
  72. break;
  73. case JukeboxVisualState.Off:
  74. SetLayerState(JukeboxVisualLayers.Base, component.OffState, sprite);
  75. break;
  76. case JukeboxVisualState.Select:
  77. PlayAnimation(uid, JukeboxVisualLayers.Base, component.SelectState, 1.0f, sprite);
  78. break;
  79. }
  80. }
  81. private void PlayAnimation(EntityUid uid, JukeboxVisualLayers layer, string? state, float animationTime, SpriteComponent sprite)
  82. {
  83. if (string.IsNullOrEmpty(state))
  84. return;
  85. if (!_animationPlayer.HasRunningAnimation(uid, state))
  86. {
  87. var animation = GetAnimation(layer, state, animationTime);
  88. sprite.LayerSetVisible(layer, true);
  89. _animationPlayer.Play(uid, animation, state);
  90. }
  91. }
  92. private static Animation GetAnimation(JukeboxVisualLayers layer, string state, float animationTime)
  93. {
  94. return new Animation
  95. {
  96. Length = TimeSpan.FromSeconds(animationTime),
  97. AnimationTracks =
  98. {
  99. new AnimationTrackSpriteFlick
  100. {
  101. LayerKey = layer,
  102. KeyFrames =
  103. {
  104. new AnimationTrackSpriteFlick.KeyFrame(state, 0f)
  105. }
  106. }
  107. }
  108. };
  109. }
  110. private void SetLayerState(JukeboxVisualLayers layer, string? state, SpriteComponent sprite)
  111. {
  112. if (string.IsNullOrEmpty(state))
  113. return;
  114. sprite.LayerSetVisible(layer, true);
  115. sprite.LayerSetAutoAnimated(layer, true);
  116. sprite.LayerSetState(layer, state);
  117. }
  118. }