using Content.Shared.Audio.Jukebox;
using Robust.Client.Animations;
using Robust.Client.GameObjects;
using Robust.Shared.Prototypes;
namespace Content.Client.Audio.Jukebox;
public sealed class JukeboxSystem : SharedJukeboxSystem
{
[Dependency] private readonly IPrototypeManager _protoManager = default!;
[Dependency] private readonly AnimationPlayerSystem _animationPlayer = default!;
[Dependency] private readonly SharedAppearanceSystem _appearanceSystem = default!;
[Dependency] private readonly SharedUserInterfaceSystem _uiSystem = default!;
public override void Initialize()
base.Initialize();
SubscribeLocalEvent<JukeboxComponent, AppearanceChangeEvent>(OnAppearanceChange);
SubscribeLocalEvent<JukeboxComponent, AnimationCompletedEvent>(OnAnimationCompleted);
SubscribeLocalEvent<JukeboxComponent, AfterAutoHandleStateEvent>(OnJukeboxAfterState);
_protoManager.PrototypesReloaded += OnProtoReload;
}
public override void Shutdown()
base.Shutdown();
_protoManager.PrototypesReloaded -= OnProtoReload;
private void OnProtoReload(PrototypesReloadedEventArgs obj)
if (!obj.WasModified<JukeboxPrototype>())
return;
var query = AllEntityQuery<JukeboxComponent, UserInterfaceComponent>();
while (query.MoveNext(out var uid, out var jukebox, out var ui))
if (!_uiSystem.TryGetOpenUi<JukeboxBoundUserInterface>((uid, ui), JukeboxUiKey.Key, out var bui))
continue;
if (obj.Removed?.TryGetValue(typeof(JukeboxPrototype), out var removed) ?? false)
var list = new List<ProtoId<JukeboxPrototype>>();
while (jukebox.SongIdQueue.Count > 0)
var next = jukebox.SongIdQueue.Dequeue();
if (removed.Contains(next))
list.Add(next);
foreach (var song in list)
jukebox.SongIdQueue.Enqueue(song);
bui.PopulateMusic();
private void OnJukeboxAfterState(Entity<JukeboxComponent> ent, ref AfterAutoHandleStateEvent args)
if (!_uiSystem.TryGetOpenUi<JukeboxBoundUserInterface>(ent.Owner, JukeboxUiKey.Key, out var bui))
bui.Reload();
private void OnAnimationCompleted(EntityUid uid, JukeboxComponent component, AnimationCompletedEvent args)
if (!TryComp<SpriteComponent>(uid, out var sprite))
if (!TryComp<AppearanceComponent>(uid, out var appearance) ||
!_appearanceSystem.TryGetData<JukeboxVisualState>(uid, JukeboxVisuals.VisualState, out var visualState, appearance))
visualState = JukeboxVisualState.On;
UpdateAppearance(uid, visualState, component, sprite);
private void OnAppearanceChange(EntityUid uid, JukeboxComponent component, ref AppearanceChangeEvent args)
if (args.Sprite == null)
if (!args.AppearanceData.TryGetValue(JukeboxVisuals.VisualState, out var visualStateObject) ||
visualStateObject is not JukeboxVisualState visualState)
UpdateAppearance(uid, visualState, component, args.Sprite);
private void UpdateAppearance(EntityUid uid, JukeboxVisualState visualState, JukeboxComponent component, SpriteComponent sprite)
SetLayerState(JukeboxVisualLayers.Base, component.OffState, sprite);
switch (visualState)
case JukeboxVisualState.On:
SetLayerState(JukeboxVisualLayers.Base, component.OnState, sprite);
break;
case JukeboxVisualState.Off:
case JukeboxVisualState.Select:
PlayAnimation(uid, JukeboxVisualLayers.Base, component.SelectState, 1.0f, sprite);
private void PlayAnimation(EntityUid uid, JukeboxVisualLayers layer, string? state, float animationTime, SpriteComponent sprite)
if (string.IsNullOrEmpty(state))
if (!_animationPlayer.HasRunningAnimation(uid, state))
var animation = GetAnimation(layer, state, animationTime);
sprite.LayerSetVisible(layer, true);
_animationPlayer.Play(uid, animation, state);
private static Animation GetAnimation(JukeboxVisualLayers layer, string state, float animationTime)
return new Animation
Length = TimeSpan.FromSeconds(animationTime),
AnimationTracks =
new AnimationTrackSpriteFlick
LayerKey = layer,
KeyFrames =
new AnimationTrackSpriteFlick.KeyFrame(state, 0f)
};
private void SetLayerState(JukeboxVisualLayers layer, string? state, SpriteComponent sprite)
sprite.LayerSetAutoAnimated(layer, true);
sprite.LayerSetState(layer, state);