1
0

PlaceableSurfaceSystem.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Numerics;
  2. using Content.Shared.Hands.EntitySystems;
  3. using Content.Shared.Interaction;
  4. using Content.Shared.Storage;
  5. using Content.Shared.Storage.Components;
  6. namespace Content.Shared.Placeable;
  7. public sealed class PlaceableSurfaceSystem : EntitySystem
  8. {
  9. [Dependency] private readonly SharedHandsSystem _handsSystem = default!;
  10. [Dependency] private readonly SharedTransformSystem _transformSystem = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<PlaceableSurfaceComponent, AfterInteractUsingEvent>(OnAfterInteractUsing);
  15. SubscribeLocalEvent<PlaceableSurfaceComponent, StorageInteractUsingAttemptEvent>(OnStorageInteractUsingAttempt);
  16. SubscribeLocalEvent<PlaceableSurfaceComponent, StorageAfterOpenEvent>(OnStorageAfterOpen);
  17. SubscribeLocalEvent<PlaceableSurfaceComponent, StorageAfterCloseEvent>(OnStorageAfterClose);
  18. }
  19. public void SetPlaceable(EntityUid uid, bool isPlaceable, PlaceableSurfaceComponent? surface = null)
  20. {
  21. if (!Resolve(uid, ref surface, false))
  22. return;
  23. if (surface.IsPlaceable == isPlaceable)
  24. return;
  25. surface.IsPlaceable = isPlaceable;
  26. Dirty(uid, surface);
  27. }
  28. public void SetPlaceCentered(EntityUid uid, bool placeCentered, PlaceableSurfaceComponent? surface = null)
  29. {
  30. if (!Resolve(uid, ref surface))
  31. return;
  32. surface.PlaceCentered = placeCentered;
  33. Dirty(uid, surface);
  34. }
  35. public void SetPositionOffset(EntityUid uid, Vector2 offset, PlaceableSurfaceComponent? surface = null)
  36. {
  37. if (!Resolve(uid, ref surface))
  38. return;
  39. surface.PositionOffset = offset;
  40. Dirty(uid, surface);
  41. }
  42. private void OnAfterInteractUsing(EntityUid uid, PlaceableSurfaceComponent surface, AfterInteractUsingEvent args)
  43. {
  44. if (args.Handled || !args.CanReach)
  45. return;
  46. if (!surface.IsPlaceable)
  47. return;
  48. // 99% of the time they want to dump the stuff inside on the table, they can manually place with q if they really need to.
  49. // Just causes prediction CBT otherwise.
  50. if (HasComp<DumpableComponent>(args.Used))
  51. return;
  52. if (!_handsSystem.TryDrop(args.User, args.Used))
  53. return;
  54. _transformSystem.SetCoordinates(args.Used,
  55. surface.PlaceCentered ? Transform(uid).Coordinates.Offset(surface.PositionOffset) : args.ClickLocation);
  56. args.Handled = true;
  57. }
  58. private void OnStorageInteractUsingAttempt(Entity<PlaceableSurfaceComponent> ent, ref StorageInteractUsingAttemptEvent args)
  59. {
  60. args.Cancelled = true;
  61. }
  62. private void OnStorageAfterOpen(Entity<PlaceableSurfaceComponent> ent, ref StorageAfterOpenEvent args)
  63. {
  64. SetPlaceable(ent.Owner, true, ent.Comp);
  65. }
  66. private void OnStorageAfterClose(Entity<PlaceableSurfaceComponent> ent, ref StorageAfterCloseEvent args)
  67. {
  68. SetPlaceable(ent.Owner, false, ent.Comp);
  69. }
  70. }