]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Predict sleeping action (#37414)
authormetalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Thu, 15 May 2025 12:52:24 +0000 (22:52 +1000)
committerGitHub <noreply@github.com>
Thu, 15 May 2025 12:52:24 +0000 (22:52 +1000)
* Predict sleeping action

* reviews

Content.Client/Bed/BedSystem.cs [new file with mode: 0644]
Content.Server/Bed/BedSystem.cs
Content.Server/Bed/Components/HealOnBuckleHealing.cs [deleted file]
Content.Shared/Bed/Components/HealOnBuckleComponent.cs [moved from Content.Server/Bed/Components/HealOnBuckleComponent.cs with 53% similarity]
Content.Shared/Bed/Components/HealOnBuckleHealing.cs [new file with mode: 0644]
Content.Shared/Bed/SharedBedSystem.cs [new file with mode: 0644]

diff --git a/Content.Client/Bed/BedSystem.cs b/Content.Client/Bed/BedSystem.cs
new file mode 100644 (file)
index 0000000..9c6f282
--- /dev/null
@@ -0,0 +1,8 @@
+using Content.Shared.Bed;
+
+namespace Content.Client.Bed;
+
+public sealed class BedSystem : SharedBedSystem
+{
+
+}
index b2d688940861d206006d3820d0405adffb77992c..673984c0a7f45bc262267b2c0495921b1971c89b 100644 (file)
@@ -1,9 +1,9 @@
 using Content.Server.Actions;
 using Content.Server.Bed.Components;
 using Content.Server.Body.Systems;
-using Content.Server.Power.Components;
 using Content.Server.Power.EntitySystems;
 using Content.Shared.Bed;
+using Content.Shared.Bed.Components;
 using Content.Shared.Bed.Sleep;
 using Content.Shared.Body.Components;
 using Content.Shared.Buckle.Components;
@@ -11,49 +11,30 @@ using Content.Shared.Damage;
 using Content.Shared.Emag.Systems;
 using Content.Shared.Mobs.Systems;
 using Content.Shared.Power;
-using Robust.Shared.Timing;
-using Robust.Shared.Utility;
 
 namespace Content.Server.Bed
 {
-    public sealed class BedSystem : EntitySystem
+    public sealed class BedSystem : SharedBedSystem
     {
         [Dependency] private readonly DamageableSystem _damageableSystem = default!;
-        [Dependency] private readonly ActionsSystem _actionsSystem = default!;
         [Dependency] private readonly EmagSystem _emag = default!;
-        [Dependency] private readonly SleepingSystem _sleepingSystem = default!;
         [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
         [Dependency] private readonly MobStateSystem _mobStateSystem = default!;
-        [Dependency] private readonly IGameTiming _timing = default!;
+
+        private EntityQuery<SleepingComponent> _sleepingQuery;
 
         public override void Initialize()
         {
             base.Initialize();
-            SubscribeLocalEvent<HealOnBuckleComponent, StrappedEvent>(OnStrapped);
-            SubscribeLocalEvent<HealOnBuckleComponent, UnstrappedEvent>(OnUnstrapped);
+
+            _sleepingQuery = GetEntityQuery<SleepingComponent>();
+
             SubscribeLocalEvent<StasisBedComponent, StrappedEvent>(OnStasisStrapped);
             SubscribeLocalEvent<StasisBedComponent, UnstrappedEvent>(OnStasisUnstrapped);
             SubscribeLocalEvent<StasisBedComponent, PowerChangedEvent>(OnPowerChanged);
             SubscribeLocalEvent<StasisBedComponent, GotEmaggedEvent>(OnEmagged);
         }
 
-        private void OnStrapped(Entity<HealOnBuckleComponent> bed, ref StrappedEvent args)
-        {
-            EnsureComp<HealOnBuckleHealingComponent>(bed);
-            bed.Comp.NextHealTime = _timing.CurTime + TimeSpan.FromSeconds(bed.Comp.HealTime);
-            _actionsSystem.AddAction(args.Buckle, ref bed.Comp.SleepAction, SleepingSystem.SleepActionId, bed);
-
-            // Single action entity, cannot strap multiple entities to the same bed.
-            DebugTools.AssertEqual(args.Strap.Comp.BuckledEntities.Count, 1);
-        }
-
-        private void OnUnstrapped(Entity<HealOnBuckleComponent> bed, ref UnstrappedEvent args)
-        {
-            _actionsSystem.RemoveAction(args.Buckle, bed.Comp.SleepAction);
-            _sleepingSystem.TryWaking(args.Buckle.Owner);
-            RemComp<HealOnBuckleHealingComponent>(bed);
-        }
-
         public override void Update(float frameTime)
         {
             base.Update(frameTime);
@@ -61,7 +42,7 @@ namespace Content.Server.Bed
             var query = EntityQueryEnumerator<HealOnBuckleHealingComponent, HealOnBuckleComponent, StrapComponent>();
             while (query.MoveNext(out var uid, out _, out var bedComponent, out var strapComponent))
             {
-                if (_timing.CurTime < bedComponent.NextHealTime)
+                if (Timing.CurTime < bedComponent.NextHealTime)
                     continue;
 
                 bedComponent.NextHealTime += TimeSpan.FromSeconds(bedComponent.HealTime);
@@ -76,7 +57,7 @@ namespace Content.Server.Bed
 
                     var damage = bedComponent.Damage;
 
-                    if (HasComp<SleepingComponent>(healedEntity))
+                    if (_sleepingQuery.HasComp(healedEntity))
                         damage *= bedComponent.SleepMultiplier;
 
                     _damageableSystem.TryChangeDamage(healedEntity, damage, true, origin: uid);
diff --git a/Content.Server/Bed/Components/HealOnBuckleHealing.cs b/Content.Server/Bed/Components/HealOnBuckleHealing.cs
deleted file mode 100644 (file)
index aaa82c7..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-namespace Content.Server.Bed.Components
-{
-    // TODO rename this component
-    [RegisterComponent]
-    public sealed partial class HealOnBuckleHealingComponent : Component
-    {}
-}
similarity index 53%
rename from Content.Server/Bed/Components/HealOnBuckleComponent.cs
rename to Content.Shared/Bed/Components/HealOnBuckleComponent.cs
index 3c6f3a4382bce7679ecaf019a17a1d073b92d55b..87f235cfb643067dbba5657ef2a9201d29a32878 100644 (file)
@@ -1,8 +1,10 @@
 using Content.Shared.Damage;
+using Robust.Shared.GameStates;
+using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
 
-namespace Content.Server.Bed.Components
+namespace Content.Shared.Bed.Components
 {
-    [RegisterComponent]
+    [RegisterComponent, NetworkedComponent, AutoGenerateComponentPause, AutoGenerateComponentState]
     public sealed partial class HealOnBuckleComponent : Component
     {
         /// <summary>
@@ -23,8 +25,16 @@ namespace Content.Server.Bed.Components
         [DataField]
         public float SleepMultiplier = 3f;
 
+        /// <summary>
+        /// Next time that <see cref="Damage"/> will be applied.
+        /// </summary>
+        [DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), AutoPausedField, AutoNetworkedField]
         public TimeSpan NextHealTime = TimeSpan.Zero; //Next heal
 
-        [DataField] public EntityUid? SleepAction;
+        /// <summary>
+        /// Action for the attached entity to be able to sleep.
+        /// </summary>
+        [DataField, AutoNetworkedField]
+        public EntityUid? SleepAction;
     }
 }
diff --git a/Content.Shared/Bed/Components/HealOnBuckleHealing.cs b/Content.Shared/Bed/Components/HealOnBuckleHealing.cs
new file mode 100644 (file)
index 0000000..ad83ed6
--- /dev/null
@@ -0,0 +1,7 @@
+using Robust.Shared.GameStates;
+
+namespace Content.Shared.Bed.Components;
+
+// TODO rename this component
+[RegisterComponent, NetworkedComponent]
+public sealed partial class HealOnBuckleHealingComponent : Component;
diff --git a/Content.Shared/Bed/SharedBedSystem.cs b/Content.Shared/Bed/SharedBedSystem.cs
new file mode 100644 (file)
index 0000000..903bfef
--- /dev/null
@@ -0,0 +1,49 @@
+using Content.Shared.Actions;
+using Content.Shared.Bed.Components;
+using Content.Shared.Bed.Sleep;
+using Content.Shared.Buckle.Components;
+using Robust.Shared.Timing;
+using Robust.Shared.Utility;
+
+namespace Content.Shared.Bed;
+
+public abstract class SharedBedSystem : EntitySystem
+{
+    [Dependency] protected readonly IGameTiming Timing = default!;
+    [Dependency] private readonly ActionContainerSystem _actConts = default!;
+    [Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
+    [Dependency] private readonly SleepingSystem _sleepingSystem = default!;
+
+    public override void Initialize()
+    {
+        base.Initialize();
+
+        SubscribeLocalEvent<HealOnBuckleComponent, MapInitEvent>(OnHealMapInit);
+        SubscribeLocalEvent<HealOnBuckleComponent, StrappedEvent>(OnStrapped);
+        SubscribeLocalEvent<HealOnBuckleComponent, UnstrappedEvent>(OnUnstrapped);
+    }
+
+    private void OnHealMapInit(Entity<HealOnBuckleComponent> ent, ref MapInitEvent args)
+    {
+        _actConts.EnsureAction(ent.Owner, ref ent.Comp.SleepAction, SleepingSystem.SleepActionId);
+        Dirty(ent);
+    }
+
+    private void OnStrapped(Entity<HealOnBuckleComponent> bed, ref StrappedEvent args)
+    {
+        EnsureComp<HealOnBuckleHealingComponent>(bed);
+        bed.Comp.NextHealTime = Timing.CurTime + TimeSpan.FromSeconds(bed.Comp.HealTime);
+        _actionsSystem.AddAction(args.Buckle, ref bed.Comp.SleepAction, SleepingSystem.SleepActionId, bed);
+        Dirty(bed);
+
+        // Single action entity, cannot strap multiple entities to the same bed.
+        DebugTools.AssertEqual(args.Strap.Comp.BuckledEntities.Count, 1);
+    }
+
+    private void OnUnstrapped(Entity<HealOnBuckleComponent> bed, ref UnstrappedEvent args)
+    {
+        _actionsSystem.RemoveAction(args.Buckle, bed.Comp.SleepAction);
+        _sleepingSystem.TryWaking(args.Buckle.Owner);
+        RemComp<HealOnBuckleHealingComponent>(bed);
+    }
+}