mirror of
https://github.com/space-syndicate/space-station-14.git
synced 2026-10-09 18:31:17 +03:00
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>
This commit is contained in:
parent
6c29a6eecb
commit
ab96ec8b20
2 changed files with 73 additions and 0 deletions
|
|
@ -0,0 +1,25 @@
|
|||
using Content.Shared.EntityTable.EntitySelectors;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Power.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Spawns a entity when the battery reaches max charge.
|
||||
/// It also consumes all power when spawning the entity.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed partial class SpawnOnBatteryFullComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// A entity proto to spawn.
|
||||
/// </summary>
|
||||
/// <remarks>Takes priority over <see cref="Table"/></remarks>
|
||||
[DataField]
|
||||
public EntProtoId? Proto;
|
||||
|
||||
/// <summary>
|
||||
/// A entity table to spawn stuff from.
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public EntityTableSelector? Table;
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue