1
0

ShuttleSystem.Impact.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Numerics;
  2. using Content.Server.Shuttles.Components;
  3. using Content.Shared.Audio;
  4. using Robust.Shared.Audio;
  5. using Robust.Shared.Map;
  6. using Robust.Shared.Physics.Dynamics;
  7. using Robust.Shared.Physics.Events;
  8. using Robust.Shared.Player;
  9. namespace Content.Server.Shuttles.Systems;
  10. public sealed partial class ShuttleSystem
  11. {
  12. /// <summary>
  13. /// Minimum velocity difference between 2 bodies for a shuttle "impact" to occur.
  14. /// </summary>
  15. private const int MinimumImpactVelocity = 10;
  16. private readonly SoundCollectionSpecifier _shuttleImpactSound = new("ShuttleImpactSound");
  17. private void InitializeImpact()
  18. {
  19. SubscribeLocalEvent<ShuttleComponent, StartCollideEvent>(OnShuttleCollide);
  20. }
  21. private void OnShuttleCollide(EntityUid uid, ShuttleComponent component, ref StartCollideEvent args)
  22. {
  23. if (!HasComp<ShuttleComponent>(args.OtherEntity))
  24. return;
  25. var ourBody = args.OurBody;
  26. var otherBody = args.OtherBody;
  27. // TODO: Would also be nice to have a continuous sound for scraping.
  28. var ourXform = Transform(uid);
  29. if (ourXform.MapUid == null)
  30. return;
  31. var otherXform = Transform(args.OtherEntity);
  32. var ourPoint = Vector2.Transform(args.WorldPoint, _transform.GetInvWorldMatrix(ourXform));
  33. var otherPoint = Vector2.Transform(args.WorldPoint, _transform.GetInvWorldMatrix(otherXform));
  34. var ourVelocity = _physics.GetLinearVelocity(uid, ourPoint, ourBody, ourXform);
  35. var otherVelocity = _physics.GetLinearVelocity(args.OtherEntity, otherPoint, otherBody, otherXform);
  36. var jungleDiff = (ourVelocity - otherVelocity).Length();
  37. if (jungleDiff < MinimumImpactVelocity)
  38. {
  39. return;
  40. }
  41. var coordinates = new EntityCoordinates(ourXform.MapUid.Value, args.WorldPoint);
  42. var volume = MathF.Min(10f, 1f * MathF.Pow(jungleDiff, 0.5f) - 5f);
  43. var audioParams = AudioParams.Default.WithVariation(SharedContentAudioSystem.DefaultVariation).WithVolume(volume);
  44. _audio.PlayPvs(_shuttleImpactSound, coordinates, audioParams);
  45. }
  46. }