StorageFillVisualizerSystem.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Content.Shared.Rounding;
  2. using Content.Shared.Storage;
  3. using Content.Shared.Storage.Components;
  4. using Robust.Shared.Containers;
  5. namespace Content.Server.Storage.EntitySystems;
  6. public sealed class StorageFillVisualizerSystem : EntitySystem
  7. {
  8. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  9. public override void Initialize()
  10. {
  11. base.Initialize();
  12. SubscribeLocalEvent<StorageFillVisualizerComponent, ComponentStartup>(OnStartup);
  13. SubscribeLocalEvent<StorageFillVisualizerComponent, EntInsertedIntoContainerMessage>(OnInserted);
  14. SubscribeLocalEvent<StorageFillVisualizerComponent, EntRemovedFromContainerMessage>(OnRemoved);
  15. }
  16. private void OnStartup(EntityUid uid, StorageFillVisualizerComponent component, ComponentStartup args)
  17. {
  18. UpdateAppearance(uid, component: component);
  19. }
  20. private void OnInserted(EntityUid uid, StorageFillVisualizerComponent component, EntInsertedIntoContainerMessage args)
  21. {
  22. UpdateAppearance(uid, component: component);
  23. }
  24. private void OnRemoved(EntityUid uid, StorageFillVisualizerComponent component, EntRemovedFromContainerMessage args)
  25. {
  26. UpdateAppearance(uid, component: component);
  27. }
  28. private void UpdateAppearance(EntityUid uid, StorageComponent? storage = null, AppearanceComponent? appearance = null,
  29. StorageFillVisualizerComponent? component = null)
  30. {
  31. if (!Resolve(uid, ref storage, ref appearance, ref component, false))
  32. return;
  33. if (component.MaxFillLevels < 1)
  34. return;
  35. if (!_appearance.TryGetData<int>(uid, StorageVisuals.StorageUsed, out var used, appearance))
  36. return;
  37. if (!_appearance.TryGetData<int>(uid, StorageVisuals.Capacity, out var capacity, appearance))
  38. return;
  39. var level = ContentHelpers.RoundToLevels(used, capacity, component.MaxFillLevels);
  40. _appearance.SetData(uid, StorageFillVisuals.FillLevel, level, appearance);
  41. }
  42. }