StorageContainerVisualsSystem.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using Content.Client.Storage.Components;
  2. using Content.Shared.Rounding;
  3. using Content.Shared.Storage;
  4. using Robust.Client.GameObjects;
  5. namespace Content.Client.Storage.Systems;
  6. /// <inheritdoc cref="StorageContainerVisualsComponent"/>
  7. public sealed class StorageContainerVisualsSystem : VisualizerSystem<StorageContainerVisualsComponent>
  8. {
  9. protected override void OnAppearanceChange(EntityUid uid, StorageContainerVisualsComponent component, ref AppearanceChangeEvent args)
  10. {
  11. if (args.Sprite == null)
  12. return;
  13. if (!AppearanceSystem.TryGetData<int>(uid, StorageVisuals.StorageUsed, out var used, args.Component))
  14. return;
  15. if (!AppearanceSystem.TryGetData<int>(uid, StorageVisuals.Capacity, out var capacity, args.Component))
  16. return;
  17. var fraction = used / (float) capacity;
  18. if (!args.Sprite.LayerMapTryGet(component.FillLayer, out var fillLayer))
  19. return;
  20. var closestFillSprite = Math.Min(ContentHelpers.RoundToNearestLevels(fraction, 1, component.MaxFillLevels + 1),
  21. component.MaxFillLevels);
  22. if (closestFillSprite > 0)
  23. {
  24. if (component.FillBaseName == null)
  25. return;
  26. args.Sprite.LayerSetVisible(fillLayer, true);
  27. var stateName = component.FillBaseName + closestFillSprite;
  28. args.Sprite.LayerSetState(fillLayer, stateName);
  29. }
  30. else
  31. {
  32. args.Sprite.LayerSetVisible(fillLayer, false);
  33. }
  34. }
  35. }