CatStation/Content.Shared/Power/EntitySystems/PowerStateSystem.cs
Pieter-Jan Briers 5168b5f3d4
IoC source gen compatibility (#43863)
* IoC source gen compatibility

Can be merged before or after https://github.com/space-wizards/RobustToolbox/pull/6549 doesn't really matter.

* Missed a spot
2026-05-09 03:29:58 +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 partial class SharedPowerStateSystem : EntitySystem
{
[Dependency] private SharedPowerReceiverSystem _powerReceiverSystem = default!;
[Dependency] private 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);
}
}