MagbootsSystem.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Content.Shared.Actions;
  2. using Content.Shared.Alert;
  3. using Content.Shared.Atmos.Components;
  4. using Content.Shared.Clothing.EntitySystems;
  5. using Content.Shared.Gravity;
  6. using Content.Shared.Inventory;
  7. using Content.Shared.Item;
  8. using Content.Shared.Item.ItemToggle;
  9. using Content.Shared.Item.ItemToggle.Components;
  10. using Robust.Shared.Containers;
  11. namespace Content.Shared.Clothing;
  12. public sealed class SharedMagbootsSystem : EntitySystem
  13. {
  14. [Dependency] private readonly AlertsSystem _alerts = default!;
  15. [Dependency] private readonly ClothingSystem _clothing = default!;
  16. [Dependency] private readonly InventorySystem _inventory = default!;
  17. [Dependency] private readonly ItemToggleSystem _toggle = default!;
  18. [Dependency] private readonly SharedContainerSystem _container = default!;
  19. [Dependency] private readonly SharedGravitySystem _gravity = default!;
  20. [Dependency] private readonly SharedItemSystem _item = default!;
  21. public override void Initialize()
  22. {
  23. base.Initialize();
  24. SubscribeLocalEvent<MagbootsComponent, ItemToggledEvent>(OnToggled);
  25. SubscribeLocalEvent<MagbootsComponent, ClothingGotEquippedEvent>(OnGotEquipped);
  26. SubscribeLocalEvent<MagbootsComponent, ClothingGotUnequippedEvent>(OnGotUnequipped);
  27. SubscribeLocalEvent<MagbootsComponent, IsWeightlessEvent>(OnIsWeightless);
  28. SubscribeLocalEvent<MagbootsComponent, InventoryRelayedEvent<IsWeightlessEvent>>(OnIsWeightless);
  29. }
  30. private void OnToggled(Entity<MagbootsComponent> ent, ref ItemToggledEvent args)
  31. {
  32. var (uid, comp) = ent;
  33. // only stick to the floor if being worn in the correct slot
  34. if (_container.TryGetContainingContainer((uid, null, null), out var container) &&
  35. _inventory.TryGetSlotEntity(container.Owner, comp.Slot, out var worn)
  36. && uid == worn)
  37. {
  38. UpdateMagbootEffects(container.Owner, ent, args.Activated);
  39. }
  40. var prefix = args.Activated ? "on" : null;
  41. _item.SetHeldPrefix(ent, prefix);
  42. _clothing.SetEquippedPrefix(ent, prefix);
  43. }
  44. private void OnGotUnequipped(Entity<MagbootsComponent> ent, ref ClothingGotUnequippedEvent args)
  45. {
  46. UpdateMagbootEffects(args.Wearer, ent, false);
  47. }
  48. private void OnGotEquipped(Entity<MagbootsComponent> ent, ref ClothingGotEquippedEvent args)
  49. {
  50. UpdateMagbootEffects(args.Wearer, ent, _toggle.IsActivated(ent.Owner));
  51. }
  52. public void UpdateMagbootEffects(EntityUid user, Entity<MagbootsComponent> ent, bool state)
  53. {
  54. // TODO: public api for this and add access
  55. if (TryComp<MovedByPressureComponent>(user, out var moved))
  56. moved.Enabled = !state;
  57. if (state)
  58. _alerts.ShowAlert(user, ent.Comp.MagbootsAlert);
  59. else
  60. _alerts.ClearAlert(user, ent.Comp.MagbootsAlert);
  61. }
  62. private void OnIsWeightless(Entity<MagbootsComponent> ent, ref IsWeightlessEvent args)
  63. {
  64. if (args.Handled || !_toggle.IsActivated(ent.Owner))
  65. return;
  66. // do not cancel weightlessness if the person is in off-grid.
  67. if (ent.Comp.RequiresGrid && !_gravity.EntityOnGravitySupportingGridOrMap(ent.Owner))
  68. return;
  69. args.IsWeightless = false;
  70. args.Handled = true;
  71. }
  72. private void OnIsWeightless(Entity<MagbootsComponent> ent, ref InventoryRelayedEvent<IsWeightlessEvent> args)
  73. {
  74. OnIsWeightless(ent, ref args.Args);
  75. }
  76. }