Files
OldThink/Content.Client/Alerts/ClientAlertsSystem.cs

99 lines
2.9 KiB
C#
Raw Normal View History

2022-01-05 00:19:23 -08:00
using System.Linq;
using Content.Shared.Alert;
using JetBrains.Annotations;
using Robust.Client.Player;
using Robust.Shared.Player;
2022-01-05 00:19:23 -08:00
using Robust.Shared.Prototypes;
2022-01-05 00:19:23 -08:00
namespace Content.Client.Alerts;
[UsedImplicitly]
public sealed class ClientAlertsSystem : AlertsSystem
{
2022-01-05 00:19:23 -08:00
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()
{
2022-01-05 00:19:23 -08:00
base.Initialize();
SubscribeLocalEvent<AlertsComponent, LocalPlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<AlertsComponent, LocalPlayerDetachedEvent>(OnPlayerDetached);
2022-01-05 00:19:23 -08:00
SubscribeLocalEvent<AlertsComponent, AfterAutoHandleStateEvent>(ClientAlertsHandleState);
2022-01-05 00:19:23 -08:00
}
protected override void LoadPrototypes()
{
base.LoadPrototypes();
2022-01-05 00:19:23 -08:00
AlertOrder = _prototypeManager.EnumeratePrototypes<AlertOrderPrototype>().FirstOrDefault();
if (AlertOrder == null)
Log.Error("No alertOrder prototype found, alerts will be in random order");
2022-01-05 00:19:23 -08:00
}
2022-01-05 00:19:23 -08:00
public IReadOnlyDictionary<AlertKey, AlertState>? ActiveAlerts
{
get
{
var ent = _playerManager.LocalEntity;
2022-01-05 00:19:23 -08:00
return ent is not null
? GetActiveAlerts(ent.Value)
: null;
}
}
2022-01-05 00:19:23 -08:00
protected override void AfterShowAlert(Entity<AlertsComponent> alerts)
2022-01-05 00:19:23 -08:00
{
UpdateHud(alerts);
}
2022-01-05 00:19:23 -08:00
protected override void AfterClearAlert(Entity<AlertsComponent> alerts)
{
UpdateHud(alerts);
}
private void ClientAlertsHandleState(Entity<AlertsComponent> alerts, ref AfterAutoHandleStateEvent args)
{
UpdateHud(alerts);
2022-01-05 00:19:23 -08:00
}
private void UpdateHud(Entity<AlertsComponent> entity)
2022-01-05 00:19:23 -08:00
{
if (_playerManager.LocalEntity == entity.Owner)
SyncAlerts?.Invoke(this, entity.Comp.Alerts);
2022-01-05 00:19:23 -08:00
}
private void OnPlayerAttached(EntityUid uid, AlertsComponent component, LocalPlayerAttachedEvent args)
2022-01-05 00:19:23 -08:00
{
if (_playerManager.LocalEntity != uid)
return;
SyncAlerts?.Invoke(this, component.Alerts);
2022-01-05 00:19:23 -08:00
}
protected override void HandleComponentShutdown(EntityUid uid, AlertsComponent component, ComponentShutdown args)
2022-01-05 00:19:23 -08:00
{
base.HandleComponentShutdown(uid, component, args);
if (_playerManager.LocalEntity != uid)
return;
2022-01-05 00:19:23 -08:00
ClearAlerts?.Invoke(this, EventArgs.Empty);
2022-01-05 00:19:23 -08:00
}
private void OnPlayerDetached(EntityUid uid, AlertsComponent component, LocalPlayerDetachedEvent args)
2022-01-05 00:19:23 -08:00
{
ClearAlerts?.Invoke(this, EventArgs.Empty);
}
public void AlertClicked(AlertType alertType)
{
RaiseNetworkEvent(new ClickAlertEvent(alertType));
}
}