TurfSystem.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Numerics;
  2. using Content.Shared.Physics;
  3. using Robust.Shared.Map;
  4. using Robust.Shared.Map.Components;
  5. using Robust.Shared.Physics;
  6. namespace Content.Shared.Maps;
  7. /// <summary>
  8. /// This system provides various useful helper methods for turfs & tiles. Replacement for <see cref="TurfHelpers"/>
  9. /// </summary>
  10. public sealed class TurfSystem : EntitySystem
  11. {
  12. [Dependency] private readonly EntityLookupSystem _entityLookup = default!;
  13. [Dependency] private readonly SharedTransformSystem _transform = default!;
  14. /// <summary>
  15. /// Returns true if a given tile is blocked by physics-enabled entities.
  16. /// </summary>
  17. public bool IsTileBlocked(TileRef turf, CollisionGroup mask, float minIntersectionArea = 0.1f)
  18. => IsTileBlocked(turf.GridUid, turf.GridIndices, mask, minIntersectionArea: minIntersectionArea);
  19. /// <summary>
  20. /// Returns true if a given tile is blocked by physics-enabled entities.
  21. /// </summary>
  22. /// <param name="gridUid">The grid that owns the tile</param>
  23. /// <param name="indices">The tile indices</param>
  24. /// <param name="mask">Collision layers to check</param>
  25. /// <param name="grid">Grid component</param>
  26. /// <param name="gridXform">Grid's transform</param>
  27. /// <param name="minIntersectionArea">Minimum area that must be covered for a tile to be considered blocked</param>
  28. public bool IsTileBlocked(EntityUid gridUid,
  29. Vector2i indices,
  30. CollisionGroup mask,
  31. MapGridComponent? grid = null,
  32. TransformComponent? gridXform = null,
  33. float minIntersectionArea = 0.1f)
  34. {
  35. if (!Resolve(gridUid, ref grid, ref gridXform))
  36. return false;
  37. var xformQuery = GetEntityQuery<TransformComponent>();
  38. var (gridPos, gridRot, matrix) = _transform.GetWorldPositionRotationMatrix(gridXform, xformQuery);
  39. var size = grid.TileSize;
  40. var localPos = new Vector2(indices.X * size + (size / 2f), indices.Y * size + (size / 2f));
  41. var worldPos = Vector2.Transform(localPos, matrix);
  42. // This is scaled to 95 % so it doesn't encompass walls on other tiles.
  43. var tileAabb = Box2.UnitCentered.Scale(0.95f * size);
  44. var worldBox = new Box2Rotated(tileAabb.Translated(worldPos), gridRot, worldPos);
  45. tileAabb = tileAabb.Translated(localPos);
  46. var intersectionArea = 0f;
  47. var fixtureQuery = GetEntityQuery<FixturesComponent>();
  48. foreach (var ent in _entityLookup.GetEntitiesIntersecting(gridUid, worldBox, LookupFlags.Dynamic | LookupFlags.Static))
  49. {
  50. if (!fixtureQuery.TryGetComponent(ent, out var fixtures))
  51. continue;
  52. // get grid local coordinates
  53. var (pos, rot) = _transform.GetWorldPositionRotation(xformQuery.GetComponent(ent), xformQuery);
  54. rot -= gridRot;
  55. pos = (-gridRot).RotateVec(pos - gridPos);
  56. var xform = new Transform(pos, (float) rot.Theta);
  57. foreach (var fixture in fixtures.Fixtures.Values)
  58. {
  59. if (!fixture.Hard)
  60. continue;
  61. if ((fixture.CollisionLayer & (int) mask) == 0)
  62. continue;
  63. for (var i = 0; i < fixture.Shape.ChildCount; i++)
  64. {
  65. var intersection = fixture.Shape.ComputeAABB(xform, i).Intersect(tileAabb);
  66. intersectionArea += intersection.Width * intersection.Height;
  67. if (intersectionArea > minIntersectionArea)
  68. return true;
  69. }
  70. }
  71. }
  72. return false;
  73. }
  74. /// <summary>
  75. /// Returns the location of the centre of the tile in grid coordinates.
  76. /// </summary>
  77. public EntityCoordinates GetTileCenter(TileRef turf)
  78. {
  79. var grid = Comp<MapGridComponent>(turf.GridUid);
  80. var center = (turf.GridIndices + new Vector2(0.5f, 0.5f)) * grid.TileSize;
  81. return new EntityCoordinates(turf.GridUid, center);
  82. }
  83. }