DrowsinessSystem.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using Content.Shared.Bed.Sleep;
  2. using Content.Shared.Drowsiness;
  3. using Content.Shared.StatusEffect;
  4. using Robust.Shared.Random;
  5. using Robust.Shared.Timing;
  6. namespace Content.Server.Drowsiness;
  7. public sealed class DrowsinessSystem : SharedDrowsinessSystem
  8. {
  9. [ValidatePrototypeId<StatusEffectPrototype>]
  10. private const string SleepKey = "ForcedSleep"; // Same one used by N2O and other sleep chems.
  11. [Dependency] private readonly IGameTiming _timing = default!;
  12. [Dependency] private readonly IRobustRandom _random = default!;
  13. [Dependency] private readonly StatusEffectsSystem _statusEffects = default!;
  14. /// <inheritdoc/>
  15. public override void Initialize()
  16. {
  17. SubscribeLocalEvent<DrowsinessComponent, ComponentStartup>(OnInit);
  18. }
  19. private void OnInit(EntityUid uid, DrowsinessComponent component, ComponentStartup args)
  20. {
  21. component.NextIncidentTime = _timing.CurTime + TimeSpan.FromSeconds(_random.NextFloat(component.TimeBetweenIncidents.X, component.TimeBetweenIncidents.Y));
  22. }
  23. public override void Update(float frameTime)
  24. {
  25. base.Update(frameTime);
  26. var query = EntityQueryEnumerator<DrowsinessComponent>();
  27. while (query.MoveNext(out var uid, out var component))
  28. {
  29. if (_timing.CurTime < component.NextIncidentTime)
  30. continue;
  31. // Set the new time.
  32. component.NextIncidentTime = _timing.CurTime + TimeSpan.FromSeconds(_random.NextFloat(component.TimeBetweenIncidents.X, component.TimeBetweenIncidents.Y));
  33. // sleep duration
  34. var duration = TimeSpan.FromSeconds(_random.NextFloat(component.DurationOfIncident.X, component.DurationOfIncident.Y));
  35. // Make sure the sleep time doesn't cut into the time to next incident.
  36. component.NextIncidentTime += duration;
  37. _statusEffects.TryAddStatusEffect<ForcedSleepingComponent>(uid, SleepKey, duration, false);
  38. }
  39. }
  40. }