Skip to content

Commit

Permalink
Mirror: Use entity queries in ambient sound & power receiver systems (#…
Browse files Browse the repository at this point in the history
…376)

## Mirror of PR #26410: [Use entity queries in ambient sound & power
receiver
systems](space-wizards/space-station-14#26410)
from <img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `31d70db547f855d2a3d4075e4fcf54d1c87c0f06`

PR opened by <img
src="https://avatars.githubusercontent.com/u/60421075?v=4"
width="16"/><a href="https://github.com/ElectroJr"> ElectroJr</a> at
2024-03-24 20:29:56 UTC

---

PR changed 2 files with 18 additions and 10 deletions.

The PR had the following labels:


---

<details open="true"><summary><h1>Original Body</h1></summary>

> 


</details>

Co-authored-by: SimpleStation14 <Unknown>
Co-authored-by: VMSolidus <[email protected]>
  • Loading branch information
SimpleStation14 and VMSolidus authored May 29, 2024
1 parent ec3986b commit 4e8365c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
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

0 comments on commit 4e8365c

Please sign in to comment.