Fix sleep wake spam (#13047)

This commit is contained in:
metalgearsloth 2023-01-10 22:46:58 +11:00 committed by GitHub
parent a4725ef58e
commit e79d822206
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 23 deletions

View File

@ -0,0 +1,8 @@
using Content.Server.Bed.Sleep;
namespace Content.Client.Bed;
public sealed class SleepingSystem : SharedSleepingSystem
{
}

View File

@ -21,14 +21,15 @@ using Robust.Shared.Timing;
namespace Content.Server.Bed.Sleep
{
public sealed class SleepingSystem : EntitySystem
public sealed class SleepingSystem : SharedSleepingSystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly ActionsSystem _actionsSystem = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly ActionsSystem _actionsSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
public override void Initialize()
{
base.Initialize();
@ -88,7 +89,7 @@ namespace Content.Server.Bed.Sleep
return;
if (args.DamageDelta.Total >= component.WakeThreshold)
TryWaking(uid);
TryWaking(uid, component);
}
private void OnSleepAction(EntityUid uid, MobStateComponent component, SleepActionEvent args)
@ -98,7 +99,11 @@ namespace Content.Server.Bed.Sleep
private void OnWakeAction(EntityUid uid, MobStateComponent component, WakeActionEvent args)
{
TryWaking(uid);
if (!TryWakeCooldown(uid))
return;
if (TryWaking(uid))
args.Handled = true;
}
/// <summary>
@ -126,7 +131,10 @@ namespace Content.Server.Bed.Sleep
{
Act = () =>
{
TryWaking(args.Target, user: args.User);
if (!TryWakeCooldown(uid))
return;
TryWaking(args.Target, user: args.User);
},
Text = Loc.GetString("action-name-wake"),
Priority = 2
@ -140,16 +148,11 @@ namespace Content.Server.Bed.Sleep
/// </summary>
private void OnInteractHand(EntityUid uid, SleepingComponent component, InteractHandEvent args)
{
args.Handled = true;
var curTime = _gameTiming.CurTime;
if (curTime < component.CoolDownEnd)
{
if (!TryWakeCooldown(uid))
return;
}
args.Handled = true;
TryWaking(args.Target, user: args.User);
component.CoolDownEnd = curTime + component.Cooldown;
}
private void OnExamined(EntityUid uid, SleepingComponent component, ExaminedEvent args)
@ -189,25 +192,44 @@ namespace Content.Server.Bed.Sleep
return true;
}
private bool TryWakeCooldown(EntityUid uid, SleepingComponent? component = null)
{
if (!Resolve(uid, ref component, false))
return false;
var curTime = _gameTiming.CurTime;
if (curTime < component.CoolDownEnd)
{
return false;
}
component.CoolDownEnd = curTime + component.Cooldown;
return true;
}
/// <summary>
/// Try to wake up.
/// </summary>
public bool TryWaking(EntityUid uid, bool force = false, EntityUid? user = null)
public bool TryWaking(EntityUid uid, SleepingComponent? component = null, bool force = false, EntityUid? user = null)
{
if (!Resolve(uid, ref component))
return false;
if (!force && HasComp<ForcedSleepingComponent>(uid))
{
if (user != null)
{
SoundSystem.Play("/Audio/Effects/thudswoosh.ogg", Filter.Pvs(uid), uid, AudioHelpers.WithVariation(0.05f, _robustRandom));
_popupSystem.PopupEntity(Loc.GetString("wake-other-failure", ("target", Identity.Entity(uid, EntityManager))), uid, user.Value, Shared.Popups.PopupType.SmallCaution);
_audio.PlayPvs("/Audio/Effects/thudswoosh.ogg", uid, AudioHelpers.WithVariation(0.05f, _robustRandom));
_popupSystem.PopupEntity(Loc.GetString("wake-other-failure", ("target", Identity.Entity(uid, EntityManager))), uid, Filter.Entities(user.Value), true, Shared.Popups.PopupType.SmallCaution);
}
return false;
}
if (user != null)
{
SoundSystem.Play("/Audio/Effects/thudswoosh.ogg", Filter.Pvs(uid), uid, AudioHelpers.WithVariation(0.05f, _robustRandom));
_popupSystem.PopupEntity(Loc.GetString("wake-other-success", ("target", Identity.Entity(uid, EntityManager))), uid, user.Value);
_audio.PlayPvs("/Audio/Effects/thudswoosh.ogg", uid, AudioHelpers.WithVariation(0.05f, _robustRandom));
_popupSystem.PopupEntity(Loc.GetString("wake-other-success", ("target", Identity.Entity(uid, EntityManager))), uid, Filter.Entities(user.Value), true);
}
RemComp<SleepingComponent>(uid);
return true;

View File

@ -5,15 +5,23 @@ using Content.Shared.Bed.Sleep;
namespace Content.Server.Bed.Sleep
{
public sealed class SharedSleepingSystem : EntitySystem
public abstract class SharedSleepingSystem : EntitySystem
{
[Dependency] private readonly SharedBlindingSystem _blindingSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SleepingComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<SleepingComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<SleepingComponent, SpeakAttemptEvent>(OnSpeakAttempt);
SubscribeLocalEvent<SleepingComponent, EntityUnpausedEvent>(OnSleepUnpaused);
}
private void OnSleepUnpaused(EntityUid uid, SleepingComponent component, ref EntityUnpausedEvent args)
{
component.CoolDownEnd += args.PausedTime;
Dirty(component);
}
private void OnInit(EntityUid uid, SleepingComponent component, ComponentInit args)

View File

@ -1,4 +1,6 @@
using Content.Shared.FixedPoint;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Shared.Bed.Sleep;
@ -8,9 +10,11 @@ namespace Content.Shared.Bed.Sleep;
[NetworkedComponent, RegisterComponent]
public sealed class SleepingComponent : Component
{
// How much damage of any type it takes to wake this entity.
/// <summary>
/// How much damage of any type it takes to wake this entity.
/// </summary>
[DataField("wakeThreshold")]
public float WakeThreshold = 2;
public FixedPoint2 WakeThreshold = FixedPoint2.New(2);
/// <summary>
/// Cooldown time between users hand interaction.
@ -19,5 +23,6 @@ public sealed class SleepingComponent : Component
[ViewVariables(VVAccess.ReadWrite)]
public TimeSpan Cooldown = TimeSpan.FromSeconds(1f);
[DataField("cooldownEnd", customTypeSerializer:typeof(TimeOffsetSerializer))]
public TimeSpan CoolDownEnd;
}