From 3007200832d61ed8e3423ecd231a291ff45c4410 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 24 Oct 2023 18:45:42 -0700 Subject: [PATCH] Adds AttemptEntity(Uns|S)tickEvent. (#20728) * try-stick * convert spider charge to attempt-stick-events --- .../Ninja/Systems/SpiderChargeSystem.cs | 19 +++++++---- .../Sticky/Events/EntityStuckEvent.cs | 23 +++++++++++++ Content.Server/Sticky/Systems/StickySystem.cs | 33 ++++++++++++++++--- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/Content.Server/Ninja/Systems/SpiderChargeSystem.cs b/Content.Server/Ninja/Systems/SpiderChargeSystem.cs index 6f3c3b3f9d..0182bd7ca7 100644 --- a/Content.Server/Ninja/Systems/SpiderChargeSystem.cs +++ b/Content.Server/Ninja/Systems/SpiderChargeSystem.cs @@ -23,7 +23,7 @@ public sealed class SpiderChargeSystem : EntitySystem { base.Initialize(); - SubscribeLocalEvent(BeforePlant); + SubscribeLocalEvent(OnAttemptStick); SubscribeLocalEvent(OnStuck); SubscribeLocalEvent(OnExplode); } @@ -31,14 +31,17 @@ public sealed class SpiderChargeSystem : EntitySystem /// /// Require that the planter is a ninja and the charge is near the target warp point. /// - private void BeforePlant(EntityUid uid, SpiderChargeComponent comp, BeforeRangedInteractEvent args) + private void OnAttemptStick(EntityUid uid, SpiderChargeComponent comp, AttemptEntityStickEvent args) { + if (args.Cancelled) + return; + var user = args.User; if (!_mind.TryGetRole(user, out var role)) { _popup.PopupEntity(Loc.GetString("spider-charge-not-ninja"), user, user); - args.Handled = true; + args.Cancelled = true; return; } @@ -47,12 +50,14 @@ public sealed class SpiderChargeSystem : EntitySystem return; // assumes warp point still exists - var target = Transform(role.SpiderChargeTarget.Value).MapPosition; - var coords = args.ClickLocation.ToMap(EntityManager, _transform); - if (!coords.InRange(target, comp.Range)) + var targetXform = Transform(role.SpiderChargeTarget.Value); + var locXform = Transform(args.Target); + if (locXform.MapID != targetXform.MapID || + (_transform.GetWorldPosition(locXform) - _transform.GetWorldPosition(targetXform)).LengthSquared() > comp.Range * comp.Range) { _popup.PopupEntity(Loc.GetString("spider-charge-too-far"), user, user); - args.Handled = true; + args.Cancelled = true; + return; } } diff --git a/Content.Server/Sticky/Events/EntityStuckEvent.cs b/Content.Server/Sticky/Events/EntityStuckEvent.cs index b924436489..7857fad7d5 100644 --- a/Content.Server/Sticky/Events/EntityStuckEvent.cs +++ b/Content.Server/Sticky/Events/EntityStuckEvent.cs @@ -1,5 +1,28 @@ namespace Content.Server.Sticky.Events; +/// +/// Risen on sticky entity to see if it can stick to another entity. +/// +[ByRefEvent] +public record struct AttemptEntityStickEvent(EntityUid Target, EntityUid User) +{ + public readonly EntityUid Target = Target; + public readonly EntityUid User = User; + public bool Cancelled = false; +} + +/// +/// Risen on sticky entity to see if it can unstick from another entity. +/// +[ByRefEvent] +public record struct AttemptEntityUnstickEvent(EntityUid Target, EntityUid User) +{ + public readonly EntityUid Target = Target; + public readonly EntityUid User = User; + public bool Cancelled = false; +} + + /// /// Risen on sticky entity when it was stuck to other entity. /// diff --git a/Content.Server/Sticky/Systems/StickySystem.cs b/Content.Server/Sticky/Systems/StickySystem.cs index 330b878c05..bcc1be39a9 100644 --- a/Content.Server/Sticky/Systems/StickySystem.cs +++ b/Content.Server/Sticky/Systems/StickySystem.cs @@ -56,7 +56,7 @@ public sealed class StickySystem : EntitySystem { DoContactInteraction = true, Text = Loc.GetString("comp-sticky-unstick-verb-text"), - Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/eject.svg.192dpi.png")), + Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/eject.svg.192dpi.png")), Act = () => StartUnsticking(uid, args.User, component) }); } @@ -72,6 +72,11 @@ public sealed class StickySystem : EntitySystem if (component.Blacklist != null && component.Blacklist.IsValid(target)) return false; + var attemptEv = new AttemptEntityStickEvent(target, user); + RaiseLocalEvent(uid, ref attemptEv); + if (attemptEv.Cancelled) + return false; + // check if delay is not zero to start do after var delay = (float) component.StickDelay.TotalSeconds; if (delay > 0) @@ -120,6 +125,14 @@ public sealed class StickySystem : EntitySystem if (!Resolve(uid, ref component)) return; + if (component.StuckTo is not { } stuckTo) + return; + + var attemptEv = new AttemptEntityUnstickEvent(stuckTo, user); + RaiseLocalEvent(uid, ref attemptEv); + if (attemptEv.Cancelled) + return; + var delay = (float) component.UnstickDelay.TotalSeconds; if (delay > 0) { @@ -152,6 +165,11 @@ public sealed class StickySystem : EntitySystem if (!Resolve(uid, ref component)) return; + var attemptEv = new AttemptEntityStickEvent(target, user); + RaiseLocalEvent(uid, ref attemptEv); + if (attemptEv.Cancelled) + return; + // add container to entity and insert sticker into it var container = _containerSystem.EnsureContainer(target, StickerSlotId); container.ShowContents = true; @@ -179,12 +197,17 @@ public sealed class StickySystem : EntitySystem { if (!Resolve(uid, ref component)) return; - if (component.StuckTo == null) + + if (component.StuckTo is not { } stuckTo) + return; + + var attemptEv = new AttemptEntityUnstickEvent(stuckTo, user); + RaiseLocalEvent(uid, ref attemptEv); + if (attemptEv.Cancelled) return; // try to remove sticky item from target container - var target = component.StuckTo.Value; - if (!_containerSystem.TryGetContainer(target, StickerSlotId, out var container) || !container.Remove(uid)) + if (!_containerSystem.TryGetContainer(stuckTo, StickerSlotId, out var container) || !container.Remove(uid)) return; // delete container if it's now empty if (container.ContainedEntities.Count == 0) @@ -207,6 +230,6 @@ public sealed class StickySystem : EntitySystem } component.StuckTo = null; - RaiseLocalEvent(uid, new EntityUnstuckEvent(target, user), true); + RaiseLocalEvent(uid, new EntityUnstuckEvent(stuckTo, user), true); } } -- 2.51.2