Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirror: Use entity queries in ambient sound & power receiver systems #376

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Content.Server/Power/EntitySystems/PowerReceiverSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public sealed class PowerReceiverSystem : EntitySystem
[Dependency] private readonly IAdminManager _adminManager = default!;
[Dependency] private readonly AppearanceSystem _appearance = default!;
[Dependency] private readonly AudioSystem _audio = default!;
private EntityQuery<ApcPowerReceiverComponent> _recQuery;
private EntityQuery<ApcPowerProviderComponent> _provQuery;

public override void Initialize()
{
Expand All @@ -35,6 +37,9 @@ public override void Initialize()

SubscribeLocalEvent<ApcPowerReceiverComponent, GetVerbsEvent<Verb>>(OnGetVerbs);
SubscribeLocalEvent<PowerSwitchComponent, GetVerbsEvent<AlternativeVerb>>(AddSwitchPowerVerb);

_recQuery = GetEntityQuery<ApcPowerReceiverComponent>();
_provQuery = GetEntityQuery<ApcPowerProviderComponent>();
}

private void OnGetVerbs(EntityUid uid, ApcPowerReceiverComponent component, GetVerbsEvent<Verb> args)
Expand Down Expand Up @@ -77,7 +82,7 @@ private void OnProviderShutdown(EntityUid uid, ApcPowerProviderComponent compone
private void OnProviderConnected(Entity<ApcPowerReceiverComponent> receiver, ref ExtensionCableSystem.ProviderConnectedEvent args)
{
var providerUid = args.Provider.Owner;
if (!EntityManager.TryGetComponent<ApcPowerProviderComponent>(providerUid, out var provider))
if (!_provQuery.TryGetComponent(providerUid, out var provider))
return;

receiver.Comp.Provider = provider;
Expand All @@ -94,15 +99,15 @@ private void OnProviderDisconnected(Entity<ApcPowerReceiverComponent> receiver,

private void OnReceiverConnected(Entity<ApcPowerProviderComponent> provider, ref ExtensionCableSystem.ReceiverConnectedEvent args)
{
if (EntityManager.TryGetComponent(args.Receiver, out ApcPowerReceiverComponent? receiver))
if (_recQuery.TryGetComponent(args.Receiver, out var receiver))
{
provider.Comp.AddReceiver(receiver);
}
}

private void OnReceiverDisconnected(EntityUid uid, ApcPowerProviderComponent provider, ExtensionCableSystem.ReceiverDisconnectedEvent args)
{
if (EntityManager.TryGetComponent(args.Receiver, out ApcPowerReceiverComponent? receiver))
if (_recQuery.TryGetComponent(args.Receiver, out var receiver))
{
provider.RemoveReceiver(receiver);
}
Expand All @@ -116,7 +121,7 @@ private void AddSwitchPowerVerb(EntityUid uid, PowerSwitchComponent component, G
if (!HasComp<HandsComponent>(args.User))
return;

if (!TryComp<ApcPowerReceiverComponent>(uid, out var receiver))
if (!_recQuery.TryGetComponent(uid, out var receiver))
return;

if (!receiver.NeedsPower)
Expand Down Expand Up @@ -152,7 +157,7 @@ private void ProviderChanged(Entity<ApcPowerReceiverComponent> receiver)
/// </summary>
public bool IsPowered(EntityUid uid, ApcPowerReceiverComponent? receiver = null)
{
if (!Resolve(uid, ref receiver, false))
if (!_recQuery.Resolve(uid, ref receiver, false))
return true;

return receiver.Powered;
Expand All @@ -164,7 +169,7 @@ public bool IsPowered(EntityUid uid, ApcPowerReceiverComponent? receiver = null)
/// </summary>
public bool TogglePower(EntityUid uid, bool playSwitchSound = true, ApcPowerReceiverComponent? receiver = null, EntityUid? user = null)
{
if (!Resolve(uid, ref receiver, false))
if (!_recQuery.Resolve(uid, ref receiver, false))
return true;

// it'll save a lot of confusion if 'always powered' means 'always powered'
Expand Down
11 changes: 7 additions & 4 deletions Content.Shared/Audio/SharedAmbientSoundSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ namespace Content.Shared.Audio;

public abstract class SharedAmbientSoundSystem : EntitySystem
{
private EntityQuery<AmbientSoundComponent> _query;

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<AmbientSoundComponent, ComponentGetState>(GetCompState);
SubscribeLocalEvent<AmbientSoundComponent, ComponentHandleState>(HandleCompState);
_query = GetEntityQuery<AmbientSoundComponent>();
}

public virtual void SetAmbience(EntityUid uid, bool value, AmbientSoundComponent? ambience = null)
{
if (!Resolve(uid, ref ambience, false) || ambience.Enabled == value)
if (!_query.Resolve(uid, ref ambience, false) || ambience.Enabled == value)
return;

ambience.Enabled = value;
Expand All @@ -24,7 +27,7 @@ public virtual void SetAmbience(EntityUid uid, bool value, AmbientSoundComponent

public virtual void SetRange(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
{
if (!Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Range, value))
if (!_query.Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Range, value))
return;

ambience.Range = value;
Expand All @@ -39,7 +42,7 @@ protected virtual void QueueUpdate(EntityUid uid, AmbientSoundComponent ambience

public virtual void SetVolume(EntityUid uid, float value, AmbientSoundComponent? ambience = null)
{
if (!Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Volume, value))
if (!_query.Resolve(uid, ref ambience, false) || MathHelper.CloseToPercent(ambience.Volume, value))
return;

ambience.Volume = value;
Expand All @@ -48,7 +51,7 @@ public virtual void SetVolume(EntityUid uid, float value, AmbientSoundComponent?

public virtual void SetSound(EntityUid uid, SoundSpecifier sound, AmbientSoundComponent? ambience = null)
{
if (!Resolve(uid, ref ambience, false) || ambience.Sound == sound)
if (!_query.Resolve(uid, ref ambience, false) || ambience.Sound == sound)
return;

ambience.Sound = sound;
Expand Down
Loading