1
0

ToggleableLightVisualsSystem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Content.Client.Clothing;
  2. using Content.Client.Items.Systems;
  3. using Content.Shared.Clothing;
  4. using Content.Shared.Hands;
  5. using Content.Shared.Inventory;
  6. using Content.Shared.Item;
  7. using Content.Shared.Toggleable;
  8. using Robust.Client.GameObjects;
  9. using Robust.Shared.Utility;
  10. using System.Linq;
  11. namespace Content.Client.Toggleable;
  12. public sealed class ToggleableLightVisualsSystem : VisualizerSystem<ToggleableLightVisualsComponent>
  13. {
  14. [Dependency] private readonly SharedItemSystem _itemSys = default!;
  15. [Dependency] private readonly SharedPointLightSystem _lights = default!;
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. SubscribeLocalEvent<ToggleableLightVisualsComponent, GetInhandVisualsEvent>(OnGetHeldVisuals, after: new[] { typeof(ItemSystem) });
  20. SubscribeLocalEvent<ToggleableLightVisualsComponent, GetEquipmentVisualsEvent>(OnGetEquipmentVisuals, after: new[] { typeof(ClientClothingSystem) });
  21. }
  22. protected override void OnAppearanceChange(EntityUid uid, ToggleableLightVisualsComponent component, ref AppearanceChangeEvent args)
  23. {
  24. if (!AppearanceSystem.TryGetData<bool>(uid, ToggleableLightVisuals.Enabled, out var enabled, args.Component))
  25. return;
  26. var modulate = AppearanceSystem.TryGetData<Color>(uid, ToggleableLightVisuals.Color, out var color, args.Component);
  27. // Update the item's sprite
  28. if (args.Sprite != null && component.SpriteLayer != null && args.Sprite.LayerMapTryGet(component.SpriteLayer, out var layer))
  29. {
  30. args.Sprite.LayerSetVisible(layer, enabled);
  31. if (modulate)
  32. args.Sprite.LayerSetColor(layer, color);
  33. }
  34. // Update any point-lights
  35. if (TryComp(uid, out PointLightComponent? light))
  36. {
  37. DebugTools.Assert(!light.NetSyncEnabled, "light visualizers require point lights without net-sync");
  38. _lights.SetEnabled(uid, enabled, light);
  39. if (enabled && modulate)
  40. {
  41. _lights.SetColor(uid, color, light);
  42. }
  43. }
  44. // update clothing & in-hand visuals.
  45. _itemSys.VisualsChanged(uid);
  46. }
  47. /// <summary>
  48. /// Add the unshaded light overlays to any clothing sprites.
  49. /// </summary>
  50. private void OnGetEquipmentVisuals(EntityUid uid, ToggleableLightVisualsComponent component, GetEquipmentVisualsEvent args)
  51. {
  52. if (!TryComp(uid, out AppearanceComponent? appearance)
  53. || !AppearanceSystem.TryGetData<bool>(uid, ToggleableLightVisuals.Enabled, out var enabled, appearance)
  54. || !enabled)
  55. return;
  56. if (!TryComp(args.Equipee, out InventoryComponent? inventory))
  57. return;
  58. List<PrototypeLayerData>? layers = null;
  59. // attempt to get species specific data
  60. if (inventory.SpeciesId != null)
  61. component.ClothingVisuals.TryGetValue($"{args.Slot}-{inventory.SpeciesId}", out layers);
  62. // No species specific data. Try to default to generic data.
  63. if (layers == null && !component.ClothingVisuals.TryGetValue(args.Slot, out layers))
  64. return;
  65. var modulate = AppearanceSystem.TryGetData<Color>(uid, ToggleableLightVisuals.Color, out var color, appearance);
  66. var i = 0;
  67. foreach (var layer in layers)
  68. {
  69. var key = layer.MapKeys?.FirstOrDefault();
  70. if (key == null)
  71. {
  72. key = i == 0 ? $"{args.Slot}-toggle" : $"{args.Slot}-toggle-{i}";
  73. i++;
  74. }
  75. if (modulate)
  76. layer.Color = color;
  77. args.Layers.Add((key, layer));
  78. }
  79. }
  80. private void OnGetHeldVisuals(EntityUid uid, ToggleableLightVisualsComponent component, GetInhandVisualsEvent args)
  81. {
  82. if (!TryComp(uid, out AppearanceComponent? appearance)
  83. || !AppearanceSystem.TryGetData<bool>(uid, ToggleableLightVisuals.Enabled, out var enabled, appearance)
  84. || !enabled)
  85. return;
  86. if (!component.InhandVisuals.TryGetValue(args.Location, out var layers))
  87. return;
  88. var modulate = AppearanceSystem.TryGetData<Color>(uid, ToggleableLightVisuals.Color, out var color, appearance);
  89. var i = 0;
  90. var defaultKey = $"inhand-{args.Location.ToString().ToLowerInvariant()}-toggle";
  91. foreach (var layer in layers)
  92. {
  93. var key = layer.MapKeys?.FirstOrDefault();
  94. if (key == null)
  95. {
  96. key = i == 0 ? defaultKey : $"{defaultKey}-{i}";
  97. i++;
  98. }
  99. if (modulate)
  100. layer.Color = color;
  101. args.Layers.Add((key, layer));
  102. }
  103. }
  104. }