2022-01-05 00:19:23 -08:00
|
|
|
using System.Linq;
|
|
|
|
|
using Content.Shared.Alert;
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using Robust.Client.Player;
|
2025-05-02 18:22:29 +10:00
|
|
|
using Robust.Client.UserInterface;
|
2024-11-09 01:28:24 +01:00
|
|
|
using Robust.Shared.GameStates;
|
2023-11-11 13:08:10 +11:00
|
|
|
using Robust.Shared.Player;
|
2022-01-05 00:19:23 -08:00
|
|
|
using Robust.Shared.Prototypes;
|
2021-06-18 01:49:18 -07:00
|
|
|
|
2022-01-05 00:19:23 -08:00
|
|
|
namespace Content.Client.Alerts;
|
|
|
|
|
|
|
|
|
|
[UsedImplicitly]
|
2022-09-11 18:56:21 -07:00
|
|
|
public sealed class ClientAlertsSystem : AlertsSystem
|
2021-06-18 01:49:18 -07:00
|
|
|
{
|
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!;
|
2025-05-02 18:22:29 +10:00
|
|
|
[Dependency] private readonly IUserInterfaceManager _ui = default!;
|
2022-01-05 00:19:23 -08:00
|
|
|
|
|
|
|
|
public event EventHandler? ClearAlerts;
|
|
|
|
|
public event EventHandler<IReadOnlyDictionary<AlertKey, AlertState>>? SyncAlerts;
|
|
|
|
|
|
|
|
|
|
public override void Initialize()
|
2021-06-18 01:49:18 -07:00
|
|
|
{
|
2022-01-05 00:19:23 -08:00
|
|
|
base.Initialize();
|
|
|
|
|
|
2023-10-28 09:59:53 +11:00
|
|
|
SubscribeLocalEvent<AlertsComponent, LocalPlayerAttachedEvent>(OnPlayerAttached);
|
|
|
|
|
SubscribeLocalEvent<AlertsComponent, LocalPlayerDetachedEvent>(OnPlayerDetached);
|
2024-11-09 01:28:24 +01:00
|
|
|
SubscribeLocalEvent<AlertsComponent, ComponentHandleState>(OnHandleState);
|
2022-01-05 00:19:23 -08:00
|
|
|
}
|
2025-05-02 18:22:29 +10:00
|
|
|
|
|
|
|
|
protected override void HandledAlert()
|
|
|
|
|
{
|
|
|
|
|
_ui.ClickSound();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 00:19:23 -08:00
|
|
|
protected override void LoadPrototypes()
|
|
|
|
|
{
|
|
|
|
|
base.LoadPrototypes();
|
2022-02-16 00:23:23 -07:00
|
|
|
|
2022-01-05 00:19:23 -08:00
|
|
|
AlertOrder = _prototypeManager.EnumeratePrototypes<AlertOrderPrototype>().FirstOrDefault();
|
|
|
|
|
if (AlertOrder == null)
|
2024-03-10 01:15:13 +01:00
|
|
|
Log.Error("No alertOrder prototype found, alerts will be in random order");
|
2022-01-05 00:19:23 -08:00
|
|
|
}
|
2021-06-18 01:49:18 -07:00
|
|
|
|
2022-01-05 00:19:23 -08:00
|
|
|
public IReadOnlyDictionary<AlertKey, AlertState>? ActiveAlerts
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2024-02-13 22:48:39 +01:00
|
|
|
var ent = _playerManager.LocalEntity;
|
2022-01-05 00:19:23 -08:00
|
|
|
return ent is not null
|
|
|
|
|
? GetActiveAlerts(ent.Value)
|
|
|
|
|
: null;
|
2021-06-18 01:49:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-05 00:19:23 -08:00
|
|
|
|
2024-11-09 01:28:24 +01:00
|
|
|
private void OnHandleState(Entity<AlertsComponent> alerts, ref ComponentHandleState args)
|
2022-01-05 00:19:23 -08:00
|
|
|
{
|
2024-11-09 01:28:24 +01:00
|
|
|
if (args.Current is not AlertComponentState cast)
|
|
|
|
|
return;
|
|
|
|
|
|
2025-04-29 15:32:18 +02:00
|
|
|
// Save all client-sided alerts to later put back in
|
|
|
|
|
var clientAlerts = new Dictionary<AlertKey, AlertState>();
|
|
|
|
|
foreach (var alert in alerts.Comp.Alerts)
|
|
|
|
|
{
|
|
|
|
|
if (alert.Key.AlertType != null && TryGet(alert.Key.AlertType.Value, out var alertProto))
|
|
|
|
|
{
|
|
|
|
|
if (alertProto.ClientHandled)
|
|
|
|
|
clientAlerts[alert.Key] = alert.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-22 22:41:16 +11:00
|
|
|
alerts.Comp.Alerts = new(cast.Alerts);
|
2024-11-09 01:28:24 +01:00
|
|
|
|
2025-04-29 15:32:18 +02:00
|
|
|
foreach (var alert in clientAlerts)
|
|
|
|
|
{
|
|
|
|
|
alerts.Comp.Alerts[alert.Key] = alert.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 14:30:56 +11:00
|
|
|
UpdateHud(alerts);
|
2024-02-03 14:36:09 +11:00
|
|
|
}
|
2022-01-05 00:19:23 -08:00
|
|
|
|
2024-11-09 01:28:24 +01:00
|
|
|
protected override void AfterShowAlert(Entity<AlertsComponent> alerts)
|
2024-02-03 14:36:09 +11:00
|
|
|
{
|
2024-03-19 14:30:56 +11:00
|
|
|
UpdateHud(alerts);
|
|
|
|
|
}
|
2024-02-03 10:32:30 -08:00
|
|
|
|
2024-11-09 01:28:24 +01:00
|
|
|
protected override void AfterClearAlert(Entity<AlertsComponent> alerts)
|
2024-03-19 14:30:56 +11:00
|
|
|
{
|
|
|
|
|
UpdateHud(alerts);
|
2022-01-05 00:19:23 -08:00
|
|
|
}
|
|
|
|
|
|
2024-03-19 14:30:56 +11:00
|
|
|
private void UpdateHud(Entity<AlertsComponent> entity)
|
2022-01-05 00:19:23 -08:00
|
|
|
{
|
2024-03-19 14:30:56 +11:00
|
|
|
if (_playerManager.LocalEntity == entity.Owner)
|
|
|
|
|
SyncAlerts?.Invoke(this, entity.Comp.Alerts);
|
2022-01-05 00:19:23 -08:00
|
|
|
}
|
|
|
|
|
|
2023-10-28 09:59:53 +11:00
|
|
|
private void OnPlayerAttached(EntityUid uid, AlertsComponent component, LocalPlayerAttachedEvent args)
|
2022-01-05 00:19:23 -08:00
|
|
|
{
|
2024-02-13 22:48:39 +01:00
|
|
|
if (_playerManager.LocalEntity != uid)
|
2022-03-04 23:52:51 +13:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
SyncAlerts?.Invoke(this, component.Alerts);
|
2022-01-05 00:19:23 -08:00
|
|
|
}
|
|
|
|
|
|
2022-03-04 23:52:51 +13:00
|
|
|
protected override void HandleComponentShutdown(EntityUid uid, AlertsComponent component, ComponentShutdown args)
|
2022-01-05 00:19:23 -08:00
|
|
|
{
|
2022-03-04 23:52:51 +13:00
|
|
|
base.HandleComponentShutdown(uid, component, args);
|
|
|
|
|
|
2024-02-13 22:48:39 +01:00
|
|
|
if (_playerManager.LocalEntity != uid)
|
2022-03-04 23:52:51 +13:00
|
|
|
return;
|
2022-01-05 00:19:23 -08:00
|
|
|
|
2022-03-04 23:52:51 +13:00
|
|
|
ClearAlerts?.Invoke(this, EventArgs.Empty);
|
2022-01-05 00:19:23 -08:00
|
|
|
}
|
|
|
|
|
|
2023-10-28 09:59:53 +11:00
|
|
|
private void OnPlayerDetached(EntityUid uid, AlertsComponent component, LocalPlayerDetachedEvent args)
|
2022-01-05 00:19:23 -08:00
|
|
|
{
|
|
|
|
|
ClearAlerts?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-23 22:43:04 -04:00
|
|
|
public void AlertClicked(ProtoId<AlertPrototype> alertType)
|
2022-01-05 00:19:23 -08:00
|
|
|
{
|
2024-08-07 01:15:35 -04:00
|
|
|
RaisePredictiveEvent(new ClickAlertEvent(alertType));
|
2022-01-05 00:19:23 -08:00
|
|
|
}
|
2021-06-18 01:49:18 -07:00
|
|
|
}
|