CharacteristicSystem.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Runtime.CompilerServices;
  2. using Robust.Shared.Prototypes;
  3. namespace Content.Shared._Stalker.Characteristics;
  4. public sealed class CharacteristicSystem : EntitySystem
  5. {
  6. [Robust.Shared.IoC.Dependency] private readonly IPrototypeManager _prototype = default!;
  7. public IReadOnlyDictionary<CharacteristicType, Characteristic> Characteristics => _characteristics;
  8. private readonly Dictionary<CharacteristicType, Characteristic> _characteristics = new();
  9. public override void Initialize()
  10. {
  11. base.Initialize();
  12. EnumerateCharacteristics();
  13. _prototype.PrototypesReloaded += OnProtoReload;
  14. }
  15. // TODO: It'd be better to check influence of this attribute here on perf :)
  16. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  17. private void EnumerateCharacteristics()
  18. {
  19. var prototypes = _prototype.EnumeratePrototypes<CharacteristicPrototype>();
  20. foreach (var prototype in prototypes)
  21. {
  22. var characteristic = new Characteristic(prototype);
  23. _characteristics.Add(prototype.Type, characteristic);
  24. }
  25. }
  26. private void OnProtoReload(PrototypesReloadedEventArgs args)
  27. {
  28. if (!args.WasModified<CharacteristicPrototype>())
  29. return;
  30. EnumerateCharacteristics();
  31. }
  32. }