2021-06-19 11:35:56 +02:00
using Content.Shared.Audio ;
using Content.Shared.Interaction ;
using Content.Shared.Throwing ;
using Content.Server.Interaction.Components ;
using Content.Server.Throwing ;
using JetBrains.Annotations ;
using Robust.Shared.Audio ;
using Robust.Shared.GameObjects ;
using Robust.Shared.IoC ;
using Robust.Shared.Prototypes ;
using Robust.Shared.Player ;
using Robust.Shared.Random ;
2021-06-27 21:27:59 +02:00
using Robust.Shared.Log ;
2021-06-19 11:35:56 +02:00
2021-06-27 21:48:57 +02:00
namespace Content.Server.Sound
2021-06-19 11:35:56 +02:00
{
[UsedImplicitly]
public class EmitSoundSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default ! ;
[Dependency] private readonly IRobustRandom _random = default ! ;
/// <inheritdoc />
public override void Initialize ( )
{
base . Initialize ( ) ;
SubscribeLocalEvent < EmitSoundOnLandComponent , LandEvent > ( ( eUI , comp , arg ) = > PlaySound ( comp ) ) ;
SubscribeLocalEvent < EmitSoundOnUseComponent , UseInHandEvent > ( ( eUI , comp , arg ) = > PlaySound ( comp ) ) ;
SubscribeLocalEvent < EmitSoundOnThrowComponent , ThrownEvent > ( ( eUI , comp , arg ) = > PlaySound ( comp ) ) ;
SubscribeLocalEvent < EmitSoundOnActivateComponent , ActivateInWorldEvent > ( ( eUI , comp , args ) = > PlaySound ( comp ) ) ;
}
private void PlaySound ( BaseEmitSoundComponent component )
{
2021-06-27 21:48:57 +02:00
if ( ! string . IsNullOrWhiteSpace ( component . SoundCollectionName ) )
2021-06-27 21:27:59 +02:00
{
PlayRandomSoundFromCollection ( component ) ;
}
2021-06-27 21:48:57 +02:00
else if ( ! string . IsNullOrWhiteSpace ( component . SoundName ) )
2021-06-27 21:30:28 +02:00
{
2021-06-27 21:48:57 +02:00
PlaySingleSound ( component . SoundName , component ) ;
2021-06-27 21:30:28 +02:00
}
2021-06-27 21:27:59 +02:00
else
{
2021-06-27 21:48:57 +02:00
Logger . Warning ( $"{nameof(component)} Uid:{component.Owner.Uid} has neither {nameof(component.SoundCollectionName)} nor {nameof(component.SoundName)} to play." ) ;
2021-06-27 21:27:59 +02:00
}
2021-06-19 11:35:56 +02:00
}
private void PlayRandomSoundFromCollection ( BaseEmitSoundComponent component )
{
2021-06-27 21:48:57 +02:00
var file = SelectRandomSoundFromSoundCollection ( component . SoundCollectionName ! ) ;
PlaySingleSound ( file , component ) ;
2021-06-19 11:35:56 +02:00
}
private string SelectRandomSoundFromSoundCollection ( string soundCollectionName )
{
var soundCollection = _prototypeManager . Index < SoundCollectionPrototype > ( soundCollectionName ) ;
return _random . Pick ( soundCollection . PickFiles ) ;
}
private static void PlaySingleSound ( string soundName , BaseEmitSoundComponent component )
{
2021-06-27 21:48:57 +02:00
SoundSystem . Play ( Filter . Pvs ( component . Owner ) , soundName , component . Owner ,
AudioHelpers . WithVariation ( component . PitchVariation ) . WithVolume ( - 2f ) ) ;
2021-06-19 11:35:56 +02:00
}
}
}