MovementSoundSystem.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using Content.Shared.Movement.Components;
  2. using Content.Shared.Movement.Events;
  3. using Robust.Shared.Audio.Systems;
  4. using Robust.Shared.Timing;
  5. using Robust.Shared.Utility;
  6. namespace Content.Shared.Movement.Systems;
  7. /// <summary>
  8. /// Plays a sound on MoveInputEvent.
  9. /// </summary>
  10. public sealed class MovementSoundSystem : EntitySystem
  11. {
  12. [Dependency] private readonly IGameTiming _timing = default!;
  13. [Dependency] private readonly SharedAudioSystem _audio = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<MovementSoundComponent, MoveInputEvent>(OnMoveInput);
  18. }
  19. private void OnMoveInput(Entity<MovementSoundComponent> ent, ref MoveInputEvent args)
  20. {
  21. if (!_timing.IsFirstTimePredicted)
  22. return;
  23. var oldMoving = (SharedMoverController.GetNormalizedMovement(args.OldMovement) & MoveButtons.AnyDirection) != MoveButtons.None;
  24. var moving = (SharedMoverController.GetNormalizedMovement(args.Entity.Comp.HeldMoveButtons) & MoveButtons.AnyDirection) != MoveButtons.None;
  25. if (oldMoving == moving)
  26. return;
  27. if (moving)
  28. {
  29. DebugTools.Assert(ent.Comp.SoundEntity == null);
  30. ent.Comp.SoundEntity = _audio.PlayPredicted(ent.Comp.Sound, ent.Owner, ent.Owner)?.Entity;
  31. }
  32. else
  33. {
  34. ent.Comp.SoundEntity = _audio.Stop(ent.Comp.SoundEntity);
  35. }
  36. }
  37. }