100 lines
3.1 KiB
C#
100 lines
3.1 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.LocalPlayer?.ControlledEntity;
|
|
return ent is not null
|
|
? GetActiveAlerts(ent.Value)
|
|
: null;
|
|
}
|
|
}
|
|
|
|
protected override void AfterShowAlert(Entity<AlertsComponent> alerts)
|
|
{
|
|
if (_playerManager.LocalPlayer?.ControlledEntity != alerts.Owner)
|
|
return;
|
|
|
|
SyncAlerts?.Invoke(this, alerts.Comp.Alerts);
|
|
}
|
|
|
|
protected override void AfterClearAlert(Entity<AlertsComponent> alertsComponent)
|
|
{
|
|
if (_playerManager.LocalPlayer?.ControlledEntity != alertsComponent.Owner)
|
|
return;
|
|
|
|
SyncAlerts?.Invoke(this, alertsComponent.Comp.Alerts);
|
|
}
|
|
|
|
private void ClientAlertsHandleState(EntityUid uid, AlertsComponent component, ref AfterAutoHandleStateEvent args)
|
|
{
|
|
if (_playerManager.LocalPlayer?.ControlledEntity == uid)
|
|
SyncAlerts?.Invoke(this, component.Alerts);
|
|
}
|
|
|
|
private void OnPlayerAttached(EntityUid uid, AlertsComponent component, LocalPlayerAttachedEvent args)
|
|
{
|
|
if (_playerManager.LocalPlayer?.ControlledEntity != uid)
|
|
return;
|
|
|
|
SyncAlerts?.Invoke(this, component.Alerts);
|
|
}
|
|
|
|
protected override void HandleComponentShutdown(EntityUid uid, AlertsComponent component, ComponentShutdown args)
|
|
{
|
|
base.HandleComponentShutdown(uid, component, args);
|
|
|
|
if (_playerManager.LocalPlayer?.ControlledEntity != 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));
|
|
}
|
|
}
|