ChargesSystem.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Content.Server.Charges.Components;
  2. using Content.Shared.Charges.Components;
  3. using Content.Shared.Charges.Systems;
  4. using Content.Shared.Examine;
  5. using Robust.Shared.Timing;
  6. namespace Content.Server.Charges.Systems;
  7. public sealed class ChargesSystem : SharedChargesSystem
  8. {
  9. [Dependency] private readonly IGameTiming _timing = default!;
  10. public override void Update(float frameTime)
  11. {
  12. base.Update(frameTime);
  13. var query = EntityQueryEnumerator<LimitedChargesComponent, AutoRechargeComponent>();
  14. while (query.MoveNext(out var uid, out var charges, out var recharge))
  15. {
  16. if (charges.Charges == charges.MaxCharges || _timing.CurTime < recharge.NextChargeTime)
  17. continue;
  18. AddCharges(uid, 1, charges);
  19. recharge.NextChargeTime = _timing.CurTime + recharge.RechargeDuration;
  20. }
  21. }
  22. protected override void OnExamine(EntityUid uid, LimitedChargesComponent comp, ExaminedEvent args)
  23. {
  24. base.OnExamine(uid, comp, args);
  25. // only show the recharging info if it's not full
  26. if (!args.IsInDetailsRange || comp.Charges == comp.MaxCharges || !TryComp<AutoRechargeComponent>(uid, out var recharge))
  27. return;
  28. var timeRemaining = Math.Round((recharge.NextChargeTime - _timing.CurTime).TotalSeconds);
  29. args.PushMarkup(Loc.GetString("limited-charges-recharging", ("seconds", timeRemaining)));
  30. }
  31. public override void AddCharges(EntityUid uid, int change, LimitedChargesComponent? comp = null)
  32. {
  33. if (!Query.Resolve(uid, ref comp, false))
  34. return;
  35. var startRecharge = comp.Charges == comp.MaxCharges;
  36. base.AddCharges(uid, change, comp);
  37. // if a charge was just used from full, start the recharge timer
  38. // TODO: probably make this an event instead of having le server system that just does this
  39. if (change < 0 && startRecharge && TryComp<AutoRechargeComponent>(uid, out var recharge))
  40. recharge.NextChargeTime = _timing.CurTime + recharge.RechargeDuration;
  41. }
  42. }