* #3935 - toys properly play sounds when: Activated, Landed (after throwing) and Used in hand * #3935 - extracted BaseEmitSoundComponent * #3935 - refactored EmitSound components to use ECS * #3935 - added new components to client ignored components * #3935 - added suggested stuff for EmitSoundSystem et al. * #3935 added suggestions from PR * #3935 implemented suggestions from PR * #3935 updated namespace
This commit is contained in:
15
Content.Server/Sound/BaseEmitSoundComponent.cs
Normal file
15
Content.Server/Sound/BaseEmitSoundComponent.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Sound
|
||||
{
|
||||
/// <summary>
|
||||
/// Base sound emitter which defines most of the data fields.
|
||||
/// </summary>
|
||||
public abstract class BaseEmitSoundComponent : Component
|
||||
{
|
||||
[ViewVariables(VVAccess.ReadWrite)] [DataField("variation")] public float PitchVariation { get; set; } = 0.0f;
|
||||
[ViewVariables(VVAccess.ReadWrite)] [DataField("soundCollection", required: true)] public string SoundCollectionName { get; set; } = default!;
|
||||
}
|
||||
}
|
||||
14
Content.Server/Sound/EmitSoundOnActivateComponent.cs
Normal file
14
Content.Server/Sound/EmitSoundOnActivateComponent.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.Sound
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on ActivateInWorld
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public class EmitSoundOnActivateComponent : BaseEmitSoundComponent
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override string Name => "EmitSoundOnActivate";
|
||||
}
|
||||
}
|
||||
14
Content.Server/Sound/EmitSoundOnLandComponent.cs
Normal file
14
Content.Server/Sound/EmitSoundOnLandComponent.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.Sound
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple sound emitter that emits sound on LandEvent
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public class EmitSoundOnLandComponent : BaseEmitSoundComponent
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override string Name => "EmitSoundOnLand";
|
||||
}
|
||||
}
|
||||
56
Content.Server/Sound/EmitSoundSystem.cs
Normal file
56
Content.Server/Sound/EmitSoundSystem.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
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;
|
||||
|
||||
namespace Content.Server.Sound
|
||||
{
|
||||
[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)
|
||||
{
|
||||
PlayRandomSoundFromCollection(component);
|
||||
}
|
||||
|
||||
private void PlayRandomSoundFromCollection(BaseEmitSoundComponent component)
|
||||
{
|
||||
var file = SelectRandomSoundFromSoundCollection(component.SoundCollectionName!);
|
||||
PlaySingleSound(file, component);
|
||||
}
|
||||
|
||||
private string SelectRandomSoundFromSoundCollection(string soundCollectionName)
|
||||
{
|
||||
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(soundCollectionName);
|
||||
return _random.Pick(soundCollection.PickFiles);
|
||||
}
|
||||
|
||||
private static void PlaySingleSound(string soundName, BaseEmitSoundComponent component)
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(component.Owner), soundName, component.Owner,
|
||||
AudioHelpers.WithVariation(component.PitchVariation).WithVolume(-2f));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user