SharedChameleonClothingSystem.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using Content.Shared.Access.Components;
  2. using Content.Shared.Clothing.Components;
  3. using Content.Shared.Contraband;
  4. using Content.Shared.Inventory;
  5. using Content.Shared.Inventory.Events;
  6. using Content.Shared.Item;
  7. using Content.Shared.Tag;
  8. using Content.Shared.Verbs;
  9. using Robust.Shared.Prototypes;
  10. using Robust.Shared.Utility;
  11. namespace Content.Shared.Clothing.EntitySystems;
  12. public abstract class SharedChameleonClothingSystem : EntitySystem
  13. {
  14. [Dependency] private readonly IComponentFactory _factory = default!;
  15. [Dependency] private readonly IPrototypeManager _proto = default!;
  16. [Dependency] private readonly ClothingSystem _clothingSystem = default!;
  17. [Dependency] private readonly ContrabandSystem _contraband = default!;
  18. [Dependency] private readonly MetaDataSystem _metaData = default!;
  19. [Dependency] private readonly SharedItemSystem _itemSystem = default!;
  20. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  21. [Dependency] private readonly TagSystem _tag = default!;
  22. [Dependency] protected readonly SharedUserInterfaceSystem UI = default!;
  23. public override void Initialize()
  24. {
  25. base.Initialize();
  26. SubscribeLocalEvent<ChameleonClothingComponent, GotEquippedEvent>(OnGotEquipped);
  27. SubscribeLocalEvent<ChameleonClothingComponent, GotUnequippedEvent>(OnGotUnequipped);
  28. SubscribeLocalEvent<ChameleonClothingComponent, GetVerbsEvent<InteractionVerb>>(OnVerb);
  29. }
  30. private void OnGotEquipped(EntityUid uid, ChameleonClothingComponent component, GotEquippedEvent args)
  31. {
  32. component.User = args.Equipee;
  33. }
  34. private void OnGotUnequipped(EntityUid uid, ChameleonClothingComponent component, GotUnequippedEvent args)
  35. {
  36. component.User = null;
  37. }
  38. // Updates chameleon visuals and meta information.
  39. // This function is called on a server after user selected new outfit.
  40. // And after that on a client after state was updated.
  41. // This 100% makes sure that server and client have exactly same data.
  42. protected void UpdateVisuals(EntityUid uid, ChameleonClothingComponent component)
  43. {
  44. if (string.IsNullOrEmpty(component.Default) ||
  45. !_proto.TryIndex(component.Default, out EntityPrototype? proto))
  46. return;
  47. // world sprite icon
  48. UpdateSprite(uid, proto);
  49. // copy name and description, unless its an ID card
  50. if (!HasComp<IdCardComponent>(uid))
  51. {
  52. var meta = MetaData(uid);
  53. _metaData.SetEntityName(uid, proto.Name, meta);
  54. _metaData.SetEntityDescription(uid, proto.Description, meta);
  55. }
  56. // item sprite logic
  57. if (TryComp(uid, out ItemComponent? item) &&
  58. proto.TryGetComponent(out ItemComponent? otherItem, _factory))
  59. {
  60. _itemSystem.CopyVisuals(uid, otherItem, item);
  61. }
  62. // clothing sprite logic
  63. if (TryComp(uid, out ClothingComponent? clothing) &&
  64. proto.TryGetComponent("Clothing", out ClothingComponent? otherClothing))
  65. {
  66. _clothingSystem.CopyVisuals(uid, otherClothing, clothing);
  67. }
  68. // appearance data logic
  69. if (TryComp(uid, out AppearanceComponent? appearance) &&
  70. proto.TryGetComponent("Appearance", out AppearanceComponent? appearanceOther))
  71. {
  72. _appearance.AppendData(appearanceOther, uid);
  73. Dirty(uid, appearance);
  74. }
  75. // properly mark contraband
  76. if (proto.TryGetComponent("Contraband", out ContrabandComponent? contra))
  77. {
  78. EnsureComp<ContrabandComponent>(uid, out var current);
  79. _contraband.CopyDetails(uid, contra, current);
  80. }
  81. else
  82. {
  83. RemComp<ContrabandComponent>(uid);
  84. }
  85. }
  86. private void OnVerb(Entity<ChameleonClothingComponent> ent, ref GetVerbsEvent<InteractionVerb> args)
  87. {
  88. if (!args.CanAccess || !args.CanInteract || ent.Comp.User != args.User)
  89. return;
  90. // Can't pass args from a ref event inside of lambdas
  91. var user = args.User;
  92. args.Verbs.Add(new InteractionVerb()
  93. {
  94. Text = Loc.GetString("chameleon-component-verb-text"),
  95. Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/settings.svg.192dpi.png")),
  96. Act = () => UI.TryToggleUi(ent.Owner, ChameleonUiKey.Key, user)
  97. });
  98. }
  99. protected virtual void UpdateSprite(EntityUid uid, EntityPrototype proto) { }
  100. /// <summary>
  101. /// Check if this entity prototype is valid target for chameleon item.
  102. /// </summary>
  103. public bool IsValidTarget(EntityPrototype proto, SlotFlags chameleonSlot = SlotFlags.NONE, string? requiredTag = null)
  104. {
  105. // check if entity is valid
  106. if (proto.Abstract || proto.HideSpawnMenu)
  107. return false;
  108. // check if it is marked as valid chameleon target
  109. if (!proto.TryGetComponent(out TagComponent? tag, _factory) || !_tag.HasTag(tag, "WhitelistChameleon"))
  110. return false;
  111. if (requiredTag != null && !_tag.HasTag(tag, requiredTag))
  112. return false;
  113. // check if it's valid clothing
  114. if (!proto.TryGetComponent("Clothing", out ClothingComponent? clothing))
  115. return false;
  116. if (!clothing.Slots.HasFlag(chameleonSlot))
  117. return false;
  118. return true;
  119. }
  120. }