1
0

SpawnAfterInteractSystem.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Content.Server.Engineering.Components;
  2. using Content.Server.Stack;
  3. using Content.Shared.Coordinates.Helpers;
  4. using Content.Shared.DoAfter;
  5. using Content.Shared.Interaction;
  6. using Content.Shared.Maps;
  7. using Content.Shared.Physics;
  8. using Content.Shared.Stacks;
  9. using JetBrains.Annotations;
  10. using Robust.Shared.Map.Components;
  11. using Content.Shared.Tag;
  12. namespace Content.Server.Engineering.EntitySystems
  13. {
  14. [UsedImplicitly]
  15. public sealed class SpawnAfterInteractSystem : EntitySystem
  16. {
  17. [Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
  18. [Dependency] private readonly StackSystem _stackSystem = default!;
  19. [Dependency] private readonly TurfSystem _turfSystem = default!;
  20. [Dependency] private readonly TagSystem _tagSystem = default!;
  21. [Dependency] private readonly EntityLookupSystem _lookup = default!;
  22. public override void Initialize()
  23. {
  24. base.Initialize();
  25. SubscribeLocalEvent<SpawnAfterInteractComponent, AfterInteractEvent>(HandleAfterInteract);
  26. }
  27. private async void HandleAfterInteract(EntityUid uid, SpawnAfterInteractComponent component, AfterInteractEvent args)
  28. {
  29. if (!args.CanReach && !component.IgnoreDistance)
  30. return;
  31. if (string.IsNullOrEmpty(component.Prototype))
  32. return;
  33. if (!TryComp<MapGridComponent>(args.ClickLocation.GetGridUid(EntityManager), out var grid))
  34. return;
  35. if (!grid.TryGetTileRef(args.ClickLocation, out var tileRef))
  36. return;
  37. bool IsTileClear()
  38. {
  39. return tileRef.Tile.IsEmpty == false && !_turfSystem.IsTileBlocked(tileRef, CollisionGroup.MobMask);
  40. }
  41. if (!IsTileClear())
  42. return;
  43. if (component.DoAfterTime > 0)
  44. {
  45. var doAfterArgs = new DoAfterArgs(EntityManager, args.User, component.DoAfterTime, new AwaitedDoAfterEvent(), null)
  46. {
  47. BreakOnMove = true,
  48. };
  49. var result = await _doAfterSystem.WaitDoAfter(doAfterArgs);
  50. if (result != DoAfterStatus.Finished)
  51. return;
  52. }
  53. if (component.Deleted || !IsTileClear())
  54. return;
  55. if (EntityManager.TryGetComponent(uid, out StackComponent? stackComp)
  56. && component.RemoveOnInteract && !_stackSystem.Use(uid, 1, stackComp))
  57. {
  58. return;
  59. }
  60. EntityManager.SpawnEntity(component.Prototype, args.ClickLocation.SnapToGrid(grid));
  61. if (_tagSystem.HasTag(uid, "Bridging"))
  62. {
  63. var loc_entities = _lookup.GetEntitiesInRange(uid, 1);
  64. foreach (var ent in loc_entities)
  65. {
  66. if (_tagSystem.HasTag(ent, "Water") && _tagSystem.HasTag(ent, "Bridgeable"))
  67. {
  68. var coordinates = Transform(ent).Coordinates;
  69. QueueDel(ent);
  70. EntityManager.SpawnEntity("FloorWaterEntity", coordinates);
  71. }
  72. }
  73. }
  74. if (component.RemoveOnInteract && stackComp == null)
  75. TryQueueDel(uid);
  76. }
  77. }
  78. }