1
0

RandomSpriteSystem.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Content.Shared.Decals;
  2. using Content.Shared.Random.Helpers;
  3. using Content.Shared.Sprite;
  4. using Robust.Shared.GameStates;
  5. using Robust.Shared.Prototypes;
  6. using Robust.Shared.Random;
  7. namespace Content.Server.Sprite;
  8. public sealed class RandomSpriteSystem: SharedRandomSpriteSystem
  9. {
  10. [Dependency] private readonly IPrototypeManager _prototype = default!;
  11. [Dependency] private readonly IRobustRandom _random = default!;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. SubscribeLocalEvent<RandomSpriteComponent, ComponentGetState>(OnGetState);
  16. SubscribeLocalEvent<RandomSpriteComponent, MapInitEvent>(OnMapInit);
  17. }
  18. private void OnMapInit(EntityUid uid, RandomSpriteComponent component, MapInitEvent args)
  19. {
  20. if (component.Selected.Count > 0)
  21. return;
  22. if (component.Available.Count == 0)
  23. return;
  24. var groups = new List<Dictionary<string, Dictionary<string, string?>>>();
  25. if (component.GetAllGroups)
  26. {
  27. groups = component.Available;
  28. }
  29. else
  30. {
  31. groups.Add(_random.Pick(component.Available));
  32. }
  33. component.Selected.EnsureCapacity(groups.Count);
  34. Color? previousColor = null;
  35. foreach (var group in groups)
  36. {
  37. foreach (var layer in group)
  38. {
  39. Color? color = null;
  40. var selectedState = _random.Pick(layer.Value);
  41. if (!string.IsNullOrEmpty(selectedState.Value))
  42. {
  43. if (selectedState.Value == $"Inherit")
  44. color = previousColor;
  45. else
  46. {
  47. color = _random.Pick(_prototype.Index<ColorPalettePrototype>(selectedState.Value).Colors.Values);
  48. previousColor = color;
  49. }
  50. }
  51. component.Selected.Add(layer.Key, (selectedState.Key, color));
  52. }
  53. }
  54. Dirty(uid, component);
  55. }
  56. private void OnGetState(EntityUid uid, RandomSpriteComponent component, ref ComponentGetState args)
  57. {
  58. args.State = new RandomSpriteColorComponentState()
  59. {
  60. Selected = component.Selected,
  61. };
  62. }
  63. }