]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Cleanup: Superbonk cleanup (#35104)
authorWinkarst <74284083+Winkarst-cpu@users.noreply.github.com>
Sat, 19 Apr 2025 14:26:56 +0000 (17:26 +0300)
committerGitHub <noreply@github.com>
Sat, 19 Apr 2025 14:26:56 +0000 (10:26 -0400)
* SuperBonk cleanup

* Update

* Comment changes

* Update

* Changes

* EnsureComp

Content.Server/Administration/Components/SuperBonkComponent.cs
Content.Server/Administration/Systems/SuperBonkSystem.cs

index 868d232e6063cf2439c17eaadfa38c8279b51793..0ceeccd136d8df511efd82a29af68e18724f1829 100644 (file)
@@ -1,37 +1,31 @@
 using Content.Server.Administration.Systems;
-using Content.Shared.Climbing.Components;
+using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
 
 namespace Content.Server.Administration.Components;
 
 /// <summary>
 /// Component to track the timer for the SuperBonk smite.
 /// </summary>
-[RegisterComponent, Access(typeof(SuperBonkSystem))]
-public sealed partial class SuperBonkComponent: Component
+[RegisterComponent, AutoGenerateComponentPause, Access(typeof(SuperBonkSystem))]
+public sealed partial class SuperBonkComponent : Component
 {
-    /// <summary>
-    /// Entity being Super Bonked.
-    /// </summary>
-    [DataField]
-    public EntityUid Target;
-
     /// <summary>
     /// All of the tables the target will be bonked on.
     /// </summary>
     [DataField]
-    public Dictionary<EntityUid, BonkableComponent>.Enumerator Tables;
+    public List<EntityUid>.Enumerator Tables;
 
     /// <summary>
-    /// Value used to reset the timer once it expires.
+    /// How often should we bonk.
     /// </summary>
     [DataField]
-    public float InitialTime = 0.10f;
+    public TimeSpan BonkCooldown = TimeSpan.FromMilliseconds(100);
 
     /// <summary>
-    /// Timer till the next bonk.
+    /// Next time when we will bonk.
     /// </summary>
-    [DataField]
-    public float TimeRemaining = 0.10f;
+    [DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), AutoPausedField]
+    public TimeSpan NextBonk = TimeSpan.Zero;
 
     /// <summary>
     /// Whether to remove the clumsy component from the target after SuperBonk is done.
index 5cd62d835729343b8ee5229ada657aa273b84202..c4de0d0a41c4f69cfd733f93b1f5ea87fe30d80e 100644 (file)
@@ -4,6 +4,7 @@ using Content.Shared.Clumsy;
 using Content.Shared.Mobs;
 using Content.Shared.Mobs.Components;
 using Robust.Shared.Audio.Systems;
+using Robust.Shared.Timing;
 
 namespace Content.Server.Administration.Systems;
 
@@ -12,44 +13,38 @@ public sealed class SuperBonkSystem : EntitySystem
     [Dependency] private readonly SharedTransformSystem _transformSystem = default!;
     [Dependency] private readonly ClumsySystem _clumsySystem = default!;
     [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
+    [Dependency] private readonly IGameTiming _timing = default!;
 
     public override void Initialize()
     {
         base.Initialize();
 
+        SubscribeLocalEvent<SuperBonkComponent, ComponentInit>(OnInit);
         SubscribeLocalEvent<SuperBonkComponent, MobStateChangedEvent>(OnMobStateChanged);
-        SubscribeLocalEvent<SuperBonkComponent, ComponentShutdown>(OnBonkShutdown);
+        SubscribeLocalEvent<SuperBonkComponent, ComponentShutdown>(OnShutdown);
     }
 
-    public void StartSuperBonk(EntityUid target, float delay = 0.1f, bool stopWhenDead = false)
+    private void OnInit(Entity<SuperBonkComponent> ent, ref ComponentInit args)
     {
+        var (_, component) = ent;
 
-        //The other check in the code to stop when the target dies does not work if the target is already dead.
-        if (stopWhenDead && TryComp<MobStateComponent>(target, out var mState))
-        {
-            if (mState.CurrentState == MobState.Dead)
-                return;
-        }
+        component.NextBonk = _timing.CurTime + component.BonkCooldown;
+    }
 
-        var hadClumsy = EnsureComp<ClumsyComponent>(target, out _);
+    private void OnMobStateChanged(Entity<SuperBonkComponent> ent, ref MobStateChangedEvent args)
+    {
+        var (uid, component) = ent;
 
-        var tables = EntityQueryEnumerator<BonkableComponent>();
-        var bonks = new Dictionary<EntityUid, BonkableComponent>();
-        // This is done so we don't crash if something like a new table is spawned.
-        while (tables.MoveNext(out var uid, out var comp))
-        {
-            bonks.Add(uid, comp);
-        }
+        if (component.StopWhenDead && args.NewMobState == MobState.Dead)
+            RemCompDeferred<SuperBonkComponent>(uid);
+    }
 
-        var sComp = new SuperBonkComponent
-        {
-            Target = target,
-            Tables = bonks.GetEnumerator(),
-            RemoveClumsy = !hadClumsy,
-            StopWhenDead = stopWhenDead,
-        };
+    private void OnShutdown(Entity<SuperBonkComponent> ent, ref ComponentShutdown args)
+    {
+        var (uid, component) = ent;
 
-        AddComp(target, sComp);
+        if (component.RemoveClumsy)
+            RemComp<ClumsyComponent>(uid);
     }
 
     public override void Update(float frameTime)
@@ -59,49 +54,58 @@ public sealed class SuperBonkSystem : EntitySystem
 
         while (comps.MoveNext(out var uid, out var comp))
         {
-            comp.TimeRemaining -= frameTime;
-            if (!(comp.TimeRemaining <= 0))
+            if (comp.NextBonk > _timing.CurTime)
                 continue;
 
-            Bonk(comp);
-
-            if (!(comp.Tables.MoveNext()))
+            if (!TryBonk(uid, comp.Tables.Current) || !comp.Tables.MoveNext())
             {
-                RemComp<SuperBonkComponent>(comp.Target);
+                RemComp<SuperBonkComponent>(uid);
                 continue;
             }
 
-            comp.TimeRemaining = comp.InitialTime;
+            comp.NextBonk += comp.BonkCooldown;
         }
     }
 
-    private void Bonk(SuperBonkComponent comp)
+    public void StartSuperBonk(EntityUid target, bool stopWhenDead = false)
     {
-        var uid = comp.Tables.Current.Key;
-
-        // It would be very weird for something without a transform component to have a bonk component
-        // but just in case because I don't want to crash the server.
-        if (!HasComp<TransformComponent>(uid) || !TryComp<ClumsyComponent>(comp.Target, out var clumsyComp))
+        //The other check in the code to stop when the target dies does not work if the target is already dead.
+        if (stopWhenDead && TryComp<MobStateComponent>(target, out var mobState) && mobState.CurrentState == MobState.Dead)
             return;
 
-        _transformSystem.SetCoordinates(comp.Target, Transform(uid).Coordinates);
 
-        _clumsySystem.HitHeadClumsy((comp.Target, clumsyComp), uid);
+        if (EnsureComp<SuperBonkComponent>(target, out var component))
+            return;
+
+        var tables = EntityQueryEnumerator<BonkableComponent>();
+        var bonks = new List<EntityUid>();
+        // This is done so we don't crash if something like a new table is spawned.
+        while (tables.MoveNext(out var uid, out var comp))
+        {
+            bonks.Add(uid);
+        }
 
-        _audioSystem.PlayPvs(clumsyComp.TableBonkSound, comp.Target);
+        component.Tables = bonks.GetEnumerator();
+        component.RemoveClumsy = !EnsureComp<ClumsyComponent>(target, out _);
+        component.StopWhenDead = stopWhenDead;
     }
 
-    private void OnMobStateChanged(EntityUid uid, SuperBonkComponent comp, MobStateChangedEvent args)
+    private bool TryBonk(EntityUid uid, EntityUid tableUid)
     {
-        if (comp.StopWhenDead && args.NewMobState == MobState.Dead)
+        if (!TryComp<ClumsyComponent>(uid, out var clumsyComp))
+            return false;
+
+        // It would be very weird for something without a transform component to have a bonk component
+        // but just in case because I don't want to crash the server.
+        if (HasComp<TransformComponent>(tableUid))
         {
-            RemComp<SuperBonkComponent>(uid);
+            _transformSystem.SetCoordinates(uid, Transform(tableUid).Coordinates);
+
+            _clumsySystem.HitHeadClumsy((uid, clumsyComp), tableUid);
+
+            _audioSystem.PlayPvs(clumsyComp.TableBonkSound, tableUid);
         }
-    }
 
-    private void OnBonkShutdown(EntityUid uid, SuperBonkComponent comp, ComponentShutdown ev)
-    {
-        if (comp.RemoveClumsy)
-            RemComp<ClumsyComponent>(comp.Target);
+        return true;
     }
 }