ECSatize AlertsSystem (#5559)

This commit is contained in:
Acruid
2022-01-05 00:19:23 -08:00
committed by GitHub
parent 36d4de5e61
commit 5b1cd2dd96
59 changed files with 1069 additions and 1038 deletions

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Alert;
using Robust.Shared.GameObjects;
@@ -15,6 +15,7 @@ namespace Content.Shared.StatusEffect
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IComponentFactory _componentFactory = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly AlertsSystem _alertsSystem = default!;
public override void Initialize()
{
@@ -80,20 +81,16 @@ namespace Content.Shared.StatusEffect
/// <param name="time">How long the effect should last for.</param>
/// <param name="refresh">The status effect cooldown should be refreshed (true) or accumulated (false).</param>
/// <param name="status">The status effects component to change, if you already have it.</param>
/// <param name="alerts">The alerts component to modify, if the status effect has an alert.</param>
/// <returns>False if the effect could not be added or the component already exists, true otherwise.</returns>
/// <typeparam name="T">The component type to add and remove from the entity.</typeparam>
public bool TryAddStatusEffect<T>(EntityUid uid, string key, TimeSpan time, bool refresh,
StatusEffectsComponent? status=null,
SharedAlertsComponent? alerts=null)
StatusEffectsComponent? status = null)
where T: Component, new()
{
if (!Resolve(uid, ref status, false))
return false;
Resolve(uid, ref alerts, false);
if (TryAddStatusEffect(uid, key, time, refresh, status, alerts))
if (TryAddStatusEffect(uid, key, time, refresh, status))
{
// If they already have the comp, we just won't bother updating anything.
if (!EntityManager.HasComponent<T>(uid))
@@ -108,15 +105,12 @@ namespace Content.Shared.StatusEffect
}
public bool TryAddStatusEffect(EntityUid uid, string key, TimeSpan time, bool refresh, string component,
StatusEffectsComponent? status = null,
SharedAlertsComponent? alerts = null)
StatusEffectsComponent? status = null)
{
if (!Resolve(uid, ref status, false))
return false;
Resolve(uid, ref alerts, false);
if (TryAddStatusEffect(uid, key, time, refresh, status, alerts))
if (TryAddStatusEffect(uid, key, time, refresh, status))
{
// If they already have the comp, we just won't bother updating anything.
if (!EntityManager.HasComponent(uid, _componentFactory.GetRegistration(component).Type))
@@ -142,26 +136,22 @@ namespace Content.Shared.StatusEffect
/// <param name="time">How long the effect should last for.</param>
/// <param name="refresh">The status effect cooldown should be refreshed (true) or accumulated (false).</param>
/// <param name="status">The status effects component to change, if you already have it.</param>
/// <param name="alerts">The alerts component to modify, if the status effect has an alert.</param>
/// <returns>False if the effect could not be added, or if the effect already existed.</returns>
/// <remarks>
/// This obviously does not add any actual 'effects' on its own. Use the generic overload,
/// which takes in a component type, if you want to automatically add and remove a component.
///
///
/// If the effect already exists, it will simply replace the cooldown with the new one given.
/// If you want special 'effect merging' behavior, do it your own damn self!
/// </remarks>
public bool TryAddStatusEffect(EntityUid uid, string key, TimeSpan time, bool refresh,
StatusEffectsComponent? status=null,
SharedAlertsComponent? alerts=null)
StatusEffectsComponent? status=null)
{
if (!Resolve(uid, ref status, false))
return false;
if (!CanApplyEffect(uid, key, status))
return false;
Resolve(uid, ref alerts, false);
// we already checked if it has the index in CanApplyEffect so a straight index and not tryindex here
// is fine
var proto = _prototypeManager.Index<StatusEffectPrototype>(key);
@@ -191,9 +181,10 @@ namespace Content.Shared.StatusEffect
status.ActiveEffects.Add(key, new StatusEffectState(cooldown, refresh, null));
}
if (proto.Alert != null && alerts != null)
if (proto.Alert != null)
{
alerts.ShowAlert(proto.Alert.Value, cooldown: GetAlertCooldown(uid, proto.Alert.Value, status));
var cooldown1 = GetAlertCooldown(uid, proto.Alert.Value, status);
_alertsSystem.ShowAlert(uid, proto.Alert.Value, null, cooldown1);
}
status.Dirty();
@@ -233,15 +224,13 @@ namespace Content.Shared.StatusEffect
/// <param name="uid">The entity to remove an effect from.</param>
/// <param name="key">The effect ID to remove.</param>
/// <param name="status">The status effects component to change, if you already have it.</param>
/// <param name="alerts">The alerts component to modify, if the status effect has an alert.</param>
/// <returns>False if the effect could not be removed, true otherwise.</returns>
/// <remarks>
/// Obviously this doesn't automatically clear any effects a status effect might have.
/// That's up to the removed component to handle itself when it's removed.
/// </remarks>
public bool TryRemoveStatusEffect(EntityUid uid, string key,
StatusEffectsComponent? status=null,
SharedAlertsComponent? alerts=null)
StatusEffectsComponent? status=null)
{
if (!Resolve(uid, ref status, false))
return false;
@@ -250,8 +239,6 @@ namespace Content.Shared.StatusEffect
if (!_prototypeManager.TryIndex<StatusEffectPrototype>(key, out var proto))
return false;
Resolve(uid, ref alerts, false);
var state = status.ActiveEffects[key];
// There are cases where a status effect component might be server-only, so TryGetRegistration...
@@ -267,9 +254,9 @@ namespace Content.Shared.StatusEffect
EntityManager.RemoveComponent(uid, type);
}
if (proto.Alert != null && alerts != null)
if (proto.Alert != null)
{
alerts.ClearAlert(proto.Alert.Value);
_alertsSystem.ClearAlert(uid, proto.Alert.Value);
}
status.ActiveEffects.Remove(key);
@@ -284,21 +271,17 @@ namespace Content.Shared.StatusEffect
/// </summary>
/// <param name="uid">The entity to remove effects from.</param>
/// <param name="status">The status effects component to change, if you already have it.</param>
/// <param name="alerts">The alerts component to modify, if the status effect has an alert.</param>
/// <returns>False if any status effects failed to be removed, true if they all did.</returns>
public bool TryRemoveAllStatusEffects(EntityUid uid,
StatusEffectsComponent? status = null,
SharedAlertsComponent? alerts = null)
StatusEffectsComponent? status = null)
{
if (!Resolve(uid, ref status, false))
return false;
Resolve(uid, ref alerts, false);
bool failed = false;
foreach (var effect in status.ActiveEffects)
{
if(!TryRemoveStatusEffect(uid, effect.Key, status, alerts))
if(!TryRemoveStatusEffect(uid, effect.Key, status))
failed = true;
}
@@ -350,14 +333,11 @@ namespace Content.Shared.StatusEffect
/// <param name="time">The amount of time to add.</param>
/// <param name="status">The status effect component, should you already have it.</param>
public bool TryAddTime(EntityUid uid, string key, TimeSpan time,
StatusEffectsComponent? status=null,
SharedAlertsComponent? alert=null)
StatusEffectsComponent? status=null)
{
if (!Resolve(uid, ref status, false))
return false;
Resolve(uid, ref alert, false);
if (!HasStatusEffect(uid, key, status))
return false;
@@ -366,11 +346,10 @@ namespace Content.Shared.StatusEffect
status.ActiveEffects[key].Cooldown = timer;
if (_prototypeManager.TryIndex<StatusEffectPrototype>(key, out var proto)
&& alert != null
&& proto.Alert != null)
{
alert.ShowAlert(proto.Alert.Value, cooldown: GetAlertCooldown(uid, proto.Alert.Value, status));
(TimeSpan, TimeSpan)? cooldown = GetAlertCooldown(uid, proto.Alert.Value, status);
_alertsSystem.ShowAlert(uid, proto.Alert.Value, null, cooldown);
}
return true;
@@ -384,14 +363,11 @@ namespace Content.Shared.StatusEffect
/// <param name="time">The amount of time to add.</param>
/// <param name="status">The status effect component, should you already have it.</param>
public bool TryRemoveTime(EntityUid uid, string key, TimeSpan time,
StatusEffectsComponent? status=null,
SharedAlertsComponent? alert=null)
StatusEffectsComponent? status=null)
{
if (!Resolve(uid, ref status, false))
return false;
Resolve(uid, ref alert, false);
if (!HasStatusEffect(uid, key, status))
return false;
@@ -405,11 +381,10 @@ namespace Content.Shared.StatusEffect
status.ActiveEffects[key].Cooldown = timer;
if (_prototypeManager.TryIndex<StatusEffectPrototype>(key, out var proto)
&& alert != null
&& proto.Alert != null)
{
alert.ShowAlert(proto.Alert.Value, cooldown: GetAlertCooldown(uid, proto.Alert.Value, status));
(TimeSpan, TimeSpan)? cooldown = GetAlertCooldown(uid, proto.Alert.Value, status);
_alertsSystem.ShowAlert(uid, proto.Alert.Value, null, cooldown);
}
return true;