1
0

RestrictedRangeSystem.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System.Numerics;
  2. using Content.Shared.Movement.Components;
  3. using Content.Shared.Physics;
  4. using Content.Shared.Salvage;
  5. using Robust.Shared.Map;
  6. using Robust.Shared.Physics.Collision.Shapes;
  7. using Robust.Shared.Physics.Components;
  8. using Robust.Shared.Physics.Systems;
  9. namespace Content.Server.Salvage;
  10. public sealed class RestrictedRangeSystem : SharedRestrictedRangeSystem
  11. {
  12. [Dependency] private readonly FixtureSystem _fixtures = default!;
  13. [Dependency] private readonly SharedPhysicsSystem _physics = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<RestrictedRangeComponent, MapInitEvent>(OnRestrictedMapInit);
  18. }
  19. private void OnRestrictedMapInit(EntityUid uid, RestrictedRangeComponent component, MapInitEvent args)
  20. {
  21. component.BoundaryEntity = CreateBoundary(new EntityCoordinates(uid, component.Origin), component.Range);
  22. }
  23. public EntityUid CreateBoundary(EntityCoordinates coordinates, float range)
  24. {
  25. var boundaryUid = Spawn(null, coordinates);
  26. var boundaryPhysics = AddComp<PhysicsComponent>(boundaryUid);
  27. var cShape = new ChainShape();
  28. // Don't need it to be a perfect circle, just need it to be loosely accurate.
  29. cShape.CreateLoop(Vector2.Zero, range + 0.25f, false, count: 4);
  30. _fixtures.TryCreateFixture(
  31. boundaryUid,
  32. cShape,
  33. "boundary",
  34. collisionLayer: (int) (CollisionGroup.HighImpassable | CollisionGroup.Impassable | CollisionGroup.LowImpassable),
  35. body: boundaryPhysics);
  36. _physics.WakeBody(boundaryUid, body: boundaryPhysics);
  37. AddComp<BoundaryComponent>(boundaryUid);
  38. return boundaryUid;
  39. }
  40. }