#4219 upgraded EmitSoundSystem to use SoundSpecifier

This commit is contained in:
Galactic Chimp
2021-07-10 11:20:23 +02:00
parent da41942daf
commit 4500b66f28
5 changed files with 66 additions and 61 deletions

View File

@@ -1,3 +1,4 @@
using Content.Shared.Sound;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
@@ -6,13 +7,16 @@ namespace Content.Server.Sound.Components
{ {
/// <summary> /// <summary>
/// Base sound emitter which defines most of the data fields. /// Base sound emitter which defines most of the data fields.
/// Default behavior is to first try to play the sound collection, /// Accepts both single sounds and sound collections.
/// and if one isn't assigned, then try to play the single sound.
/// </summary> /// </summary>
public abstract class BaseEmitSoundComponent : Component public abstract class BaseEmitSoundComponent : Component
{ {
[ViewVariables(VVAccess.ReadWrite)] [DataField("sound")] public string? SoundName { get; set; } = default!; [ViewVariables(VVAccess.ReadWrite)]
[ViewVariables(VVAccess.ReadWrite)] [DataField("variation")] public float PitchVariation { get; set; } = 0.0f; [DataField("sound")]
[ViewVariables(VVAccess.ReadWrite)] [DataField("soundCollection")] public string? SoundCollectionName { get; set; } = default!; public SoundSpecifier Sound { get; set; } = default!;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("variation")]
public float PitchVariation { get; set; } = 0.0f;
} }
} }

View File

@@ -1,68 +1,50 @@
using Content.Shared.Audio;
using Content.Shared.Interaction;
using Content.Shared.Throwing;
using Content.Server.Interaction.Components; using Content.Server.Interaction.Components;
using Content.Server.Sound.Components; using Content.Server.Sound.Components;
using Content.Server.Throwing; using Content.Server.Throwing;
using Content.Shared.Audio;
using Content.Shared.Interaction;
using Content.Shared.Throwing;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Robust.Shared.Log; using Robust.Shared.Log;
using Robust.Shared.Player;
namespace Content.Server.Sound namespace Content.Server.Sound
{ {
/// <summary>
/// Will play a sound on various events if the affected entity has a component derived from BaseEmitSoundComponent
/// </summary>
[UsedImplicitly] [UsedImplicitly]
public class EmitSoundSystem : EntitySystem public class EmitSoundSystem : EntitySystem
{ {
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
/// <inheritdoc /> /// <inheritdoc />
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<EmitSoundOnLandComponent, LandEvent>((eUI, comp, arg) => PlaySound(comp)); SubscribeLocalEvent<EmitSoundOnLandComponent, LandEvent>((eUI, comp, arg) => HandleEmitSoundOn(comp));
SubscribeLocalEvent<EmitSoundOnUseComponent, UseInHandEvent>((eUI, comp, arg) => PlaySound(comp)); SubscribeLocalEvent<EmitSoundOnUseComponent, UseInHandEvent>((eUI, comp, arg) => HandleEmitSoundOn(comp));
SubscribeLocalEvent<EmitSoundOnThrowComponent, ThrownEvent>((eUI, comp, arg) => PlaySound(comp)); SubscribeLocalEvent<EmitSoundOnThrowComponent, ThrownEvent>((eUI, comp, arg) => HandleEmitSoundOn(comp));
SubscribeLocalEvent<EmitSoundOnActivateComponent, ActivateInWorldEvent>((eUI, comp, args) => PlaySound(comp)); SubscribeLocalEvent<EmitSoundOnActivateComponent, ActivateInWorldEvent>((eUI, comp, args) => HandleEmitSoundOn(comp));
} }
private void PlaySound(BaseEmitSoundComponent component) private void HandleEmitSoundOn(BaseEmitSoundComponent component)
{ {
if (!string.IsNullOrWhiteSpace(component.SoundCollectionName)) var soundName = component.Sound.GetSound();
{
PlayRandomSoundFromCollection(component); if (!string.IsNullOrWhiteSpace(soundName))
} {
else if (!string.IsNullOrWhiteSpace(component.SoundName)) PlaySingleSound(soundName, component);
{
PlaySingleSound(component.SoundName, component);
} }
else else
{ {
Logger.Warning($"{nameof(component)} Uid:{component.Owner.Uid} has neither {nameof(component.SoundCollectionName)} nor {nameof(component.SoundName)} to play."); Logger.Warning($"{nameof(component)} Uid:{component.Owner.Uid} has no {nameof(component.Sound)} to play.");
} }
} }
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) private static void PlaySingleSound(string soundName, BaseEmitSoundComponent component)
{ {
SoundSystem.Play(Filter.Pvs(component.Owner), soundName, component.Owner, SoundSystem.Play(Filter.Pvs(component.Owner), soundName, component.Owner, AudioHelpers.WithVariation(component.PitchVariation).WithVolume(-2f));
AudioHelpers.WithVariation(component.PitchVariation).WithVolume(-2f));
} }
} }
} }

