JetpackSystem.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Content.Server.Atmos.Components;
  2. using Content.Server.Atmos.EntitySystems;
  3. using Content.Shared.Movement.Components;
  4. using Content.Shared.Movement.Systems;
  5. using Robust.Shared.Collections;
  6. using Robust.Shared.Timing;
  7. namespace Content.Server.Movement.Systems;
  8. public sealed class JetpackSystem : SharedJetpackSystem
  9. {
  10. [Dependency] private readonly GasTankSystem _gasTank = default!;
  11. [Dependency] private readonly IGameTiming _timing = default!;
  12. protected override bool CanEnable(EntityUid uid, JetpackComponent component)
  13. {
  14. return base.CanEnable(uid, component) &&
  15. TryComp<GasTankComponent>(uid, out var gasTank) &&
  16. !(gasTank.Air.TotalMoles < component.MoleUsage);
  17. }
  18. public override void Update(float frameTime)
  19. {
  20. base.Update(frameTime);
  21. var toDisable = new ValueList<(EntityUid Uid, JetpackComponent Component)>();
  22. var query = EntityQueryEnumerator<ActiveJetpackComponent, JetpackComponent, GasTankComponent>();
  23. while (query.MoveNext(out var uid, out var active, out var comp, out var gasTankComp))
  24. {
  25. if (_timing.CurTime < active.TargetTime)
  26. continue;
  27. var gasTank = (uid, gasTankComp);
  28. active.TargetTime = _timing.CurTime + TimeSpan.FromSeconds(active.EffectCooldown);
  29. var usedAir = _gasTank.RemoveAir(gasTank, comp.MoleUsage);
  30. if (usedAir == null)
  31. continue;
  32. var usedEnoughAir =
  33. MathHelper.CloseTo(usedAir.TotalMoles, comp.MoleUsage, comp.MoleUsage/100);
  34. if (!usedEnoughAir)
  35. {
  36. toDisable.Add((uid, comp));
  37. }
  38. _gasTank.UpdateUserInterface(gasTank);
  39. }
  40. foreach (var (uid, comp) in toDisable)
  41. {
  42. SetEnabled(uid, comp, false);
  43. }
  44. }
  45. }