DeployableBarrierSystem.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Content.Shared.Lock;
  2. using Content.Shared.Movement.Pulling.Components;
  3. using Content.Shared.Movement.Pulling.Systems;
  4. using Content.Shared.Security.Components;
  5. using Robust.Shared.Physics.Systems;
  6. namespace Content.Shared.Security.Systems;
  7. public sealed class DeployableBarrierSystem : EntitySystem
  8. {
  9. [Dependency] private readonly FixtureSystem _fixtures = default!;
  10. [Dependency] private readonly SharedPointLightSystem _pointLight = default!;
  11. [Dependency] private readonly SharedPhysicsSystem _physics = default!;
  12. [Dependency] private readonly PullingSystem _pulling = default!;
  13. [Dependency] private readonly SharedTransformSystem _transform = default!;
  14. public override void Initialize()
  15. {
  16. SubscribeLocalEvent<DeployableBarrierComponent, MapInitEvent>(OnMapInit);
  17. SubscribeLocalEvent<DeployableBarrierComponent, LockToggledEvent>(OnLockToggled);
  18. }
  19. private void OnMapInit(EntityUid uid, DeployableBarrierComponent component, MapInitEvent args)
  20. {
  21. if (!TryComp(uid, out LockComponent? lockComponent))
  22. return;
  23. ToggleBarrierDeploy(uid, lockComponent.Locked, component);
  24. }
  25. private void OnLockToggled(EntityUid uid, DeployableBarrierComponent component, ref LockToggledEvent args)
  26. {
  27. ToggleBarrierDeploy(uid, args.Locked, component);
  28. }
  29. private void ToggleBarrierDeploy(EntityUid uid, bool isDeployed, DeployableBarrierComponent? component)
  30. {
  31. if (!Resolve(uid, ref component))
  32. return;
  33. var transform = Transform(uid);
  34. var fixture = _fixtures.GetFixtureOrNull(uid, component.FixtureId);
  35. if (isDeployed && transform.GridUid != null)
  36. {
  37. _transform.AnchorEntity(uid, transform);
  38. if (fixture != null)
  39. _physics.SetHard(uid, fixture, true);
  40. }
  41. else
  42. {
  43. _transform.Unanchor(uid, transform);
  44. if (fixture != null)
  45. _physics.SetHard(uid, fixture, false);
  46. }
  47. if (TryComp(uid, out PullableComponent? pullable))
  48. _pulling.TryStopPull(uid, pullable);
  49. SharedPointLightComponent? pointLight = null;
  50. if (_pointLight.ResolveLight(uid, ref pointLight))
  51. {
  52. _pointLight.SetEnabled(uid, isDeployed, pointLight);
  53. }
  54. }
  55. }