SharedBuckleSystem.Strap.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System.Linq;
  2. using Content.Shared.Buckle.Components;
  3. using Content.Shared.Construction;
  4. using Content.Shared.Destructible;
  5. using Content.Shared.Foldable;
  6. using Content.Shared.Storage;
  7. using Robust.Shared.Containers;
  8. namespace Content.Shared.Buckle;
  9. public abstract partial class SharedBuckleSystem
  10. {
  11. private void InitializeStrap()
  12. {
  13. SubscribeLocalEvent<StrapComponent, ComponentStartup>(OnStrapStartup);
  14. SubscribeLocalEvent<StrapComponent, ComponentShutdown>(OnStrapShutdown);
  15. SubscribeLocalEvent<StrapComponent, ComponentRemove>((e, c, _) => StrapRemoveAll(e, c));
  16. SubscribeLocalEvent<StrapComponent, ContainerGettingInsertedAttemptEvent>(OnStrapContainerGettingInsertedAttempt);
  17. SubscribeLocalEvent<StrapComponent, DestructionEventArgs>((e, c, _) => StrapRemoveAll(e, c));
  18. SubscribeLocalEvent<StrapComponent, BreakageEventArgs>((e, c, _) => StrapRemoveAll(e, c));
  19. SubscribeLocalEvent<StrapComponent, FoldAttemptEvent>(OnAttemptFold);
  20. SubscribeLocalEvent<StrapComponent, MachineDeconstructedEvent>((e, c, _) => StrapRemoveAll(e, c));
  21. }
  22. private void OnStrapStartup(EntityUid uid, StrapComponent component, ComponentStartup args)
  23. {
  24. Appearance.SetData(uid, StrapVisuals.State, component.BuckledEntities.Count != 0);
  25. }
  26. private void OnStrapShutdown(EntityUid uid, StrapComponent component, ComponentShutdown args)
  27. {
  28. if (!TerminatingOrDeleted(uid))
  29. StrapRemoveAll(uid, component);
  30. }
  31. private void OnStrapContainerGettingInsertedAttempt(EntityUid uid, StrapComponent component, ContainerGettingInsertedAttemptEvent args)
  32. {
  33. // If someone is attempting to put this item inside of a backpack, ensure that it has no entities strapped to it.
  34. if (args.Container.ID == StorageComponent.ContainerId && component.BuckledEntities.Count != 0)
  35. args.Cancel();
  36. }
  37. private void OnAttemptFold(EntityUid uid, StrapComponent component, ref FoldAttemptEvent args)
  38. {
  39. if (args.Cancelled)
  40. return;
  41. args.Cancelled = component.BuckledEntities.Count != 0;
  42. }
  43. /// <summary>
  44. /// Remove everything attached to the strap
  45. /// </summary>
  46. private void StrapRemoveAll(EntityUid uid, StrapComponent strapComp)
  47. {
  48. foreach (var entity in strapComp.BuckledEntities.ToArray())
  49. {
  50. Unbuckle(entity, entity);
  51. }
  52. }
  53. private bool StrapHasSpace(EntityUid strapUid, BuckleComponent buckleComp, StrapComponent? strapComp = null)
  54. {
  55. if (!Resolve(strapUid, ref strapComp, false))
  56. return false;
  57. var avail = strapComp.Size;
  58. foreach (var buckle in strapComp.BuckledEntities)
  59. {
  60. avail -= CompOrNull<BuckleComponent>(buckle)?.Size ?? 0;
  61. }
  62. return avail >= buckleComp.Size;
  63. }
  64. /// <summary>
  65. /// Sets the enabled field in the strap component to a value
  66. /// </summary>
  67. public void StrapSetEnabled(EntityUid strapUid, bool enabled, StrapComponent? strapComp = null)
  68. {
  69. if (!Resolve(strapUid, ref strapComp, false) ||
  70. strapComp.Enabled == enabled)
  71. return;
  72. strapComp.Enabled = enabled;
  73. Dirty(strapUid, strapComp);
  74. if (!enabled)
  75. StrapRemoveAll(strapUid, strapComp);
  76. }
  77. }