HumanoidAppearanceSystem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Content.Shared.Humanoid;
  2. using Content.Shared.Humanoid.Markings;
  3. using Content.Shared.Humanoid.Prototypes;
  4. using Content.Shared.Preferences;
  5. using Content.Shared.Verbs;
  6. using Robust.Shared.GameObjects.Components.Localization;
  7. namespace Content.Server.Humanoid;
  8. public sealed partial class HumanoidAppearanceSystem : SharedHumanoidAppearanceSystem
  9. {
  10. [Dependency] private readonly MarkingManager _markingManager = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<HumanoidAppearanceComponent, HumanoidMarkingModifierMarkingSetMessage>(OnMarkingsSet);
  15. SubscribeLocalEvent<HumanoidAppearanceComponent, HumanoidMarkingModifierBaseLayersSetMessage>(OnBaseLayersSet);
  16. SubscribeLocalEvent<HumanoidAppearanceComponent, GetVerbsEvent<Verb>>(OnVerbsRequest);
  17. }
  18. /// <summary>
  19. /// Removes a marking from a humanoid by ID.
  20. /// </summary>
  21. /// <param name="uid">Humanoid mob's UID</param>
  22. /// <param name="marking">The marking to try and remove.</param>
  23. /// <param name="sync">Whether to immediately sync this to the humanoid</param>
  24. /// <param name="humanoid">Humanoid component of the entity</param>
  25. public void RemoveMarking(EntityUid uid, string marking, bool sync = true, HumanoidAppearanceComponent? humanoid = null)
  26. {
  27. if (!Resolve(uid, ref humanoid)
  28. || !_markingManager.Markings.TryGetValue(marking, out var prototype))
  29. {
  30. return;
  31. }
  32. humanoid.MarkingSet.Remove(prototype.MarkingCategory, marking);
  33. if (sync)
  34. Dirty(uid, humanoid);
  35. }
  36. /// <summary>
  37. /// Removes a marking from a humanoid by category and index.
  38. /// </summary>
  39. /// <param name="uid">Humanoid mob's UID</param>
  40. /// <param name="category">Category of the marking</param>
  41. /// <param name="index">Index of the marking</param>
  42. /// <param name="humanoid">Humanoid component of the entity</param>
  43. public void RemoveMarking(EntityUid uid, MarkingCategories category, int index, HumanoidAppearanceComponent? humanoid = null)
  44. {
  45. if (index < 0
  46. || !Resolve(uid, ref humanoid)
  47. || !humanoid.MarkingSet.TryGetCategory(category, out var markings)
  48. || index >= markings.Count)
  49. {
  50. return;
  51. }
  52. humanoid.MarkingSet.Remove(category, index);
  53. Dirty(uid, humanoid);
  54. }
  55. /// <summary>
  56. /// Sets the marking ID of the humanoid in a category at an index in the category's list.
  57. /// </summary>
  58. /// <param name="uid">Humanoid mob's UID</param>
  59. /// <param name="category">Category of the marking</param>
  60. /// <param name="index">Index of the marking</param>
  61. /// <param name="markingId">The marking ID to use</param>
  62. /// <param name="humanoid">Humanoid component of the entity</param>
  63. public void SetMarkingId(EntityUid uid, MarkingCategories category, int index, string markingId, HumanoidAppearanceComponent? humanoid = null)
  64. {
  65. if (index < 0
  66. || !_markingManager.MarkingsByCategory(category).TryGetValue(markingId, out var markingPrototype)
  67. || !Resolve(uid, ref humanoid)
  68. || !humanoid.MarkingSet.TryGetCategory(category, out var markings)
  69. || index >= markings.Count)
  70. {
  71. return;
  72. }
  73. var marking = markingPrototype.AsMarking();
  74. for (var i = 0; i < marking.MarkingColors.Count && i < markings[index].MarkingColors.Count; i++)
  75. {
  76. marking.SetColor(i, markings[index].MarkingColors[i]);
  77. }
  78. humanoid.MarkingSet.Replace(category, index, marking);
  79. Dirty(uid, humanoid);
  80. }
  81. /// <summary>
  82. /// Sets the marking colors of the humanoid in a category at an index in the category's list.
  83. /// </summary>
  84. /// <param name="uid">Humanoid mob's UID</param>
  85. /// <param name="category">Category of the marking</param>
  86. /// <param name="index">Index of the marking</param>
  87. /// <param name="colors">The marking colors to use</param>
  88. /// <param name="humanoid">Humanoid component of the entity</param>
  89. public void SetMarkingColor(EntityUid uid, MarkingCategories category, int index, List<Color> colors,
  90. HumanoidAppearanceComponent? humanoid = null)
  91. {
  92. if (index < 0
  93. || !Resolve(uid, ref humanoid)
  94. || !humanoid.MarkingSet.TryGetCategory(category, out var markings)
  95. || index >= markings.Count)
  96. {
  97. return;
  98. }
  99. for (var i = 0; i < markings[index].MarkingColors.Count && i < colors.Count; i++)
  100. {
  101. markings[index].SetColor(i, colors[i]);
  102. }
  103. Dirty(uid, humanoid);
  104. }
  105. }