1
0

StackSystem.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Linq;
  2. using Content.Client.Items;
  3. using Content.Client.Storage.Systems;
  4. using Content.Shared.Stacks;
  5. using JetBrains.Annotations;
  6. using Robust.Client.GameObjects;
  7. namespace Content.Client.Stack
  8. {
  9. [UsedImplicitly]
  10. public sealed class StackSystem : SharedStackSystem
  11. {
  12. [Dependency] private readonly AppearanceSystem _appearanceSystem = default!;
  13. [Dependency] private readonly ItemCounterSystem _counterSystem = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<StackComponent, AppearanceChangeEvent>(OnAppearanceChange);
  18. Subs.ItemStatus<StackComponent>(ent => new StackStatusControl(ent));
  19. }
  20. public override void SetCount(EntityUid uid, int amount, StackComponent? component = null)
  21. {
  22. if (!Resolve(uid, ref component))
  23. return;
  24. base.SetCount(uid, amount, component);
  25. if (component.Lingering &&
  26. TryComp<SpriteComponent>(uid, out var sprite))
  27. {
  28. // tint the stack gray and make it transparent if it's lingering.
  29. var color = component.Count == 0 && component.Lingering
  30. ? Color.DarkGray.WithAlpha(0.65f)
  31. : Color.White;
  32. for (var i = 0; i < sprite.AllLayers.Count(); i++)
  33. {
  34. sprite.LayerSetColor(i, color);
  35. }
  36. }
  37. // TODO PREDICT ENTITY DELETION: This should really just be a normal entity deletion call.
  38. if (component.Count <= 0 && !component.Lingering)
  39. {
  40. Xform.DetachEntity(uid, Transform(uid));
  41. return;
  42. }
  43. component.UiUpdateNeeded = true;
  44. }
  45. private void OnAppearanceChange(EntityUid uid, StackComponent comp, ref AppearanceChangeEvent args)
  46. {
  47. if (args.Sprite == null || comp.LayerStates.Count < 1)
  48. return;
  49. // Skip processing if no actual
  50. if (!_appearanceSystem.TryGetData<int>(uid, StackVisuals.Actual, out var actual, args.Component))
  51. return;
  52. if (!_appearanceSystem.TryGetData<int>(uid, StackVisuals.MaxCount, out var maxCount, args.Component))
  53. maxCount = comp.LayerStates.Count;
  54. if (!_appearanceSystem.TryGetData<bool>(uid, StackVisuals.Hide, out var hidden, args.Component))
  55. hidden = false;
  56. if (comp.IsComposite)
  57. _counterSystem.ProcessCompositeSprite(uid, actual, maxCount, comp.LayerStates, hidden, sprite: args.Sprite);
  58. else
  59. _counterSystem.ProcessOpaqueSprite(uid, comp.BaseLayer, actual, maxCount, comp.LayerStates, hidden, sprite: args.Sprite);
  60. }
  61. }
  62. }