1
0

SharedChargesSystem.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Content.Shared.Charges.Components;
  2. using Content.Shared.Examine;
  3. namespace Content.Shared.Charges.Systems;
  4. public abstract class SharedChargesSystem : EntitySystem
  5. {
  6. protected EntityQuery<LimitedChargesComponent> Query;
  7. public override void Initialize()
  8. {
  9. base.Initialize();
  10. Query = GetEntityQuery<LimitedChargesComponent>();
  11. SubscribeLocalEvent<LimitedChargesComponent, ExaminedEvent>(OnExamine);
  12. }
  13. protected virtual void OnExamine(EntityUid uid, LimitedChargesComponent comp, ExaminedEvent args)
  14. {
  15. if (!args.IsInDetailsRange)
  16. return;
  17. using (args.PushGroup(nameof(LimitedChargesComponent)))
  18. {
  19. args.PushMarkup(Loc.GetString("limited-charges-charges-remaining", ("charges", comp.Charges)));
  20. if (comp.Charges == comp.MaxCharges)
  21. {
  22. args.PushMarkup(Loc.GetString("limited-charges-max-charges"));
  23. }
  24. }
  25. }
  26. /// <summary>
  27. /// Tries to add a number of charges. If it over or underflows it will be clamped, wasting the extra charges.
  28. /// </summary>
  29. public virtual void AddCharges(EntityUid uid, int change, LimitedChargesComponent? comp = null)
  30. {
  31. if (!Query.Resolve(uid, ref comp, false))
  32. return;
  33. var old = comp.Charges;
  34. comp.Charges = Math.Clamp(comp.Charges + change, 0, comp.MaxCharges);
  35. if (comp.Charges != old)
  36. Dirty(uid, comp);
  37. }
  38. /// <summary>
  39. /// Gets the limited charges component and returns true if there are no charges. Will return false if there is no limited charges component.
  40. /// </summary>
  41. public bool IsEmpty(EntityUid uid, LimitedChargesComponent? comp = null)
  42. {
  43. // can't be empty if there are no limited charges
  44. if (!Query.Resolve(uid, ref comp, false))
  45. return false;
  46. return comp.Charges <= 0;
  47. }
  48. /// <summary>
  49. /// Uses a single charge. Must check IsEmpty beforehand to prevent using with 0 charge.
  50. /// </summary>
  51. public void UseCharge(EntityUid uid, LimitedChargesComponent? comp = null)
  52. {
  53. AddCharges(uid, -1, comp);
  54. }
  55. /// <summary>
  56. /// Checks IsEmpty and uses a charge if it isn't empty.
  57. /// </summary>
  58. public bool TryUseCharge(Entity<LimitedChargesComponent?> ent)
  59. {
  60. if (!Query.Resolve(ent, ref ent.Comp, false))
  61. return true;
  62. if (IsEmpty(ent, ent.Comp))
  63. return false;
  64. UseCharge(ent, ent.Comp);
  65. return true;
  66. }
  67. /// <summary>
  68. /// Gets the limited charges component and returns true if the number of charges remaining is less than the specified value.
  69. /// Will return false if there is no limited charges component.
  70. /// </summary>
  71. public bool HasInsufficientCharges(EntityUid uid, int requiredCharges, LimitedChargesComponent? comp = null)
  72. {
  73. // can't be empty if there are no limited charges
  74. if (!Resolve(uid, ref comp, false))
  75. return false;
  76. return comp.Charges < requiredCharges;
  77. }
  78. /// <summary>
  79. /// Uses up a specified number of charges. Must check HasInsufficentCharges beforehand to prevent using with insufficient remaining charges.
  80. /// </summary>
  81. public virtual void UseCharges(EntityUid uid, int chargesUsed, LimitedChargesComponent? comp = null)
  82. {
  83. AddCharges(uid, -chargesUsed, comp);
  84. }
  85. }