FlyBySoundSystem.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using Content.Shared.Projectiles;
  2. using Content.Shared.Weapons.Ranged.Components;
  3. using Content.Shared.Weapons.Ranged.Systems;
  4. using Robust.Client.Player;
  5. using Robust.Shared.Audio;
  6. using Robust.Shared.Audio.Systems;
  7. using Robust.Shared.Physics.Events;
  8. using Robust.Shared.Player;
  9. using Robust.Shared.Random;
  10. namespace Content.Client.Weapons.Ranged.Systems;
  11. public sealed class FlyBySoundSystem : SharedFlyBySoundSystem
  12. {
  13. [Dependency] private readonly IPlayerManager _player = default!;
  14. [Dependency] private readonly IRobustRandom _random = default!;
  15. [Dependency] private readonly SharedAudioSystem _audio = default!;
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. SubscribeLocalEvent<FlyBySoundComponent, StartCollideEvent>(OnCollide);
  20. }
  21. private void OnCollide(EntityUid uid, FlyBySoundComponent component, ref StartCollideEvent args)
  22. {
  23. var attachedEnt = _player.LocalEntity;
  24. // If it's not our ent or we shot it.
  25. if (attachedEnt == null ||
  26. args.OtherEntity != attachedEnt ||
  27. TryComp<ProjectileComponent>(uid, out var projectile) &&
  28. projectile.Shooter == attachedEnt)
  29. {
  30. return;
  31. }
  32. if (args.OurFixtureId != FlyByFixture ||
  33. !_random.Prob(component.Prob))
  34. {
  35. return;
  36. }
  37. // Play attached to our entity because the projectile may immediately delete or the likes.
  38. _audio.PlayPredicted(component.Sound, attachedEnt.Value, attachedEnt.Value);
  39. }
  40. }