CatStation/Content.Shared/Tiles/ReplaceFloorOnSpawnSystem.cs
Whatstone a8453723d8
Cleanup: ProtoMan Part 1 - remove Shared EntitySystem IPrototypeManager instances (#44386)
* Remove Shared System IPrototypeManager instances

* Remove RecipeManager IPrototypeManager instance

* Fix ItemSystem IPrototypeManager refs, use TryComp

* SharedItemSystem changes, EntWhitelist CompName

* HasComponent->HasComp
2026-06-29 10:18:44 +00:00

46 lines
1.6 KiB
C#

using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Random;
namespace Content.Shared.Tiles;
public sealed partial class ReplaceFloorOnSpawnSystem : EntitySystem
{
[Dependency] private ITileDefinitionManager _tile = default!;
[Dependency] private IRobustRandom _random = default!;
[Dependency] private SharedMapSystem _map = default!;
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<ReplaceFloorOnSpawnComponent, MapInitEvent>(OnMapInit);
}
private void OnMapInit(Entity<ReplaceFloorOnSpawnComponent> ent, ref MapInitEvent args)
{
var xform = Transform(ent);
if (xform.GridUid is not { } grid || !TryComp<MapGridComponent>(grid, out var gridComp))
return;
if (ent.Comp.ReplaceableTiles != null && ent.Comp.ReplaceableTiles.Count == 0)
return;
var tileIndices = _map.LocalToTile(grid, gridComp, xform.Coordinates);
foreach (var offset in ent.Comp.Offsets)
{
var actualIndices = tileIndices + offset;
if (!_map.TryGetTileRef(grid, gridComp, actualIndices, out var tile))
continue;
if (ent.Comp.ReplaceableTiles != null &&
!tile.Tile.IsEmpty &&
!ent.Comp.ReplaceableTiles.Contains(_tile[tile.Tile.TypeId].ID))
continue;
var tileToSet = _random.Pick(ent.Comp.ReplacementTiles);
_map.SetTile(grid, gridComp, tile.GridIndices, new Tile(ProtoMan.Index(tileToSet).TileId));
}
}
}