using Content.Shared.Power.Components; using JetBrains.Annotations; namespace Content.Shared.Power.EntitySystems; /// /// Generic system that handles entities with . /// Used for simple machines that only need to switch between "idle" and "working" power states. /// public abstract class SharedPowerStateSystem : EntitySystem { [Dependency] private readonly SharedPowerReceiverSystem _powerReceiverSystem = default!; private EntityQuery _powerStateQuery; public override void Initialize() { base.Initialize(); _powerStateQuery = GetEntityQuery(); } /// /// Sets the working state of the entity, adjusting its power draw accordingly. /// /// The entity to set the working state for. /// Whether the entity should be in the working state. [PublicAPI] public void SetWorkingState(Entity 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; } /// /// 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 . /// /// The entity to set the working state for. /// Whether the entity should be in the working state. [PublicAPI] public void TrySetWorkingState(Entity 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); } }