Sound Specifiers. (#4239)
* Adds SoundSpecifier and a type serializer. * DiceComponent makes use of SoundSpecifier. * When rider autorefactoring decices to add field: * Remove SoundEmptySpecifier, use nullable everywhere
This commit is contained in:
committed by
GitHub
parent
9bedfe79be
commit
77ae49fd9c
@@ -1,6 +1,7 @@
|
|||||||
using Content.Shared.Audio;
|
using Content.Shared.Audio;
|
||||||
using Content.Shared.Examine;
|
using Content.Shared.Examine;
|
||||||
using Content.Shared.Interaction;
|
using Content.Shared.Interaction;
|
||||||
|
using Content.Shared.Sound;
|
||||||
using Content.Shared.Throwing;
|
using Content.Shared.Throwing;
|
||||||
using Robust.Server.GameObjects;
|
using Robust.Server.GameObjects;
|
||||||
using Robust.Shared.Audio;
|
using Robust.Shared.Audio;
|
||||||
@@ -24,15 +25,16 @@ namespace Content.Server.Dice
|
|||||||
|
|
||||||
public override string Name => "Dice";
|
public override string Name => "Dice";
|
||||||
|
|
||||||
[DataField("step")]
|
|
||||||
private int _step = 1;
|
|
||||||
private int _sides = 20;
|
private int _sides = 20;
|
||||||
private int _currentSide = 20;
|
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
[DataField("diceSoundCollection")]
|
[DataField("sound")]
|
||||||
public string _soundCollectionName = "dice";
|
private readonly SoundSpecifier _sound = new SoundCollectionSpecifier("Dice");
|
||||||
|
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
public int Step => _step;
|
[DataField("step")]
|
||||||
|
public int Step { get; } = 1;
|
||||||
|
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
[DataField("sides")]
|
[DataField("sides")]
|
||||||
public int Sides
|
public int Sides
|
||||||
@@ -41,29 +43,28 @@ namespace Content.Server.Dice
|
|||||||
set
|
set
|
||||||
{
|
{
|
||||||
_sides = value;
|
_sides = value;
|
||||||
_currentSide = value;
|
CurrentSide = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ViewVariables]
|
[ViewVariables]
|
||||||
public int CurrentSide => _currentSide;
|
public int CurrentSide { get; private set; } = 20;
|
||||||
|
|
||||||
public void Roll()
|
public void Roll()
|
||||||
{
|
{
|
||||||
_currentSide = _random.Next(1, (_sides/_step)+1) * _step;
|
CurrentSide = _random.Next(1, (_sides/Step)+1) * Step;
|
||||||
if (!Owner.TryGetComponent(out SpriteComponent? sprite)) return;
|
|
||||||
sprite.LayerSetState(0, $"d{_sides}{_currentSide}");
|
|
||||||
PlayDiceEffect();
|
PlayDiceEffect();
|
||||||
|
|
||||||
|
if (!Owner.TryGetComponent(out SpriteComponent? sprite))
|
||||||
|
return;
|
||||||
|
|
||||||
|
sprite.LayerSetState(0, $"d{_sides}{CurrentSide}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PlayDiceEffect()
|
public void PlayDiceEffect()
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrWhiteSpace(_soundCollectionName))
|
SoundSystem.Play(Filter.Pvs(Owner), _sound.GetSound(), Owner, AudioParams.Default);
|
||||||
{
|
|
||||||
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)
|
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||||
@@ -89,7 +90,7 @@ namespace Content.Server.Dice
|
|||||||
("sidesAmount", _sides))
|
("sidesAmount", _sides))
|
||||||
+ "\n" +
|
+ "\n" +
|
||||||
Loc.GetString("dice-component-on-examine-message-part-2",
|
Loc.GetString("dice-component-on-examine-message-part-2",
|
||||||
("currentSide", _currentSide)));
|
("currentSide", CurrentSide)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
67
Content.Shared/Sound/SoundSpecifier.cs
Normal file
67
Content.Shared/Sound/SoundSpecifier.cs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
using System;
|
||||||
|
using Content.Shared.Audio;
|
||||||
|
using Robust.Shared;
|
||||||
|
using Robust.Shared.Serialization.Manager.Attributes;
|
||||||
|
using Robust.Shared.Serialization.TypeSerializers.Implementations;
|
||||||
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||||
|
using Robust.Shared.Utility;
|
||||||
|
|
||||||
|
namespace Content.Shared.Sound
|
||||||
|
{
|
||||||
|
[DataDefinition]
|
||||||
|
public abstract class SoundSpecifier
|
||||||
|
{
|
||||||
|
public abstract string GetSound();
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataDefinition]
|
||||||
|
public sealed class SoundPathSpecifier : SoundSpecifier
|
||||||
|
{
|
||||||
|
public const string Node = "path";
|
||||||
|
|
||||||
|
[DataField(Node, customTypeSerializer:typeof(ResourcePathSerializer), required:true)]
|
||||||
|
public ResourcePath? Path { get; }
|
||||||
|
|
||||||
|
public SoundPathSpecifier()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public SoundPathSpecifier(string path)
|
||||||
|
{
|
||||||
|
Path = new ResourcePath(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SoundPathSpecifier(ResourcePath path)
|
||||||
|
{
|
||||||
|
Path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string GetSound()
|
||||||
|
{
|
||||||
|
return Path == null ? string.Empty : Path.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataDefinition]
|
||||||
|
public sealed class SoundCollectionSpecifier : SoundSpecifier
|
||||||
|
{
|
||||||
|
public const string Node = "collection";
|
||||||
|
|
||||||
|
[DataField(Node, customTypeSerializer:typeof(PrototypeIdSerializer<SoundCollectionPrototype>), required:true)]
|
||||||
|
public string? Collection { get; }
|
||||||
|
|
||||||
|
public SoundCollectionSpecifier()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public SoundCollectionSpecifier(string collection)
|
||||||
|
{
|
||||||
|
Collection = collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string GetSound()
|
||||||
|
{
|
||||||
|
return Collection == null ? string.Empty : AudioHelpers.GetRandomFileFromSoundCollection(Collection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
48
Content.Shared/Sound/SoundSpecifierTypeSerializer.cs
Normal file
48
Content.Shared/Sound/SoundSpecifierTypeSerializer.cs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
using System;
|
||||||
|
using Robust.Shared.IoC;
|
||||||
|
using Robust.Shared.Serialization.Manager;
|
||||||
|
using Robust.Shared.Serialization.Manager.Attributes;
|
||||||
|
using Robust.Shared.Serialization.Manager.Result;
|
||||||
|
using Robust.Shared.Serialization.Markdown.Mapping;
|
||||||
|
using Robust.Shared.Serialization.Markdown.Validation;
|
||||||
|
using Robust.Shared.Serialization.TypeSerializers.Interfaces;
|
||||||
|
|
||||||
|
namespace Content.Shared.Sound
|
||||||
|
{
|
||||||
|
[TypeSerializer]
|
||||||
|
public class SoundSpecifierTypeSerializer : ITypeReader<SoundSpecifier, MappingDataNode>
|
||||||
|
{
|
||||||
|
private Type GetType(MappingDataNode node)
|
||||||
|
{
|
||||||
|
var hasPath = node.Has(SoundPathSpecifier.Node);
|
||||||
|
var hasCollection = node.Has(SoundCollectionSpecifier.Node);
|
||||||
|
|
||||||
|
if (hasPath || !(hasPath ^ hasCollection))
|
||||||
|
return typeof(SoundPathSpecifier);
|
||||||
|
|
||||||
|
if (hasCollection)
|
||||||
|
return typeof(SoundCollectionSpecifier);
|
||||||
|
|
||||||
|
return typeof(SoundPathSpecifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeserializationResult Read(ISerializationManager serializationManager, MappingDataNode node,
|
||||||
|
IDependencyCollection dependencies, bool skipHook, ISerializationContext? context = null)
|
||||||
|
{
|
||||||
|
var type = GetType(node);
|
||||||
|
return serializationManager.Read(type, node, context, skipHook);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValidationNode Validate(ISerializationManager serializationManager, MappingDataNode node,
|
||||||
|
IDependencyCollection dependencies, ISerializationContext? context = null)
|
||||||
|
{
|
||||||
|
if (node.Has(SoundPathSpecifier.Node) && node.Has(SoundCollectionSpecifier.Node))
|
||||||
|
return new ErrorNode(node, "You can only specify either a sound path or a sound collection!");
|
||||||
|
|
||||||
|
if (!node.Has(SoundPathSpecifier.Node) && !node.Has(SoundCollectionSpecifier.Node))
|
||||||
|
return new ErrorNode(node, "You need to specify either a sound path or a sound collection!");
|
||||||
|
|
||||||
|
return serializationManager.ValidateNode(GetType(node), node, context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,6 @@
|
|||||||
id: BaseDice
|
id: BaseDice
|
||||||
components:
|
components:
|
||||||
- type: Dice
|
- type: Dice
|
||||||
- type: LoopingSound
|
|
||||||
- type: Sprite
|
- type: Sprite
|
||||||
sprite: Objects/Fun/dice.rsi
|
sprite: Objects/Fun/dice.rsi
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
- type: soundCollection
|
- type: soundCollection
|
||||||
id: dice
|
id: Dice
|
||||||
files:
|
files:
|
||||||
- /Audio/Items/Dice/dice1.ogg
|
- /Audio/Items/Dice/dice1.ogg
|
||||||
- /Audio/Items/Dice/dice2.ogg
|
- /Audio/Items/Dice/dice2.ogg
|
||||||
|
|||||||
Reference in New Issue
Block a user