using Content.Shared.Language;
using Content.Shared.Language.Events;
using Content.Shared.Language.Systems;
using Robust.Client;
using Robust.Shared.Console;
namespace Content.Client.Language.Systems;
///
/// Client-side language system.
///
///
/// Unlike the server, the client is not aware of other entities' languages; it's only notified about the entity that it posesses.
/// Due to that, this system stores such information in a static manner.
///
public sealed class LanguageSystem : SharedLanguageSystem
{
[Dependency] private readonly IBaseClient _client = default!;
///
/// The current language of the entity currently possessed by the player.
///
public string CurrentLanguage { get; private set; } = default!;
///
/// The list of languages the currently possessed entity can speak.
///
public List SpokenLanguages { get; private set; } = new();
///
/// The list of languages the currently possessed entity can understand.
///
public List UnderstoodLanguages { get; private set; } = new();
public override void Initialize()
{
base.Initialize();
SubscribeNetworkEvent(OnLanguagesUpdated);
_client.RunLevelChanged += OnRunLevelChanged;
}
private void OnLanguagesUpdated(LanguagesUpdatedMessage message)
{
CurrentLanguage = message.CurrentLanguage;
SpokenLanguages = message.Spoken;
UnderstoodLanguages = message.Understood;
}
private void OnRunLevelChanged(object? sender, RunLevelChangedEventArgs args)
{
// Request an update when entering a game
if (args.NewLevel == ClientRunLevel.InGame)
RequestStateUpdate();
}
///
/// Sends a network request to the server to update this system's state.
/// The server may ignore the said request if the player is not possessing an entity.
///
public void RequestStateUpdate()
{
RaiseNetworkEvent(new RequestLanguagesMessage());
}
public void RequestSetLanguage(LanguagePrototype language)
{
if (language.ID == CurrentLanguage)
return;
RaiseNetworkEvent(new LanguagesSetMessage(language.ID));
// May cause some minor desync...
// So to reduce the probability of desync, we replicate the change locally too
if (SpokenLanguages.Contains(language.ID))
CurrentLanguage = language.ID;
}
}