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

108 lines
3.4 KiB
C#
Raw Normal View History

2022-01-05 00:19:23 -08:00
using System.Linq;
using Content.Shared.Alert;
2023-07-13 20:20:46 +10:00
using Content.Shared.Mobs.Systems;
2022-01-05 00:19:23 -08:00
using JetBrains.Annotations;
using Robust.Client.GameObjects;
2022-01-05 00:19:23 -08:00
using Robust.Client.Player;
using Robust.Shared.GameStates;
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, PlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<AlertsComponent, PlayerDetachedEvent>(OnPlayerDetached);
2022-01-05 00:19:23 -08:00
SubscribeLocalEvent<AlertsComponent, ComponentHandleState>(ClientAlertsHandleState);
}
protected override void LoadPrototypes()
{
base.LoadPrototypes();
2022-01-05 00:19:23 -08:00
AlertOrder = _prototypeManager.EnumeratePrototypes<AlertOrderPrototype>().FirstOrDefault();
if (AlertOrder == null)
2023-07-13 20:20:46 +10:00
Log.Error("alert", "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.LocalPlayer?.ControlledEntity;
return ent is not null
? GetActiveAlerts(ent.Value)
: null;
}
}
2022-01-05 00:19:23 -08:00
protected override void AfterShowAlert(AlertsComponent alertsComponent)
{
if (_playerManager.LocalPlayer?.ControlledEntity != alertsComponent.Owner)
2022-01-05 00:19:23 -08:00
return;
SyncAlerts?.Invoke(this, alertsComponent.Alerts);
}
protected override void AfterClearAlert(AlertsComponent alertsComponent)
{
if (_playerManager.LocalPlayer?.ControlledEntity != alertsComponent.Owner)
2022-01-05 00:19:23 -08:00
return;
SyncAlerts?.Invoke(this, alertsComponent.Alerts);
}
private void ClientAlertsHandleState(EntityUid uid, AlertsComponent component, ref ComponentHandleState args)
{
var componentAlerts = (args.Current as AlertsComponentState)?.Alerts;
2023-07-13 20:20:46 +10:00
if (componentAlerts == null)
return;
2022-01-05 00:19:23 -08:00
2023-07-13 20:20:46 +10:00
component.Alerts = new Dictionary<AlertKey, AlertState>(componentAlerts);
2022-01-05 00:19:23 -08:00
if (_playerManager.LocalPlayer?.ControlledEntity == uid)
SyncAlerts?.Invoke(this, componentAlerts);
2022-01-05 00:19:23 -08:00
}
private void OnPlayerAttached(EntityUid uid, AlertsComponent component, PlayerAttachedEvent args)
2022-01-05 00:19:23 -08:00
{
if (_playerManager.LocalPlayer?.ControlledEntity != 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.LocalPlayer?.ControlledEntity != 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, PlayerDetachedEvent args)
2022-01-05 00:19:23 -08:00
{
ClearAlerts?.Invoke(this, EventArgs.Empty);
}
public void AlertClicked(AlertType alertType)
{
RaiseNetworkEvent(new ClickAlertEvent(alertType));
}
}