From 8982956a6bcca08c32753732c7697e7bb2186583 Mon Sep 17 00:00:00 2001 From: Winkarst <74284083+Winkarst-cpu@users.noreply.github.com> Date: Sat, 19 Apr 2025 17:26:56 +0300 Subject: [PATCH] Cleanup: Superbonk cleanup (#35104) * SuperBonk cleanup * Update * Comment changes * Update * Changes * EnsureComp --- .../Components/SuperBonkComponent.cs | 24 ++--- .../Administration/Systems/SuperBonkSystem.cs | 100 +++++++++--------- 2 files changed, 61 insertions(+), 63 deletions(-) diff --git a/Content.Server/Administration/Components/SuperBonkComponent.cs b/Content.Server/Administration/Components/SuperBonkComponent.cs index 868d232e60..0ceeccd136 100644 --- a/Content.Server/Administration/Components/SuperBonkComponent.cs +++ b/Content.Server/Administration/Components/SuperBonkComponent.cs @@ -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; /// /// Component to track the timer for the SuperBonk smite. /// -[RegisterComponent, Access(typeof(SuperBonkSystem))] -public sealed partial class SuperBonkComponent: Component +[RegisterComponent, AutoGenerateComponentPause, Access(typeof(SuperBonkSystem))] +public sealed partial class SuperBonkComponent : Component { - /// - /// Entity being Super Bonked. - /// - [DataField] - public EntityUid Target; - /// /// All of the tables the target will be bonked on. /// [DataField] - public Dictionary.Enumerator Tables; + public List.Enumerator Tables; /// - /// Value used to reset the timer once it expires. + /// How often should we bonk. /// [DataField] - public float InitialTime = 0.10f; + public TimeSpan BonkCooldown = TimeSpan.FromMilliseconds(100); /// - /// Timer till the next bonk. + /// Next time when we will bonk. /// - [DataField] - public float TimeRemaining = 0.10f; + [DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), AutoPausedField] + public TimeSpan NextBonk = TimeSpan.Zero; /// /// Whether to remove the clumsy component from the target after SuperBonk is done. diff --git a/Content.Server/Administration/Systems/SuperBonkSystem.cs b/Content.Server/Administration/Systems/SuperBonkSystem.cs index 5cd62d8357..c4de0d0a41 100644 --- a/Content.Server/Administration/Systems/SuperBonkSystem.cs +++ b/Content.Server/Administration/Systems/SuperBonkSystem.cs @@ -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(OnInit); SubscribeLocalEvent(OnMobStateChanged); - SubscribeLocalEvent(OnBonkShutdown); + SubscribeLocalEvent(OnShutdown); } - public void StartSuperBonk(EntityUid target, float delay = 0.1f, bool stopWhenDead = false) + private void OnInit(Entity 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(target, out var mState)) - { - if (mState.CurrentState == MobState.Dead) - return; - } + component.NextBonk = _timing.CurTime + component.BonkCooldown; + } - var hadClumsy = EnsureComp(target, out _); + private void OnMobStateChanged(Entity ent, ref MobStateChangedEvent args) + { + var (uid, component) = ent; - var tables = EntityQueryEnumerator(); - var bonks = new Dictionary(); - // 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(uid); + } - var sComp = new SuperBonkComponent - { - Target = target, - Tables = bonks.GetEnumerator(), - RemoveClumsy = !hadClumsy, - StopWhenDead = stopWhenDead, - }; + private void OnShutdown(Entity ent, ref ComponentShutdown args) + { + var (uid, component) = ent; - AddComp(target, sComp); + if (component.RemoveClumsy) + RemComp(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(comp.Target); + RemComp(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(uid) || !TryComp(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(target, out var mobState) && mobState.CurrentState == MobState.Dead) return; - _transformSystem.SetCoordinates(comp.Target, Transform(uid).Coordinates); - _clumsySystem.HitHeadClumsy((comp.Target, clumsyComp), uid); + if (EnsureComp(target, out var component)) + return; + + var tables = EntityQueryEnumerator(); + var bonks = new List(); + // 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(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(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(tableUid)) { - RemComp(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(comp.Target); + return true; } } -- 2.51.2