* Use new Subs.CVar helper Removes manual config OnValueChanged calls, removes need to remember to manually unsubscribe. This both reduces boilerplate and fixes many issues where subscriptions weren't removed on entity system shutdown. * Fix a bunch of warnings * More warning fixes * Use new DateTime serializer to get rid of ISerializationHooks in changelog code. * Get rid of some more ISerializationHooks for enums * And a little more * Apply suggestions from code review Co-authored-by: 0x6273 <0x40@keemail.me> --------- Co-authored-by: 0x6273 <0x40@keemail.me>
100 lines
3.0 KiB
C#
100 lines
3.0 KiB
C#
using System.Linq;
|
|
using Content.Shared.Alert;
|
|
using JetBrains.Annotations;
|
|
using Robust.Client.Player;
|
|
using Robust.Shared.Player;
|
|
using Robust.Shared.Prototypes;
|
|
|
|
namespace Content.Client.Alerts;
|
|
|
|
[UsedImplicitly]
|
|
public sealed class ClientAlertsSystem : AlertsSystem
|
|
{
|
|
public AlertOrderPrototype? AlertOrder { get; set; }
|
|
|
|
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
|
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
|
|
|
public event EventHandler? ClearAlerts;
|
|
public event EventHandler<IReadOnlyDictionary<AlertKey, AlertState>>? SyncAlerts;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
SubscribeLocalEvent<AlertsComponent, LocalPlayerAttachedEvent>(OnPlayerAttached);
|
|
SubscribeLocalEvent<AlertsComponent, LocalPlayerDetachedEvent>(OnPlayerDetached);
|
|
|
|
SubscribeLocalEvent<AlertsComponent, AfterAutoHandleStateEvent>(ClientAlertsHandleState);
|
|
}
|
|
protected override void LoadPrototypes()
|
|
{
|
|
base.LoadPrototypes();
|
|
|
|
AlertOrder = _prototypeManager.EnumeratePrototypes<AlertOrderPrototype>().FirstOrDefault();
|
|
if (AlertOrder == null)
|
|
Log.Error("alert", "no alertOrder prototype found, alerts will be in random order");
|
|
}
|
|
|
|
public IReadOnlyDictionary<AlertKey, AlertState>? ActiveAlerts
|
|
{
|
|
get
|
|
{
|
|
var ent = _playerManager.LocalEntity;
|
|
return ent is not null
|
|
? GetActiveAlerts(ent.Value)
|
|
: null;
|
|
}
|
|
}
|
|
|
|
protected override void AfterShowAlert(Entity<AlertsComponent> alerts)
|
|
{
|
|
if (_playerManager.LocalEntity != alerts.Owner)
|
|
return;
|
|
|
|
SyncAlerts?.Invoke(this, alerts.Comp.Alerts);
|
|
}
|
|
|
|
protected override void AfterClearAlert(Entity<AlertsComponent> alertsComponent)
|
|
{
|
|
if (_playerManager.LocalEntity != alertsComponent.Owner)
|
|
return;
|
|
|
|
SyncAlerts?.Invoke(this, alertsComponent.Comp.Alerts);
|
|
}
|
|
|
|
private void ClientAlertsHandleState(EntityUid uid, AlertsComponent component, ref AfterAutoHandleStateEvent args)
|
|
{
|
|
if (_playerManager.LocalEntity == uid)
|
|
SyncAlerts?.Invoke(this, component.Alerts);
|
|
}
|
|
|
|
private void OnPlayerAttached(EntityUid uid, AlertsComponent component, LocalPlayerAttachedEvent args)
|
|
{
|
|
if (_playerManager.LocalEntity != uid)
|
|
return;
|
|
|
|
SyncAlerts?.Invoke(this, component.Alerts);
|
|
}
|
|
|
|
protected override void HandleComponentShutdown(EntityUid uid, AlertsComponent component, ComponentShutdown args)
|
|
{
|
|
base.HandleComponentShutdown(uid, component, args);
|
|
|
|
if (_playerManager.LocalEntity != uid)
|
|
return;
|
|
|
|
ClearAlerts?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
private void OnPlayerDetached(EntityUid uid, AlertsComponent component, LocalPlayerDetachedEvent args)
|
|
{
|
|
ClearAlerts?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
|
|
public void AlertClicked(AlertType alertType)
|
|
{
|
|
RaiseNetworkEvent(new ClickAlertEvent(alertType));
|
|
}
|
|
}
|