Skip to content

Commit

Permalink
Replace some obsolete TileAccess methods (#5516)
Browse files Browse the repository at this point in the history
* Replace some obsolete TileAccess methods

* Guh
  • Loading branch information
MilenVolf authored Nov 12, 2024
1 parent fa9030e commit 2a1bcb6
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
9 changes: 5 additions & 4 deletions Robust.Client/Placement/Modes/AlignTileAny.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;

Expand All @@ -14,16 +15,16 @@ public AlignTileAny(PlacementManager pMan) : base(pMan) { }
public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
{
// Go over diagonal size so when placing in a line it doesn't stop snapping.
const float SearchBoxSize = 2f; // size of search box in meters
const float searchBoxSize = 2f; // size of search box in meters

MouseCoords = ScreenToCursorGrid(mouseScreen).AlignWithClosestGridTile(SearchBoxSize, pManager.EntityManager, pManager.MapManager);
MouseCoords = ScreenToCursorGrid(mouseScreen).AlignWithClosestGridTile(searchBoxSize, pManager.EntityManager, pManager.MapManager);

var gridId = MouseCoords.GetGridUid(pManager.EntityManager);
var gridId = pManager.EntityManager.System<SharedTransformSystem>().GetGrid(MouseCoords);

if (!pManager.EntityManager.TryGetComponent<MapGridComponent>(gridId, out var mapGrid))
return;

CurrentTile = mapGrid.GetTileRef(MouseCoords);
CurrentTile = pManager.EntityManager.System<SharedMapSystem>().GetTileRef(gridId.Value, mapGrid, MouseCoords);
float tileSize = mapGrid.TileSize; //convert from ushort to float
GridDistancing = tileSize;

Expand Down
11 changes: 5 additions & 6 deletions Robust.Client/Placement/Modes/AlignTileEmpty.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Numerics;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Maths;
Expand All @@ -19,12 +18,12 @@ public override void AlignPlacementMode(ScreenCoordinates mouseScreen)
MouseCoords = ScreenToCursorGrid(mouseScreen);

var tileSize = 1f;
var gridIdOpt = MouseCoords.GetGridUid(pManager.EntityManager);
var gridIdOpt = pManager.EntityManager.System<SharedTransformSystem>().GetGrid(MouseCoords);

if (gridIdOpt is EntityUid gridId && gridId.IsValid())
if (gridIdOpt is { } gridId && gridId.IsValid())
{
var mapGrid = pManager.EntityManager.GetComponent<MapGridComponent>(gridId);
CurrentTile = mapGrid.GetTileRef(MouseCoords);
CurrentTile = pManager.EntityManager.System<SharedMapSystem>().GetTileRef(gridId, mapGrid ,MouseCoords);
tileSize = mapGrid.TileSize; //convert from ushort to float
}

Expand All @@ -50,12 +49,12 @@ public override bool IsValidPosition(EntityCoordinates position)
return false;
}

var map = MouseCoords.GetMapId(pManager.EntityManager);
var map = pManager.EntityManager.System<SharedTransformSystem>().GetMapId(MouseCoords);
var bottomLeft = new Vector2(CurrentTile.X, CurrentTile.Y);
var topRight = new Vector2(CurrentTile.X + 0.99f, CurrentTile.Y + 0.99f);
var box = new Box2(bottomLeft, topRight);

return !EntitySystem.Get<EntityLookupSystem>().AnyEntitiesIntersecting(map, box);
return !pManager.EntityManager.System<EntityLookupSystem>().AnyEntitiesIntersecting(map, box);
}
}
}
6 changes: 3 additions & 3 deletions Robust.Client/Placement/PlacementManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -754,14 +754,14 @@ private void RequestPlacement(EntityCoordinates coordinates)

