Files
tbd-station-14/Content.Shared/Speech/SpeechSystem.cs
metalgearsloth 0d7423c01d 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.
2022-12-27 18:03:25 +01:00

61 lines
1.8 KiB
C#

using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Speech
{
public sealed class SpeechSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SpeakAttemptEvent>(OnSpeakAttempt);
SubscribeLocalEvent<SpeechComponent, ComponentGetState>(OnSpeechGetState);
SubscribeLocalEvent<SpeechComponent, ComponentHandleState>(OnSpeechHandleState);
}
public void SetSpeech(EntityUid uid, bool value, SpeechComponent? component = null)
{
if (value && !Resolve(uid, ref component))
return;
component = EnsureComp<SpeechComponent>(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 SpeechComponent? speech) || !speech.Enabled)
args.Cancel();
}
[Serializable, NetSerializable]
private sealed class SpeechComponentState : ComponentState
{
public readonly bool Enabled;
public SpeechComponentState(bool enabled)
{
Enabled = enabled;
}
}
}
}