Files
OldThink/Content.Shared/Sound/SharedEmitSoundSystem.cs

158 lines
6.0 KiB
C#
Raw Normal View History

2023-01-04 12:56:35 -06:00
using Content.Shared.Hands;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using Content.Shared.Maps;
using Content.Shared.Popups;
using Content.Shared.Sound.Components;
using Content.Shared.Throwing;
using Content.Shared.White.EndOfRoundStats.EmitSoundStatSystem;
2023-01-04 12:56:35 -06:00
using JetBrains.Annotations;
2023-05-20 02:04:26 +10:00
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
2023-01-04 12:56:35 -06:00
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Events;
2023-01-04 12:56:35 -06:00
using Robust.Shared.Random;
using Robust.Shared.Timing;
2023-01-04 12:56:35 -06:00
namespace Content.Shared.Sound;
/// <summary>
/// Will play a sound on various events if the affected entity has a component derived from BaseEmitSoundComponent
/// </summary>
[UsedImplicitly]
public abstract class SharedEmitSoundSystem : EntitySystem
2023-01-04 12:56:35 -06:00
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly INetManager _netMan = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefMan = default!;
[Dependency] protected readonly IRobustRandom Random = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
[Dependency] protected readonly SharedPopupSystem Popup = default!;
public override void Initialize()
2023-01-04 12:56:35 -06:00
{
base.Initialize();
SubscribeLocalEvent<EmitSoundOnSpawnComponent, MapInitEvent>(OnEmitSpawnOnInit);
SubscribeLocalEvent<EmitSoundOnLandComponent, LandEvent>(OnEmitSoundOnLand);
SubscribeLocalEvent<EmitSoundOnUseComponent, UseInHandEvent>(OnEmitSoundOnUseInHand);
SubscribeLocalEvent<EmitSoundOnThrowComponent, ThrownEvent>(OnEmitSoundOnThrown);
SubscribeLocalEvent<EmitSoundOnActivateComponent, ActivateInWorldEvent>(OnEmitSoundOnActivateInWorld);
SubscribeLocalEvent<EmitSoundOnPickupComponent, GotEquippedHandEvent>(OnEmitSoundOnPickup);
SubscribeLocalEvent<EmitSoundOnDropComponent, DroppedEvent>(OnEmitSoundOnDrop);
2023-05-01 14:49:25 +10:00
SubscribeLocalEvent<EmitSoundOnCollideComponent, EntityUnpausedEvent>(OnEmitSoundUnpaused);
SubscribeLocalEvent<EmitSoundOnCollideComponent, StartCollideEvent>(OnEmitSoundOnCollide);
}
2023-01-04 12:56:35 -06:00
private void OnEmitSpawnOnInit(EntityUid uid, EmitSoundOnSpawnComponent component, MapInitEvent args)
{
TryEmitSound(uid, component, predict: false);
}
2023-01-04 12:56:35 -06:00
private void OnEmitSoundOnLand(EntityUid uid, BaseEmitSoundComponent component, ref LandEvent args)
{
2023-05-19 17:10:31 +10:00
if (!args.PlaySound ||
!TryComp<TransformComponent>(uid, out var xform) ||
!_mapManager.TryGetGrid(xform.GridUid, out var grid))
2023-05-19 17:10:31 +10:00
{
return;
2023-05-19 17:10:31 +10:00
}
2023-01-04 12:56:35 -06:00
var tile = grid.GetTileRef(xform.Coordinates);
2023-01-04 12:56:35 -06:00
// Handle maps being grids (we'll still emit the sound).
if (xform.GridUid != xform.MapUid && tile.IsSpace(_tileDefMan))
return;
2023-01-04 12:56:35 -06:00
// hand throwing not predicted sadly
TryEmitSound(uid, component, args.User, false);
}
2023-01-04 12:56:35 -06:00
private void OnEmitSoundOnUseInHand(EntityUid uid, EmitSoundOnUseComponent component, UseInHandEvent args)
{
// Intentionally not checking whether the interaction has already been handled.
TryEmitSound(uid, component, args.User);
2023-01-04 12:56:35 -06:00
if (component.Handle)
args.Handled = true;
}
2023-01-04 12:56:35 -06:00
private void OnEmitSoundOnThrown(EntityUid uid, BaseEmitSoundComponent component, ref ThrownEvent args)
{
TryEmitSound(uid, component, args.User, false);
}
2023-01-04 12:56:35 -06:00
private void OnEmitSoundOnActivateInWorld(EntityUid uid, EmitSoundOnActivateComponent component, ActivateInWorldEvent args)
{
// Intentionally not checking whether the interaction has already been handled.
TryEmitSound(uid, component, args.User);
2023-01-04 12:56:35 -06:00
if (component.Handle)
args.Handled = true;
}
private void OnEmitSoundOnPickup(EntityUid uid, EmitSoundOnPickupComponent component, GotEquippedHandEvent args)
{
TryEmitSound(uid, component, args.User);
}
private void OnEmitSoundOnDrop(EntityUid uid, EmitSoundOnDropComponent component, DroppedEvent args)
{
TryEmitSound(uid, component, args.User);
}
2023-01-04 12:56:35 -06:00
protected void TryEmitSound(EntityUid uid, BaseEmitSoundComponent component, EntityUid? user=null, bool predict=true)
{
if (component.Sound == null)
return;
if (predict)
2023-01-04 12:56:35 -06:00
{
_audioSystem.PlayPredicted(component.Sound, uid, user);
2023-01-04 12:56:35 -06:00
}
else if (_netMan.IsServer)
2023-01-04 12:56:35 -06:00
{
// don't predict sounds that client couldn't have played already
_audioSystem.PlayPvs(component.Sound, uid);
2023-01-04 12:56:35 -06:00
}
if (_netMan.IsServer)
RaiseLocalEvent(new EmitSoundStatEvent(component.Owner, component.Sound));
}
2023-01-04 12:56:35 -06:00
2023-05-01 14:49:25 +10:00
private void OnEmitSoundUnpaused(EntityUid uid, EmitSoundOnCollideComponent component, ref EntityUnpausedEvent args)
{
component.NextSound += args.PausedTime;
}
private void OnEmitSoundOnCollide(EntityUid uid, EmitSoundOnCollideComponent component, ref StartCollideEvent args)
{
if (!args.OurFixture.Hard ||
!args.OtherFixture.Hard ||
!TryComp<PhysicsComponent>(uid, out var physics) ||
physics.LinearVelocity.Length() < component.MinimumVelocity ||
_timing.CurTime < component.NextSound ||
MetaData(uid).EntityPaused)
2023-01-04 12:56:35 -06:00
{
return;
2023-01-04 12:56:35 -06:00
}
2023-05-20 02:04:26 +10:00
const float MaxVolumeVelocity = 10f;
const float MinVolume = -10f;
const float MaxVolume = 2f;
var fraction = MathF.Min(1f, (physics.LinearVelocity.Length() - component.MinimumVelocity) / MaxVolumeVelocity);
2023-05-20 02:04:26 +10:00
var volume = MinVolume + (MaxVolume - MinVolume) * fraction;
component.NextSound = _timing.CurTime + EmitSoundOnCollideComponent.CollideCooldown;
var sound = component.Sound;
2023-05-20 02:04:26 +10:00
if (_netMan.IsServer && sound != null)
{
_audioSystem.PlayPvs(_audioSystem.GetSound(sound), uid, AudioParams.Default.WithVolume(volume));
}
2023-01-04 12:56:35 -06:00
}
}