if (CurrentPermission.IsTile)
{
var gridIdOpt = coordinates.GetGridUid(EntityManager);
var gridIdOpt = EntityManager.System<SharedTransformSystem>().GetGrid(coordinates);
// If we have actually placed something on a valid grid...
if (gridIdOpt is EntityUid gridId && gridId.IsValid())
if (gridIdOpt is { } gridId && gridId.IsValid())
{
var grid = EntityManager.GetComponent<MapGridComponent>(gridId);

// no point changing the tile to the same thing.
if (grid.GetTileRef(coordinates).Tile.TypeId == CurrentPermission.TileType)
if (EntityManager.System<SharedMapSystem>().GetTileRef(gridId, grid, coordinates).Tile.TypeId == CurrentPermission.TileType)
return;
}

Expand Down
5 changes: 3 additions & 2 deletions Robust.Client/Placement/PlacementMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ public IEnumerable<EntityCoordinates> GridCoordinates()
/// </summary>
public TileRef GetTileRef(EntityCoordinates coordinates)
{
var gridUidOpt = coordinates.GetGridUid(pManager.EntityManager);
return gridUidOpt is EntityUid gridUid && gridUid.IsValid() ? pManager.EntityManager.GetComponent<MapGridComponent>(gridUid).GetTileRef(MouseCoords)
var gridUidOpt = pManager.EntityManager.System<SharedTransformSystem>().GetGrid(coordinates);
return gridUidOpt is { } gridUid && gridUid.IsValid()
? pManager.EntityManager.System<SharedMapSystem>().GetTileRef(gridUid, pManager.EntityManager.GetComponent<MapGridComponent>(gridUid), MouseCoords)
: new TileRef(gridUidOpt ?? EntityUid.Invalid,
MouseCoords.ToVector2i(pManager.EntityManager, pManager.MapManager, pManager.EntityManager.System<SharedTransformSystem>()), Tile.Empty);
}
Expand Down
18 changes: 9 additions & 9 deletions Robust.UnitTesting/Shared/Map/GridRotation_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task TestLocalWorldConversions()

await server.WaitAssertion(() =>
{
entMan.System<SharedMapSystem>().CreateMap(out var mapId);
mapSystem.CreateMap(out var mapId);
var grid = mapMan.CreateGridEntity(mapId);
var gridEnt = grid.Owner;
var coordinates = new EntityCoordinates(gridEnt, new Vector2(10, 0));
Expand All @@ -41,18 +41,18 @@ await server.WaitAssertion(() =>
Assert.That(mapSystem.WorldToLocal(grid.Owner, grid.Comp, coordinates.Position), Is.EqualTo(coordinates.Position));
// Rotate 180 degrees should show -10, 0 for the position in map-terms and 10, 0 for the position in entity terms (i.e. no change).
entMan.GetComponent<TransformComponent>(gridEnt).WorldRotation += new Angle(MathF.PI);
transformSystem.SetWorldRotation(gridEnt, transformSystem.GetWorldRotation(gridEnt) + new Angle(MathF.PI));
Assert.That(transformSystem.GetWorldRotation(gridEnt), Is.EqualTo(new Angle(MathF.PI)));
// Check the map coordinate rotates correctly
Assert.That(mapSystem.WorldToLocal(grid.Owner, grid.Comp, new Vector2(10, 0)).EqualsApprox(new Vector2(-10, 0), 0.01f));
Assert.That(mapSystem.LocalToWorld(grid.Owner, grid.Comp, coordinates.Position).EqualsApprox(new Vector2(-10, 0), 0.01f));
Assert.That(mapSystem.WorldToLocal(gridEnt, grid.Comp, new Vector2(10, 0)).EqualsApprox(new Vector2(-10, 0), 0.01f));
Assert.That(mapSystem.LocalToWorld(gridEnt, grid.Comp, coordinates.Position).EqualsApprox(new Vector2(-10, 0), 0.01f));
// Now we'll do the same for 180 degrees.
entMan.GetComponent<TransformComponent>(gridEnt).WorldRotation += MathF.PI / 2f;
transformSystem.SetWorldRotation(gridEnt, transformSystem.GetWorldRotation(gridEnt) + MathF.PI / 2f);
// If grid facing down then worldpos of 10, 0 gets rotated 90 degrees CCW and hence should be 0, 10
Assert.That(mapSystem.WorldToLocal(grid.Owner, grid.Comp, new Vector2(10, 0)).EqualsApprox(new Vector2(0, 10), 0.01f));
Assert.That(mapSystem.WorldToLocal(gridEnt, grid.Comp, new Vector2(10, 0)).EqualsApprox(new Vector2(0, 10), 0.01f));
// If grid facing down then local 10,0 pos should just return 0, -10 given it's aligned with the rotation.
Assert.That(mapSystem.LocalToWorld(grid.Owner, grid.Comp, coordinates.Position).EqualsApprox(new Vector2(0, -10), 0.01f));
Assert.That(mapSystem.LocalToWorld(gridEnt, grid.Comp, coordinates.Position).EqualsApprox(new Vector2(0, -10), 0.01f));
});
}

Expand All @@ -70,7 +70,7 @@ public async Task TestChunkRotations()

await server.WaitAssertion(() =>
{
entMan.System<SharedMapSystem>().CreateMap(out var mapId);
mapSystem.CreateMap(out var mapId);
var grid = mapMan.CreateGridEntity(mapId);
var gridEnt = grid.Owner;
Expand All @@ -85,7 +85,7 @@ await server.WaitAssertion(() =>
}
}
var chunks = mapSystem.GetMapChunks(grid.Owner, grid.Comp).Select(c => c.Value).ToList();
var chunks = mapSystem.GetMapChunks(gridEnt, grid.Comp).Select(c => c.Value).ToList();
Assert.That(chunks.Count, Is.EqualTo(1));
var chunk = chunks[0];
Expand Down
9 changes: 6 additions & 3 deletions Robust.UnitTesting/Shared/Physics/CollisionWake_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public async Task TestCollisionWakeGrid()

var entManager = server.ResolveDependency<IEntityManager>();
var mapManager = server.ResolveDependency<IMapManager>();
var mapSystem = entManager.System<SharedMapSystem>();
var transformSystem = entManager.System<SharedTransformSystem>();

Entity<MapGridComponent> grid = default!;
MapId mapId = default!;
Expand All @@ -51,14 +53,15 @@ public async Task TestCollisionWakeGrid()

await server.WaitPost(() =>
{
entManager.System<SharedMapSystem>().CreateMap(out mapId);
mapSystem.CreateMap(out mapId);
grid = mapManager.CreateGridEntity(mapId);
grid.Comp.SetTile(Vector2i.Zero, new Tile(1));
mapSystem.SetTile(grid, Vector2i.Zero, new Tile(1));
var entityOne = entManager.SpawnEntity("CollisionWakeTestItem", new MapCoordinates(Vector2.One * 2f, mapId));
entityOnePhysics = entManager.GetComponent<PhysicsComponent>(entityOne);
xform = entManager.GetComponent<TransformComponent>(entityOne);
Assert.That(xform.ParentUid == mapManager.GetMapEntityId(mapId));
mapSystem.TryGetMap(mapId, out var mapUid);
Assert.That(xform.ParentUid == mapUid);
var entityTwo = entManager.SpawnEntity("CollisionWakeTestItem", new EntityCoordinates(grid, new Vector2(0.5f, 0.5f)));
entityTwoPhysics = entManager.GetComponent<PhysicsComponent>(entityTwo);
Expand Down

0 comments on commit 2a1bcb6

Please sign in to comment.