1
0

BarSignSystem.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Linq;
  2. using Robust.Shared.Prototypes;
  3. using Robust.Shared.Random;
  4. namespace Content.Shared.BarSign;
  5. public sealed class BarSignSystem : EntitySystem
  6. {
  7. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  8. [Dependency] private readonly IRobustRandom _random = default!;
  9. [Dependency] private readonly MetaDataSystem _metaData = default!;
  10. public override void Initialize()
  11. {
  12. SubscribeLocalEvent<BarSignComponent, MapInitEvent>(OnMapInit);
  13. Subs.BuiEvents<BarSignComponent>(BarSignUiKey.Key,
  14. subs =>
  15. {
  16. subs.Event<SetBarSignMessage>(OnSetBarSignMessage);
  17. });
  18. }
  19. private void OnMapInit(Entity<BarSignComponent> ent, ref MapInitEvent args)
  20. {
  21. if (ent.Comp.Current != null)
  22. return;
  23. var newPrototype = _random.Pick(GetAllBarSigns(_prototypeManager));
  24. SetBarSign(ent, newPrototype);
  25. }
  26. private void OnSetBarSignMessage(Entity<BarSignComponent> ent, ref SetBarSignMessage args)
  27. {
  28. if (!_prototypeManager.TryIndex(args.Sign, out var signPrototype))
  29. return;
  30. SetBarSign(ent, signPrototype);
  31. }
  32. public void SetBarSign(Entity<BarSignComponent> ent, BarSignPrototype newPrototype)
  33. {
  34. var meta = MetaData(ent);
  35. var name = Loc.GetString(newPrototype.Name);
  36. _metaData.SetEntityName(ent, name, meta);
  37. _metaData.SetEntityDescription(ent, Loc.GetString(newPrototype.Description), meta);
  38. ent.Comp.Current = newPrototype.ID;
  39. Dirty(ent);
  40. }
  41. public static List<BarSignPrototype> GetAllBarSigns(IPrototypeManager prototypeManager)
  42. {
  43. return prototypeManager
  44. .EnumeratePrototypes<BarSignPrototype>()
  45. .Where(p => !p.Hidden)
  46. .ToList();
  47. }
  48. }