CatStation/Content.Shared/Power/EntitySystems/PowerStateSystem.cs
ArtisticRoomba 376f16504c
Content.Shared EntityQuery<T> dependency injection refactor (#43498)
* part 1

* second pass

* final fix

* last fix i swear

* an even bigger genius

* regex missed this

* should be all of em?

* oops

* yeah

* Revert "should be all of em?"

This reverts commit 4dd54d1b9a76977b4cf8d6008358e4dacdee6eb1.

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
2026-04-11 21:30:36 +00:00

48 lines
2.1 KiB
C#

using Content.Shared.Power.Components;
using JetBrains.Annotations;
namespace Content.Shared.Power.EntitySystems;
/// <summary>
/// Generic system that handles entities with <see cref="PowerStateComponent"/>.
/// Used for simple machines that only need to switch between "idle" and "working" power states.
/// </summary>
public abstract class SharedPowerStateSystem : EntitySystem
{
[Dependency] private readonly SharedPowerReceiverSystem _powerReceiverSystem = default!;
[Dependency] private readonly EntityQuery<PowerStateComponent> _powerStateQuery = default!;
/// <summary>
/// Sets the working state of the entity, adjusting its power draw accordingly.
/// </summary>
/// <param name="ent">The entity to set the working state for.</param>
/// <param name="working">Whether the entity should be in the working state.</param>
[PublicAPI]
public void SetWorkingState(Entity<PowerStateComponent?> ent, bool working)
{
if (!_powerStateQuery.Resolve(ent, ref ent.Comp))
return;
_powerReceiverSystem.SetLoad(ent.Owner, working ? ent.Comp.WorkingPowerDraw : ent.Comp.IdlePowerDraw);
ent.Comp.IsWorking = working;
}
/// <summary>
/// Tries to set the working state of the entity, adjusting its power draw accordingly.
/// Use this for if you're not sure if the entity has a <see cref="PowerStateComponent"/>.
/// </summary>
/// <param name="ent">The entity to set the working state for.</param>
/// <param name="working">Whether the entity should be in the working state.</param>
[PublicAPI]
public void TrySetWorkingState(Entity<PowerStateComponent?> ent, bool working)
{
// Sometimes systems calling this API handle generic objects that can or can't consume power,
// so to reduce boilerplate we don't log an error. Any entity that *should* have an ApcPowerRecieverComponent
// will log an error in tests if someone tries to add an entity that doesn't have one.
if (!_powerStateQuery.Resolve(ent, ref ent.Comp, false))
return;
SetWorkingState(ent, working);
}
}