CutWireOnMapInitSystem.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. using Robust.Shared.Random;
  2. namespace Content.Server.Wires;
  3. /// <summary>
  4. /// Handles cutting a random wire on devices that have <see cref="CutWireOnMapInitComponent"/>.
  5. /// </summary>
  6. public sealed partial class CutWireOnMapInitSystem : EntitySystem
  7. {
  8. [Dependency] private readonly IRobustRandom _random = default!;
  9. public override void Initialize()
  10. {
  11. base.Initialize();
  12. SubscribeLocalEvent<CutWireOnMapInitComponent, MapInitEvent>(OnMapInit, after: [typeof(WiresSystem)]);
  13. }
  14. private void OnMapInit(Entity<CutWireOnMapInitComponent> entity, ref MapInitEvent args)
  15. {
  16. if (TryComp<WiresComponent>(entity, out var panel) && panel.WiresList.Count > 0)
  17. {
  18. // Pick a random wire
  19. var targetWire = _random.Pick(panel.WiresList);
  20. // Cut the wire
  21. if (targetWire.Action == null || targetWire.Action.Cut(EntityUid.Invalid, targetWire))
  22. targetWire.IsCut = true;
  23. }
  24. // Our work here is done
  25. RemCompDeferred(entity, entity.Comp);
  26. }
  27. }