View File

@@ -14,7 +14,8 @@
QuickEquip: false QuickEquip: false
- type: ItemCooldown - type: ItemCooldown
- type: EmitSoundOnUse - type: EmitSoundOnUse
sound: /Audio/Items/bikehorn.ogg sound:
path: /Audio/Items/bikehorn.ogg
semitoneVariation: 6 semitoneVariation: 6
- type: UseDelay - type: UseDelay
delay: 0.5 delay: 0.5

View File

@@ -12,6 +12,7 @@
- type: ItemCooldown - type: ItemCooldown
- type: LoopingSound - type: LoopingSound
- type: EmitSoundOnUse - type: EmitSoundOnUse
sound: /Audio/Items/skub.ogg sound:
path: /Audio/Items/skub.ogg
- type: UseDelay - type: UseDelay
delay: 2.0 delay: 2.0

View File

@@ -5,11 +5,14 @@
id: BasePlushie id: BasePlushie
components: components:
- type: EmitSoundOnUse - type: EmitSoundOnUse
soundCollection: ToySqueak sound:
collection: ToySqueak
- type: EmitSoundOnLand - type: EmitSoundOnLand
soundCollection: ToySqueak sound:
collection: ToySqueak
- type: EmitSoundOnActivate - type: EmitSoundOnActivate
soundCollection: ToySqueak sound:
collection: ToySqueak
- type: LoopingSound - type: LoopingSound
- type: ItemCooldown - type: ItemCooldown
- type: UseDelay - type: UseDelay
@@ -90,7 +93,8 @@
- type: ItemCooldown - type: ItemCooldown
- type: LoopingSound - type: LoopingSound
- type: EmitSoundOnUse - type: EmitSoundOnUse
sound: /Audio/Items/Toys/rattle.ogg sound:
path: /Audio/Items/Toys/rattle.ogg
- type: UseDelay - type: UseDelay
delay: 1.0 delay: 1.0
@@ -106,7 +110,8 @@
- type: ItemCooldown - type: ItemCooldown
- type: LoopingSound - type: LoopingSound
- type: EmitSoundOnUse - type: EmitSoundOnUse
sound: /Audio/Items/Toys/mousesqueek.ogg sound:
path: /Audio/Items/Toys/mousesqueek.ogg
- type: UseDelay - type: UseDelay
delay: 1.0 delay: 1.0
@@ -122,7 +127,8 @@
- type: ItemCooldown - type: ItemCooldown
- type: LoopingSound - type: LoopingSound
- type: EmitSoundOnUse - type: EmitSoundOnUse
sound: /Audio/Voice/Vox/shriek1.ogg sound:
path: /Audio/Voice/Vox/shriek1.ogg
- type: UseDelay - type: UseDelay
delay: 1.0 delay: 1.0
@@ -142,11 +148,13 @@
- type: Item - type: Item
sprite: Objects/Misc/carvings.rsi sprite: Objects/Misc/carvings.rsi
- type: EmitSoundOnThrow - type: EmitSoundOnThrow
sound: /Audio/Items/Toys/helpme.ogg sound:
path: /Audio/Items/Toys/helpme.ogg
- type: ItemCooldown - type: ItemCooldown
- type: LoopingSound - type: LoopingSound
- type: EmitSoundOnUse - type: EmitSoundOnUse
sound: /Audio/Items/Toys/helpme.ogg sound:
path: /Audio/Items/Toys/helpme.ogg
- type: UseDelay - type: UseDelay
delay: 1.0 delay: 1.0
@@ -162,11 +170,13 @@
- type: Item - type: Item
sprite: Objects/Misc/carvings.rsi sprite: Objects/Misc/carvings.rsi
- type: EmitSoundOnThrow - type: EmitSoundOnThrow
sound: /Audio/Items/Toys/hellothere.ogg sound:
path: /Audio/Items/Toys/hellothere.ogg
- type: ItemCooldown - type: ItemCooldown
- type: LoopingSound - type: LoopingSound
- type: EmitSoundOnUse - type: EmitSoundOnUse
sound: /Audio/Items/Toys/hellothere.ogg sound:
path: /Audio/Items/Toys/hellothere.ogg
- type: UseDelay - type: UseDelay
delay: 1.0 delay: 1.0
@@ -182,11 +192,13 @@
- type: Item - type: Item
sprite: Objects/Misc/carvings.rsi sprite: Objects/Misc/carvings.rsi
- type: EmitSoundOnThrow - type: EmitSoundOnThrow
sound: /Audio/Items/Toys/thankyou.ogg sound:
path: /Audio/Items/Toys/thankyou.ogg
- type: ItemCooldown - type: ItemCooldown
- type: LoopingSound - type: LoopingSound
- type: EmitSoundOnUse - type: EmitSoundOnUse
sound: /Audio/Items/Toys/thankyou.ogg sound:
path: /Audio/Items/Toys/thankyou.ogg
- type: UseDelay - type: UseDelay
delay: 1.0 delay: 1.0
@@ -202,11 +214,13 @@
- type: Item - type: Item
sprite: Objects/Misc/carvings.rsi sprite: Objects/Misc/carvings.rsi
- type: EmitSoundOnThrow - type: EmitSoundOnThrow
sound: /Audio/Items/Toys/verygood.ogg sound:
path: /Audio/Items/Toys/verygood.ogg
- type: ItemCooldown - type: ItemCooldown
- type: LoopingSound - type: LoopingSound
- type: EmitSoundOnUse - type: EmitSoundOnUse
sound: /Audio/Items/Toys/verygood.ogg sound:
path: /Audio/Items/Toys/verygood.ogg
- type: UseDelay - type: UseDelay
delay: 1.0 delay: 1.0
@@ -222,11 +236,13 @@
- type: Item - type: Item
sprite: Objects/Misc/carvings.rsi sprite: Objects/Misc/carvings.rsi
- type: EmitSoundOnThrow - type: EmitSoundOnThrow
sound: /Audio/Items/Toys/imsorry.ogg sound:
path: /Audio/Items/Toys/imsorry.ogg
- type: ItemCooldown - type: ItemCooldown
- type: LoopingSound - type: LoopingSound
- type: EmitSoundOnUse - type: EmitSoundOnUse
sound: /Audio/Items/Toys/imsorry.ogg sound:
path: /Audio/Items/Toys/imsorry.ogg
- type: UseDelay - type: UseDelay
delay: 1.0 delay: 1.0
@@ -297,7 +313,8 @@
- type: ItemCooldown - type: ItemCooldown
- type: LoopingSound - type: LoopingSound
- type: EmitSoundOnUse - type: EmitSoundOnUse
sound: /Audio/Items/Toys/ian.ogg sound:
path: /Audio/Items/Toys/ian.ogg
- type: UseDelay - type: UseDelay
delay: 1.0 delay: 1.0