From 0d7423c01d0311e72c7fe8dbf969ca6b996010d9 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Wed, 28 Dec 2022 04:03:25 +1100 Subject: [PATCH] Cleanup speech and emoting comps (#13194) Networks speech and removes the shared prefix from emoting. I have no idea if emoting is even being used or plan to be used in the interim. --- .../Mind/Commands/MakeSentientCommand.cs | 4 +- Content.Server/Speech/SpeechNoiseSystem.cs | 4 +- .../SurveillanceCameraSpeakerSystem.cs | 2 +- Content.Shared/Emoting/EmoteSystem.cs | 46 ++++++++++++++++- Content.Shared/Emoting/EmotingComponent.cs | 12 +++++ .../Emoting/SharedEmotingComponent.cs | 49 ------------------- ...dSpeechComponent.cs => SpeechComponent.cs} | 22 +++------ Content.Shared/Speech/SpeechSystem.cs | 46 ++++++++++++++++- 8 files changed, 112 insertions(+), 73 deletions(-) create mode 100644 Content.Shared/Emoting/EmotingComponent.cs delete mode 100644 Content.Shared/Emoting/SharedEmotingComponent.cs rename Content.Shared/Speech/{SharedSpeechComponent.cs => SpeechComponent.cs} (73%) diff --git a/Content.Server/Mind/Commands/MakeSentientCommand.cs b/Content.Server/Mind/Commands/MakeSentientCommand.cs index 0df1b5d597..d7f7f7087b 100644 --- a/Content.Server/Mind/Commands/MakeSentientCommand.cs +++ b/Content.Server/Mind/Commands/MakeSentientCommand.cs @@ -54,8 +54,8 @@ namespace Content.Server.Mind.Commands if (allowSpeech) { - entityManager.EnsureComponent(uid); - entityManager.EnsureComponent(uid); + entityManager.EnsureComponent(uid); + entityManager.EnsureComponent(uid); } entityManager.EnsureComponent(uid); diff --git a/Content.Server/Speech/SpeechNoiseSystem.cs b/Content.Server/Speech/SpeechNoiseSystem.cs index 7c0a179a35..c81d17caf2 100644 --- a/Content.Server/Speech/SpeechNoiseSystem.cs +++ b/Content.Server/Speech/SpeechNoiseSystem.cs @@ -19,10 +19,10 @@ namespace Content.Server.Speech { base.Initialize(); - SubscribeLocalEvent(OnEntitySpoke); + SubscribeLocalEvent(OnEntitySpoke); } - private void OnEntitySpoke(EntityUid uid, SharedSpeechComponent component, EntitySpokeEvent args) + private void OnEntitySpoke(EntityUid uid, SpeechComponent component, EntitySpokeEvent args) { if (component.SpeechSounds == null) return; diff --git a/Content.Server/SurveillanceCamera/Systems/SurveillanceCameraSpeakerSystem.cs b/Content.Server/SurveillanceCamera/Systems/SurveillanceCameraSpeakerSystem.cs index 8d6bce6ab6..0d3d96572c 100644 --- a/Content.Server/SurveillanceCamera/Systems/SurveillanceCameraSpeakerSystem.cs +++ b/Content.Server/SurveillanceCamera/Systems/SurveillanceCameraSpeakerSystem.cs @@ -36,7 +36,7 @@ public sealed class SurveillanceCameraSpeakerSystem : EntitySystem // this part's mostly copied from speech if (time - component.LastSoundPlayed < cd - && TryComp(args.Speaker, out var speech) + && TryComp(args.Speaker, out var speech) && speech.SpeechSounds != null && _prototypeManager.TryIndex(speech.SpeechSounds, out SpeechSoundsPrototype? speechProto)) { diff --git a/Content.Shared/Emoting/EmoteSystem.cs b/Content.Shared/Emoting/EmoteSystem.cs index b710603c35..942c6b9de0 100644 --- a/Content.Shared/Emoting/EmoteSystem.cs +++ b/Content.Shared/Emoting/EmoteSystem.cs @@ -1,4 +1,7 @@ -namespace Content.Shared.Emoting +using Robust.Shared.GameStates; +using Robust.Shared.Serialization; + +namespace Content.Shared.Emoting { public sealed class EmoteSystem : EntitySystem { @@ -7,12 +10,51 @@ base.Initialize(); SubscribeLocalEvent(OnEmoteAttempt); + SubscribeLocalEvent(OnEmotingGetState); + SubscribeLocalEvent(OnEmotingHandleState); + } + + public void SetEmoting(EntityUid uid, bool value, EmotingComponent? component = null) + { + if (value && !Resolve(uid, ref component)) + return; + + component = EnsureComp(uid); + + if (component.Enabled == value) + return; + + Dirty(component); + } + + private void OnEmotingHandleState(EntityUid uid, EmotingComponent component, ref ComponentHandleState args) + { + if (args.Current is not EmotingComponentState state) + return; + + component.Enabled = state.Enabled; + } + + private void OnEmotingGetState(EntityUid uid, EmotingComponent component, ref ComponentGetState args) + { + args.State = new EmotingComponentState(component.Enabled); } private void OnEmoteAttempt(EmoteAttemptEvent args) { - if (!TryComp(args.Uid, out SharedEmotingComponent? emote) || !emote.Enabled) + if (!TryComp(args.Uid, out EmotingComponent? emote) || !emote.Enabled) args.Cancel(); } + + [Serializable, NetSerializable] + private sealed class EmotingComponentState : ComponentState + { + public bool Enabled { get; } + + public EmotingComponentState(bool enabled) + { + Enabled = enabled; + } + } } } diff --git a/Content.Shared/Emoting/EmotingComponent.cs b/Content.Shared/Emoting/EmotingComponent.cs new file mode 100644 index 0000000000..2a1bca85b2 --- /dev/null +++ b/Content.Shared/Emoting/EmotingComponent.cs @@ -0,0 +1,12 @@ +using Robust.Shared.GameStates; + +namespace Content.Shared.Emoting +{ + [RegisterComponent, NetworkedComponent] + public sealed class EmotingComponent : Component + { + [DataField("enabled"), Access(typeof(EmoteSystem), + Friend = AccessPermissions.ReadWrite, + Other = AccessPermissions.Read)] public bool Enabled = true; + } +} diff --git a/Content.Shared/Emoting/SharedEmotingComponent.cs b/Content.Shared/Emoting/SharedEmotingComponent.cs deleted file mode 100644 index 5fb167943c..0000000000 --- a/Content.Shared/Emoting/SharedEmotingComponent.cs +++ /dev/null @@ -1,49 +0,0 @@ -using Robust.Shared.GameStates; -using Robust.Shared.Serialization; - -namespace Content.Shared.Emoting -{ - [RegisterComponent, NetworkedComponent] - public sealed class SharedEmotingComponent : Component - { - [DataField("enabled")] private bool _enabled = true; - - [ViewVariables(VVAccess.ReadWrite)] - public bool Enabled - { - get => _enabled; - set - { - if (_enabled == value) - return; - - _enabled = value; - Dirty(); - } - } - - public override ComponentState GetComponentState() - { - return new EmotingComponentState(Enabled); - } - - public override void HandleComponentState(ComponentState? curState, ComponentState? nextState) - { - if (curState is not EmotingComponentState emoting) - return; - - _enabled = emoting.Enabled; - } - - [Serializable, NetSerializable] - private sealed class EmotingComponentState : ComponentState - { - public bool Enabled { get; } - - public EmotingComponentState(bool enabled) - { - Enabled = enabled; - } - } - } -} diff --git a/Content.Shared/Speech/SharedSpeechComponent.cs b/Content.Shared/Speech/SpeechComponent.cs similarity index 73% rename from Content.Shared/Speech/SharedSpeechComponent.cs rename to Content.Shared/Speech/SpeechComponent.cs index d9378eb280..52f32fef37 100644 --- a/Content.Shared/Speech/SharedSpeechComponent.cs +++ b/Content.Shared/Speech/SpeechComponent.cs @@ -1,5 +1,6 @@ using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; using Robust.Shared.Audio; +using Robust.Shared.GameStates; namespace Content.Shared.Speech { @@ -7,11 +8,13 @@ namespace Content.Shared.Speech /// Component required for entities to be able to speak. (TODO: Entities can speak fine without this, this only forbids them speak if they have it and enabled is false.) /// Contains the option to let entities make noise when speaking, datafields for the sounds in question, and relevant AudioParams. /// - [RegisterComponent] - public sealed class SharedSpeechComponent : Component + [RegisterComponent, NetworkedComponent] + public sealed class SpeechComponent : Component { - [DataField("enabled")] - private bool _enabled = true; + [DataField("enabled"), Access(typeof(SpeechSystem), + Friend = AccessPermissions.ReadWrite, + Other = AccessPermissions.Read)] + public bool Enabled = true; [ViewVariables(VVAccess.ReadWrite)] [DataField("speechSounds", customTypeSerializer:typeof(PrototypeIdSerializer))] @@ -25,16 +28,5 @@ namespace Content.Shared.Speech public float SoundCooldownTime { get; set; } = 0.5f; public TimeSpan LastTimeSoundPlayed = TimeSpan.Zero; - - public bool Enabled - { - get => _enabled; - set - { - if (_enabled == value) return; - _enabled = value; - Dirty(); - } - } } } diff --git a/Content.Shared/Speech/SpeechSystem.cs b/Content.Shared/Speech/SpeechSystem.cs index 8b7db74699..b1a3a39cd3 100644 --- a/Content.Shared/Speech/SpeechSystem.cs +++ b/Content.Shared/Speech/SpeechSystem.cs @@ -1,4 +1,7 @@ -namespace Content.Shared.Speech +using Robust.Shared.GameStates; +using Robust.Shared.Serialization; + +namespace Content.Shared.Speech { public sealed class SpeechSystem : EntitySystem { @@ -7,12 +10,51 @@ base.Initialize(); SubscribeLocalEvent(OnSpeakAttempt); + SubscribeLocalEvent(OnSpeechGetState); + SubscribeLocalEvent(OnSpeechHandleState); + } + + public void SetSpeech(EntityUid uid, bool value, SpeechComponent? component = null) + { + if (value && !Resolve(uid, ref component)) + return; + + component = EnsureComp(uid); + + if (component.Enabled == value) + return; + + Dirty(component); + } + + private void OnSpeechHandleState(EntityUid uid, SpeechComponent component, ref ComponentHandleState args) + { + if (args.Current is not SpeechComponentState state) + return; + + component.Enabled = state.Enabled; + } + + private void OnSpeechGetState(EntityUid uid, SpeechComponent component, ref ComponentGetState args) + { + args.State = new SpeechComponentState(component.Enabled); } private void OnSpeakAttempt(SpeakAttemptEvent args) { - if (!TryComp(args.Uid, out SharedSpeechComponent? speech) || !speech.Enabled) + if (!TryComp(args.Uid, out SpeechComponent? speech) || !speech.Enabled) args.Cancel(); } + + [Serializable, NetSerializable] + private sealed class SpeechComponentState : ComponentState + { + public readonly bool Enabled; + + public SpeechComponentState(bool enabled) + { + Enabled = enabled; + } + } } }