ReplaceFloorOnSpawnSystem.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Robust.Shared.Map;
  2. using Robust.Shared.Map.Components;
  3. using Robust.Shared.Prototypes;
  4. using Robust.Shared.Random;
  5. namespace Content.Shared.Tiles;
  6. public sealed class ReplaceFloorOnSpawnSystem : EntitySystem
  7. {
  8. [Dependency] private readonly ITileDefinitionManager _tile = default!;
  9. [Dependency] private readonly IPrototypeManager _prototype = default!;
  10. [Dependency] private readonly IRobustRandom _random = default!;
  11. [Dependency] private readonly SharedMapSystem _map = default!;
  12. /// <inheritdoc/>
  13. public override void Initialize()
  14. {
  15. SubscribeLocalEvent<ReplaceFloorOnSpawnComponent, MapInitEvent>(OnMapInit);
  16. }
  17. private void OnMapInit(Entity<ReplaceFloorOnSpawnComponent> ent, ref MapInitEvent args)
  18. {
  19. var xform = Transform(ent);
  20. if (xform.GridUid is not { } grid || !TryComp<MapGridComponent>(grid, out var gridComp))
  21. return;
  22. if (ent.Comp.ReplaceableTiles != null && ent.Comp.ReplaceableTiles.Count == 0)
  23. return;
  24. var tileIndices = _map.LocalToTile(grid, gridComp, xform.Coordinates);
  25. foreach (var offset in ent.Comp.Offsets)
  26. {
  27. var actualIndices = tileIndices + offset;
  28. if (!_map.TryGetTileRef(grid, gridComp, actualIndices, out var tile))
  29. continue;
  30. if (ent.Comp.ReplaceableTiles != null &&
  31. !tile.Tile.IsEmpty &&
  32. !ent.Comp.ReplaceableTiles.Contains(_tile[tile.Tile.TypeId].ID))
  33. continue;
  34. var tileToSet = _random.Pick(ent.Comp.ReplacementTiles);
  35. _map.SetTile(grid, gridComp, tile.GridIndices, new Tile(_prototype.Index(tileToSet).TileId));
  36. }
  37. }
  38. }