HideLayerClothingSystem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Content.Shared.Clothing.Components;
  2. using Content.Shared.Humanoid;
  3. using Content.Shared.Inventory;
  4. using Robust.Shared.Timing;
  5. using Robust.Shared.Utility;
  6. namespace Content.Shared.Clothing.EntitySystems;
  7. public sealed class HideLayerClothingSystem : EntitySystem
  8. {
  9. [Dependency] private readonly SharedHumanoidAppearanceSystem _humanoid = default!;
  10. [Dependency] private readonly IGameTiming _timing = default!;
  11. public override void Initialize()
  12. {
  13. SubscribeLocalEvent<HideLayerClothingComponent, ClothingGotUnequippedEvent>(OnHideGotUnequipped);
  14. SubscribeLocalEvent<HideLayerClothingComponent, ClothingGotEquippedEvent>(OnHideGotEquipped);
  15. SubscribeLocalEvent<HideLayerClothingComponent, ItemMaskToggledEvent>(OnHideToggled);
  16. }
  17. private void OnHideToggled(Entity<HideLayerClothingComponent> ent, ref ItemMaskToggledEvent args)
  18. {
  19. if (args.Wearer != null)
  20. SetLayerVisibility(ent!, args.Wearer.Value, hideLayers: true);
  21. }
  22. private void OnHideGotEquipped(Entity<HideLayerClothingComponent> ent, ref ClothingGotEquippedEvent args)
  23. {
  24. SetLayerVisibility(ent!, args.Wearer, hideLayers: true);
  25. }
  26. private void OnHideGotUnequipped(Entity<HideLayerClothingComponent> ent, ref ClothingGotUnequippedEvent args)
  27. {
  28. SetLayerVisibility(ent!, args.Wearer, hideLayers: false);
  29. }
  30. private void SetLayerVisibility(
  31. Entity<HideLayerClothingComponent?, ClothingComponent?> clothing,
  32. Entity<HumanoidAppearanceComponent?> user,
  33. bool hideLayers)
  34. {
  35. if (_timing.ApplyingState)
  36. return;
  37. if (!Resolve(clothing.Owner, ref clothing.Comp1, ref clothing.Comp2))
  38. return;
  39. if (!Resolve(user.Owner, ref user.Comp))
  40. return;
  41. hideLayers &= IsEnabled(clothing!);
  42. var hideable = user.Comp.HideLayersOnEquip;
  43. var inSlot = clothing.Comp2.InSlotFlag ?? SlotFlags.NONE;
  44. // This method should only be getting called while the clothing is equipped (though possibly currently in
  45. // the process of getting unequipped).
  46. DebugTools.AssertNotNull(clothing.Comp2.InSlot);
  47. DebugTools.AssertNotNull(clothing.Comp2.InSlotFlag);
  48. DebugTools.AssertNotEqual(inSlot, SlotFlags.NONE);
  49. var dirty = false;
  50. // iterate the HideLayerClothingComponent's layers map and check that
  51. // the clothing is (or was)equipped in a matching slot.
  52. foreach (var (layer, validSlots) in clothing.Comp1.Layers)
  53. {
  54. if (!hideable.Contains(layer))
  55. continue;
  56. // Only update this layer if we are currently equipped to the relevant slot.
  57. if (validSlots.HasFlag(inSlot))
  58. _humanoid.SetLayerVisibility(user!, layer, !hideLayers);
  59. }
  60. // Fallback for obsolete field: assume we want to hide **all** layers, as long as we are equipped to any
  61. // relevant clothing slot
  62. #pragma warning disable CS0618 // Type or member is obsolete
  63. if (clothing.Comp1.Slots is { } slots && clothing.Comp2.Slots.HasFlag(inSlot))
  64. #pragma warning restore CS0618 // Type or member is obsolete
  65. {
  66. foreach (var layer in slots)
  67. {
  68. if (hideable.Contains(layer))
  69. _humanoid.SetLayerVisibility(user!, layer, !hideLayers, false);
  70. }
  71. }
  72. if (dirty)
  73. Dirty(user!);
  74. }
  75. private bool IsEnabled(Entity<HideLayerClothingComponent, ClothingComponent> clothing)
  76. {
  77. // TODO Generalize this
  78. // I.e., make this and mask component use some generic toggleable.
  79. if (!clothing.Comp1.HideOnToggle)
  80. return true;
  81. if (!TryComp(clothing, out MaskComponent? mask))
  82. return true;
  83. return !mask.IsToggled;
  84. }
  85. }