RechargeBasicEntityAmmoSystem.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Content.Shared.Examine;
  2. using Content.Shared.Weapons.Ranged.Components;
  3. using Robust.Shared.Audio;
  4. using Robust.Shared.Audio.Systems;
  5. using Robust.Shared.Network;
  6. using Robust.Shared.Player;
  7. using Robust.Shared.Timing;
  8. using Robust.Shared.Utility;
  9. namespace Content.Shared.Weapons.Ranged.Systems;
  10. public sealed class RechargeBasicEntityAmmoSystem : EntitySystem
  11. {
  12. [Dependency] private readonly IGameTiming _timing = default!;
  13. [Dependency] private readonly INetManager _netManager = default!;
  14. [Dependency] private readonly SharedAudioSystem _audio = default!;
  15. [Dependency] private readonly SharedGunSystem _gun = default!;
  16. [Dependency] private readonly MetaDataSystem _metadata = default!;
  17. public override void Initialize()
  18. {
  19. base.Initialize();
  20. SubscribeLocalEvent<RechargeBasicEntityAmmoComponent, MapInitEvent>(OnInit);
  21. SubscribeLocalEvent<RechargeBasicEntityAmmoComponent, ExaminedEvent>(OnExamined);
  22. }
  23. public override void Update(float frameTime)
  24. {
  25. base.Update(frameTime);
  26. var query = EntityQueryEnumerator<RechargeBasicEntityAmmoComponent, BasicEntityAmmoProviderComponent>();
  27. while (query.MoveNext(out var uid, out var recharge, out var ammo))
  28. {
  29. if (ammo.Count is null || ammo.Count == ammo.Capacity || recharge.NextCharge == null)
  30. continue;
  31. if (recharge.NextCharge > _timing.CurTime)
  32. continue;
  33. if (_gun.UpdateBasicEntityAmmoCount(uid, ammo.Count.Value + 1, ammo))
  34. {
  35. // We don't predict this because occasionally on client it may not play.
  36. // PlayPredicted will still be predicted on the client.
  37. if (_netManager.IsServer)
  38. _audio.PlayPvs(recharge.RechargeSound, uid);
  39. }
  40. if (ammo.Count == ammo.Capacity)
  41. {
  42. recharge.NextCharge = null;
  43. Dirty(uid, recharge);
  44. continue;
  45. }
  46. recharge.NextCharge = recharge.NextCharge.Value + TimeSpan.FromSeconds(recharge.RechargeCooldown);
  47. Dirty(uid, recharge);
  48. }
  49. }
  50. private void OnInit(EntityUid uid, RechargeBasicEntityAmmoComponent component, MapInitEvent args)
  51. {
  52. component.NextCharge = _timing.CurTime;
  53. Dirty(uid, component);
  54. }
  55. private void OnExamined(EntityUid uid, RechargeBasicEntityAmmoComponent component, ExaminedEvent args)
  56. {
  57. if (!component.ShowExamineText)
  58. return;
  59. if (!TryComp<BasicEntityAmmoProviderComponent>(uid, out var ammo)
  60. || ammo.Count == ammo.Capacity ||
  61. component.NextCharge == null)
  62. {
  63. args.PushMarkup(Loc.GetString("recharge-basic-entity-ammo-full"));
  64. return;
  65. }
  66. var timeLeft = component.NextCharge + _metadata.GetPauseTime(uid) - _timing.CurTime;
  67. args.PushMarkup(Loc.GetString("recharge-basic-entity-ammo-can-recharge", ("seconds", Math.Round(timeLeft.Value.TotalSeconds, 1))));
  68. }
  69. public void Reset(EntityUid uid, RechargeBasicEntityAmmoComponent? recharge = null)
  70. {
  71. if (!Resolve(uid, ref recharge, false))
  72. return;
  73. if (recharge.NextCharge == null || recharge.NextCharge < _timing.CurTime)
  74. {
  75. recharge.NextCharge = _timing.CurTime + TimeSpan.FromSeconds(recharge.RechargeCooldown);
  76. Dirty(uid, recharge);
  77. }
  78. }
  79. }