CatStation/Content.Server/Power/EntitySystems/SpawnOnBatteryFullSystem.cs
Samuka ab96ec8b20
SpawnOnBatteryFull component (#43727)
* code stuff

* fix error

* now its fixed

* use the charge from the arg

* only on battery full

* remove whitespace

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>

* use default! instead of null!

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>

* use set charge

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
2026-04-25 22:32:31 +00:00

48 lines
1.5 KiB
C#

using Content.Server.Power.Components;
using Content.Shared.EntityTable;
using Content.Shared.EntityTable.EntitySelectors;
using Content.Shared.Power;
using Content.Shared.Power.Components;
using Robust.Shared.Prototypes;
namespace Content.Server.Power.EntitySystems;
public sealed class SpawnOnBatteryFullSystem : EntitySystem
{
[Dependency] private readonly BatterySystem _battery = default!;
[Dependency] private readonly EntityTableSystem _entityTable = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SpawnOnBatteryFullComponent, BatteryStateChangedEvent>(OnBatteryStateChange);
}
private void OnBatteryStateChange(Entity<SpawnOnBatteryFullComponent> entity, ref BatteryStateChangedEvent args)
{
if (args.NewState != BatteryState.Full)
return;
if (entity.Comp.Proto == null)
SpawnFromEntityTable(entity, entity.Comp.Table);
else
SpawnFromProto(entity, entity.Comp.Proto.Value);
_battery.SetCharge(entity.Owner, 0);
}
private void SpawnFromEntityTable(EntityUid entity, EntityTableSelector? table)
{
var spawns = _entityTable.GetSpawns(table);
foreach (var spawn in spawns)
{
Spawn(spawn, Transform(entity).Coordinates);
}
}
private void SpawnFromProto(EntityUid entity, EntProtoId proto)
{
Spawn(proto, Transform(entity).Coordinates);
}
}