Files
crystall-punk-14/Content.Shared/Bed/Sleep/SharedSleepingSystem.cs

93 lines
3.3 KiB
C#
Raw Normal View History

2022-07-27 00:46:24 -04:00
using Content.Shared.Actions;
using Content.Shared.Bed.Sleep;
using Content.Shared.Damage.ForceSay;
using Content.Shared.Eye.Blinding.Systems;
using Content.Shared.Pointing;
using Content.Shared.Speech;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
2022-07-27 00:46:24 -04:00
namespace Content.Server.Bed.Sleep
{
2023-01-10 22:46:58 +11:00
public abstract class SharedSleepingSystem : EntitySystem
2022-07-27 00:46:24 -04:00
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
[Dependency] private readonly BlindableSystem _blindableSystem = default!;
2023-01-10 22:46:58 +11:00
[ValidatePrototypeId<EntityPrototype>] private const string WakeActionId = "ActionWake";
2022-07-27 00:46:24 -04:00
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SleepingComponent, MapInitEvent>(OnMapInit);
2022-07-27 00:46:24 -04:00
SubscribeLocalEvent<SleepingComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<SleepingComponent, SpeakAttemptEvent>(OnSpeakAttempt);
SubscribeLocalEvent<SleepingComponent, CanSeeAttemptEvent>(OnSeeAttempt);
SubscribeLocalEvent<SleepingComponent, PointAttemptEvent>(OnPointAttempt);
2023-01-10 22:46:58 +11:00
}
2022-07-27 00:46:24 -04:00
private void OnMapInit(EntityUid uid, SleepingComponent component, MapInitEvent args)
2022-07-27 00:46:24 -04:00
{
var ev = new SleepStateChangedEvent(true);
RaiseLocalEvent(uid, ev);
_blindableSystem.UpdateIsBlind(uid);
_actionsSystem.AddAction(uid, ref component.WakeAction, WakeActionId, uid);
2022-07-27 00:46:24 -04:00
// TODO remove hardcoded time.
_actionsSystem.SetCooldown(component.WakeAction, _gameTiming.CurTime, _gameTiming.CurTime + TimeSpan.FromSeconds(2f));
}
2022-07-27 00:46:24 -04:00
private void OnShutdown(EntityUid uid, SleepingComponent component, ComponentShutdown args)
{
_actionsSystem.RemoveAction(uid, component.WakeAction);
2022-07-27 00:46:24 -04:00
var ev = new SleepStateChangedEvent(false);
RaiseLocalEvent(uid, ev);
_blindableSystem.UpdateIsBlind(uid);
2022-07-27 00:46:24 -04:00
}
private void OnSpeakAttempt(EntityUid uid, SleepingComponent component, SpeakAttemptEvent args)
{
// TODO reduce duplication of this behavior with MobStateSystem somehow
if (HasComp<AllowNextCritSpeechComponent>(uid))
{
RemCompDeferred<AllowNextCritSpeechComponent>(uid);
return;
}
2022-07-27 00:46:24 -04:00
args.Cancel();
}
private void OnSeeAttempt(EntityUid uid, SleepingComponent component, CanSeeAttemptEvent args)
{
if (component.LifeStage <= ComponentLifeStage.Running)
args.Cancel();
}
private void OnPointAttempt(EntityUid uid, SleepingComponent component, PointAttemptEvent args)
{
args.Cancel();
}
2022-07-27 00:46:24 -04:00
}
}
public sealed partial class SleepActionEvent : InstantActionEvent {}
2022-07-27 00:46:24 -04:00
public sealed partial class WakeActionEvent : InstantActionEvent {}
2022-07-27 00:46:24 -04:00
/// <summary>
/// Raised on an entity when they fall asleep or wake up.
/// </summary>
public sealed class SleepStateChangedEvent : EntityEventArgs
{
public bool FellAsleep = false;
public SleepStateChangedEvent(bool fellAsleep)
{
FellAsleep = fellAsleep;
}
}