#4219 emit sound system add single sound support (#4237)

* Revert "#3935 implemented suggestions from PR"

This reverts commit a9b1c7b96333ca570067d6a9df1954481005892a.

* #4219 revert of single sound removal in EmitSoundSystem

* #4219 single sounds in EmitSoundSystem should work now

* #4219 some small project tweaks

* #4219 upgraded EmitSoundSystem to use SoundSpecifier

* #4219 pr tweaks

* #4219 pr tweak

* #4219 missing bike horn .yaml change
This commit is contained in:
Galactic Chimp
2021-07-25 13:37:01 +02:00
committed by GitHub
parent ef5e6a754c
commit 10af8efc0e
13 changed files with 97 additions and 122 deletions

View File

@@ -1,4 +1,4 @@
using Content.Server.Sound;
using Content.Server.Sound.Components;
using Robust.Shared.GameObjects;
namespace Content.Server.Interaction.Components

View File

@@ -1,6 +1,6 @@
using Content.Server.Clothing.Components;
using Content.Server.Items;
using Content.Server.Sound;
using Content.Server.Sound.Components;
using Content.Shared.ActionBlocker;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;

View File

@@ -1,15 +0,0 @@
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!;
}
}

View File

@@ -0,0 +1,22 @@
using Content.Shared.Sound;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Sound.Components
{
/// <summary>
/// Base sound emitter which defines most of the data fields.
/// Accepts both single sounds and sound collections.
/// </summary>
public abstract class BaseEmitSoundComponent : Component
{
[ViewVariables(VVAccess.ReadWrite)]
[DataField("sound")]
public SoundSpecifier Sound { get; set; } = default!;
[ViewVariables(VVAccess.ReadWrite)]
[DataField("variation")]
public float PitchVariation { get; set; } = 0.0f;
}
}

View File

@@ -1,6 +1,6 @@
using Robust.Shared.GameObjects;
namespace Content.Server.Sound
namespace Content.Server.Sound.Components
{
/// <summary>
/// Simple sound emitter that emits sound on ActivateInWorld

View File

@@ -1,6 +1,6 @@
using Robust.Shared.GameObjects;
namespace Content.Server.Sound
namespace Content.Server.Sound.Components
{
/// <summary>
/// Simple sound emitter that emits sound on LandEvent

View File

@@ -3,7 +3,7 @@ using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Network;
namespace Content.Server.Sound
namespace Content.Server.Sound.Components
{
[RegisterComponent]
public class LoopingLoopingSoundComponent : SharedLoopingSoundComponent

View File

@@ -1,55 +1,63 @@
using Content.Server.Interaction.Components;
using Content.Server.Sound.Components;
using Content.Server.Throwing;
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.Log;
using Robust.Shared.Player;
using Robust.Shared.Random;
namespace Content.Server.Sound
{
/// <summary>
/// Will play a sound on various events if the affected entity has a component derived from BaseEmitSoundComponent
/// </summary>
[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));
SubscribeLocalEvent<EmitSoundOnLandComponent, LandEvent>(HandleEmitSoundOnLand);
SubscribeLocalEvent<EmitSoundOnUseComponent, UseInHandEvent>(HandleEmitSoundOnUseInHand);
SubscribeLocalEvent<EmitSoundOnThrowComponent, ThrownEvent>(HandleEmitSoundOnThrown);
SubscribeLocalEvent<EmitSoundOnActivateComponent, ActivateInWorldEvent>(HandleEmitSoundOnActivateInWorld);
}
private void PlaySound(BaseEmitSoundComponent component)
private void HandleEmitSoundOnLand(EntityUid eUI, BaseEmitSoundComponent component, LandEvent arg)
{
PlayRandomSoundFromCollection(component);
TryEmitSound(component);
}
private void PlayRandomSoundFromCollection(BaseEmitSoundComponent component)
private void HandleEmitSoundOnUseInHand(EntityUid eUI, BaseEmitSoundComponent component, UseInHandEvent arg)
{
var file = SelectRandomSoundFromSoundCollection(component.SoundCollectionName!);
PlaySingleSound(file, component);
TryEmitSound(component);
}
private string SelectRandomSoundFromSoundCollection(string soundCollectionName)
private void HandleEmitSoundOnThrown(EntityUid eUI, BaseEmitSoundComponent component, ThrownEvent arg)
{
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(soundCollectionName);
return _random.Pick(soundCollection.PickFiles);
TryEmitSound(component);
}
private static void PlaySingleSound(string soundName, BaseEmitSoundComponent component)
private void HandleEmitSoundOnActivateInWorld(EntityUid eUI, BaseEmitSoundComponent component, ActivateInWorldEvent arg)
{
SoundSystem.Play(Filter.Pvs(component.Owner), soundName, component.Owner,
AudioHelpers.WithVariation(component.PitchVariation).WithVolume(-2f));
TryEmitSound(component);
}
private static void TryEmitSound(BaseEmitSoundComponent component)
{
if (!string.IsNullOrWhiteSpace(component.Sound.GetSound()))
{
SoundSystem.Play(Filter.Pvs(component.Owner), component.Sound.GetSound(), component.Owner, AudioHelpers.WithVariation(component.PitchVariation).WithVolume(-2f));
}
else
{
Logger.Warning($"{nameof(component)} Uid:{component.Owner.Uid} has no {nameof(component.Sound)} to play.");
}
}
}
}

View File

@@ -1,4 +1,4 @@
using Content.Server.Sound;
using Content.Server.Sound.Components;
using Robust.Shared.GameObjects;
namespace Content.Server.Throwing

View File

@@ -1,59 +0,0 @@
using Content.Shared.Audio;
using Content.Shared.Interaction;
using Content.Shared.Throwing;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Toys
{
[RegisterComponent]
public class ToysComponent : Component, IActivate, IUse, ILand
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
public override string Name => "Toys";
[ViewVariables]
[DataField("toySqueak")]
public string _soundCollectionName = "ToySqueak";
public void Squeak()
{
PlaySqueakEffect();
}
public void PlaySqueakEffect()
{
if (!string.IsNullOrWhiteSpace(_soundCollectionName))
{
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(_soundCollectionName);
var file = _random.Pick(soundCollection.PickFiles);
SoundSystem.Play(Filter.Pvs(Owner), file, Owner, AudioParams.Default);
}
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
Squeak();
}
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
{
Squeak();
return false;
}
void ILand.Land(LandEventArgs eventArgs)
{
Squeak();
}
}
}

View File

@@ -14,7 +14,8 @@
QuickEquip: false
- type: ItemCooldown
- type: EmitSoundOnUse
soundCollection: BikeHorn
sound:
collection: BikeHorn
semitoneVariation: 6
- type: UseDelay
delay: 0.5

View File

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

View File

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