* Added Jukebox, along with music for jukebox * Fixed Jukebox meta.json copyright * Removed songs I couldn't find a license for. * Renamed files to solve check failures from spaces * Added missing attributions.yml * Fixed lack of description in Jukebox * Jukebox is now constructable. * Change Jukebox menu to FancyWindow * Moved Jukebox messages out of jukebox component * Removed Jukebox OnValueChanged. * JukeboxComp now uses AutoGenerateComponentState * Removed state code, since it's auto generated * Fixed various Jukebox code to match conventions. * Updated Standard.yml to match changed song list. * fixes * Jukebox workin * Fix * Polishing * Finalising * Revert * bad * jukey * Reviews * name * Update submodule to 218.2.0 --------- Co-authored-by: iNVERTED <alextjorgensen@gmail.com>
153 lines
5.0 KiB
C#
153 lines
5.0 KiB
C#
using Content.Server.Power.Components;
|
|
using Content.Server.Power.EntitySystems;
|
|
using Content.Shared.Audio.Jukebox;
|
|
using Robust.Server.GameObjects;
|
|
using Robust.Shared.Audio;
|
|
using Robust.Shared.Audio.Components;
|
|
using Robust.Shared.Audio.Systems;
|
|
using Robust.Shared.Prototypes;
|
|
using JukeboxComponent = Content.Shared.Audio.Jukebox.JukeboxComponent;
|
|
|
|
namespace Content.Server.Audio.Jukebox;
|
|
|
|
|
|
public sealed class JukeboxSystem : SharedJukeboxSystem
|
|
{
|
|
[Dependency] private readonly IPrototypeManager _protoManager = default!;
|
|
[Dependency] private readonly AppearanceSystem _appearanceSystem = default!;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
SubscribeLocalEvent<JukeboxComponent, JukeboxSelectedMessage>(OnJukeboxSelected);
|
|
SubscribeLocalEvent<JukeboxComponent, JukeboxPlayingMessage>(OnJukeboxPlay);
|
|
SubscribeLocalEvent<JukeboxComponent, JukeboxPauseMessage>(OnJukeboxPause);
|
|
SubscribeLocalEvent<JukeboxComponent, JukeboxStopMessage>(OnJukeboxStop);
|
|
SubscribeLocalEvent<JukeboxComponent, JukeboxSetTimeMessage>(OnJukeboxSetTime);
|
|
SubscribeLocalEvent<JukeboxComponent, ComponentInit>(OnComponentInit);
|
|
SubscribeLocalEvent<JukeboxComponent, ComponentShutdown>(OnComponentShutdown);
|
|
|
|
SubscribeLocalEvent<JukeboxComponent, PowerChangedEvent>(OnPowerChanged);
|
|
}
|
|
|
|
private void OnComponentInit(EntityUid uid, JukeboxComponent component, ComponentInit args)
|
|
{
|
|
if (HasComp<ApcPowerReceiverComponent>(uid))
|
|
{
|
|
TryUpdateVisualState(uid, component);
|
|
}
|
|
}
|
|
|
|
private void OnJukeboxPlay(EntityUid uid, JukeboxComponent component, ref JukeboxPlayingMessage args)
|
|
{
|
|
if (Exists(component.AudioStream))
|
|
{
|
|
Audio.SetState(component.AudioStream, AudioState.Playing);
|
|
}
|
|
else
|
|
{
|
|
component.AudioStream = Audio.Stop(component.AudioStream);
|
|
|
|
if (string.IsNullOrEmpty(component.SelectedSongId) ||
|
|
!_protoManager.TryIndex(component.SelectedSongId, out var jukeboxProto))
|
|
{
|
|
return;
|
|
}
|
|
|
|
component.AudioStream = Audio.PlayPvs(jukeboxProto.Path, uid, AudioParams.Default.WithMaxDistance(10f))?.Entity;
|
|
Dirty(uid, component);
|
|
}
|
|
}
|
|
|
|
private void OnJukeboxPause(Entity<JukeboxComponent> ent, ref JukeboxPauseMessage args)
|
|
{
|
|
Audio.SetState(ent.Comp.AudioStream, AudioState.Paused);
|
|
}
|
|
|
|
private void OnJukeboxSetTime(EntityUid uid, JukeboxComponent component, JukeboxSetTimeMessage args)
|
|
{
|
|
var offset = (args.Session.Channel.Ping * 1.5f) / 1000f;
|
|
Audio.SetPlaybackPosition(component.AudioStream, args.SongTime + offset);
|
|
}
|
|
|
|
private void OnPowerChanged(Entity<JukeboxComponent> entity, ref PowerChangedEvent args)
|
|
{
|
|
TryUpdateVisualState(entity);
|
|
|
|
if (!this.IsPowered(entity.Owner, EntityManager))
|
|
{
|
|
Stop(entity);
|
|
}
|
|
}
|
|
|
|
private void OnJukeboxStop(Entity<JukeboxComponent> entity, ref JukeboxStopMessage args)
|
|
{
|
|
Stop(entity);
|
|
}
|
|
|
|
private void Stop(Entity<JukeboxComponent> entity)
|
|
{
|
|
Audio.SetState(entity.Comp.AudioStream, AudioState.Stopped);
|
|
Dirty(entity);
|
|
}
|
|
|
|
private void OnJukeboxSelected(EntityUid uid, JukeboxComponent component, JukeboxSelectedMessage args)
|
|
{
|
|
if (!Audio.IsPlaying(component.AudioStream))
|
|
{
|
|
component.SelectedSongId = args.SongId;
|
|
DirectSetVisualState(uid, JukeboxVisualState.Select);
|
|
component.Selecting = true;
|
|
component.AudioStream = Audio.Stop(component.AudioStream);
|
|
}
|
|
|
|
Dirty(uid, component);
|
|
}
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
base.Update(frameTime);
|
|
|
|
var query = EntityQueryEnumerator<JukeboxComponent>();
|
|
while (query.MoveNext(out var uid, out var comp))
|
|
{
|
|
if (comp.Selecting)
|
|
{
|
|
comp.SelectAccumulator += frameTime;
|
|
if (comp.SelectAccumulator >= 0.5f)
|
|
{
|
|
comp.SelectAccumulator = 0f;
|
|
comp.Selecting = false;
|
|
|
|
TryUpdateVisualState(uid, comp);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnComponentShutdown(EntityUid uid, JukeboxComponent component, ComponentShutdown args)
|
|
{
|
|
component.AudioStream = Audio.Stop(component.AudioStream);
|
|
}
|
|
|
|
private void DirectSetVisualState(EntityUid uid, JukeboxVisualState state)
|
|
{
|
|
_appearanceSystem.SetData(uid, JukeboxVisuals.VisualState, state);
|
|
}
|
|
|
|
private void TryUpdateVisualState(EntityUid uid, JukeboxComponent? jukeboxComponent = null)
|
|
{
|
|
if (!Resolve(uid, ref jukeboxComponent))
|
|
return;
|
|
|
|
var finalState = JukeboxVisualState.On;
|
|
|
|
if (!this.IsPowered(uid, EntityManager))
|
|
{
|
|
finalState = JukeboxVisualState.Off;
|
|
}
|
|
|
|
_appearanceSystem.SetData(uid, JukeboxVisuals.VisualState, finalState);
|
|
}
|
|
}
|