ChameleonClothingSystem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Linq;
  2. using Content.Client.PDA;
  3. using Content.Shared.Clothing.Components;
  4. using Content.Shared.Clothing.EntitySystems;
  5. using Content.Shared.Inventory;
  6. using Robust.Client.GameObjects;
  7. using Robust.Shared.Prototypes;
  8. namespace Content.Client.Clothing.Systems;
  9. // All valid items for chameleon are calculated on client startup and stored in dictionary.
  10. public sealed class ChameleonClothingSystem : SharedChameleonClothingSystem
  11. {
  12. [Dependency] private readonly IPrototypeManager _proto = default!;
  13. [Dependency] private readonly IComponentFactory _factory = default!;
  14. private static readonly SlotFlags[] IgnoredSlots =
  15. {
  16. SlotFlags.All,
  17. SlotFlags.PREVENTEQUIP,
  18. SlotFlags.NONE
  19. };
  20. private static readonly SlotFlags[] Slots = Enum.GetValues<SlotFlags>().Except(IgnoredSlots).ToArray();
  21. private readonly Dictionary<SlotFlags, List<string>> _data = new();
  22. public override void Initialize()
  23. {
  24. base.Initialize();
  25. SubscribeLocalEvent<ChameleonClothingComponent, AfterAutoHandleStateEvent>(HandleState);
  26. PrepareAllVariants();
  27. SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnProtoReloaded);
  28. }
  29. private void OnProtoReloaded(PrototypesReloadedEventArgs args)
  30. {
  31. if (args.WasModified<EntityPrototype>())
  32. PrepareAllVariants();
  33. }
  34. private void HandleState(EntityUid uid, ChameleonClothingComponent component, ref AfterAutoHandleStateEvent args)
  35. {
  36. UpdateVisuals(uid, component);
  37. }
  38. protected override void UpdateSprite(EntityUid uid, EntityPrototype proto)
  39. {
  40. base.UpdateSprite(uid, proto);
  41. if (TryComp(uid, out SpriteComponent? sprite)
  42. && proto.TryGetComponent(out SpriteComponent? otherSprite, _factory))
  43. {
  44. sprite.CopyFrom(otherSprite);
  45. }
  46. // Edgecase for PDAs to include visuals when UI is open
  47. if (TryComp(uid, out PdaBorderColorComponent? borderColor)
  48. && proto.TryGetComponent(out PdaBorderColorComponent? otherBorderColor, _factory))
  49. {
  50. borderColor.BorderColor = otherBorderColor.BorderColor;
  51. borderColor.AccentHColor = otherBorderColor.AccentHColor;
  52. borderColor.AccentVColor = otherBorderColor.AccentVColor;
  53. }
  54. }
  55. /// <summary>
  56. /// Get a list of valid chameleon targets for these slots.
  57. /// </summary>
  58. public IEnumerable<string> GetValidTargets(SlotFlags slot)
  59. {
  60. var set = new HashSet<string>();
  61. foreach (var availableSlot in _data.Keys)
  62. {
  63. if (slot.HasFlag(availableSlot))
  64. {
  65. set.UnionWith(_data[availableSlot]);
  66. }
  67. }
  68. return set;
  69. }
  70. private void PrepareAllVariants()
  71. {
  72. _data.Clear();
  73. var prototypes = _proto.EnumeratePrototypes<EntityPrototype>();
  74. foreach (var proto in prototypes)
  75. {
  76. // check if this is valid clothing
  77. if (!IsValidTarget(proto))
  78. continue;
  79. if (!proto.TryGetComponent(out ClothingComponent? item, _factory))
  80. continue;
  81. // sort item by their slot flags
  82. // one item can be placed in several buckets
  83. foreach (var slot in Slots)
  84. {
  85. if (!item.Slots.HasFlag(slot))
  86. continue;
  87. if (!_data.ContainsKey(slot))
  88. {
  89. _data.Add(slot, new List<string>());
  90. }
  91. _data[slot].Add(proto.ID);
  92. }
  93. }
  94. }
  95. }