]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Adds AttemptEntity(Uns|S)tickEvent. (#20728)
authorTemporalOroboros <TemporalOroboros@gmail.com>
Wed, 25 Oct 2023 01:45:42 +0000 (18:45 -0700)
committerGitHub <noreply@github.com>
Wed, 25 Oct 2023 01:45:42 +0000 (21:45 -0400)
* try-stick

* convert spider charge to attempt-stick-events

Content.Server/Ninja/Systems/SpiderChargeSystem.cs
Content.Server/Sticky/Events/EntityStuckEvent.cs
Content.Server/Sticky/Systems/StickySystem.cs

index 6f3c3b3f9df852096bd8c34fda51dffe25c6a691..0182bd7ca78f0061067d74677d175f6f2421cd6c 100644 (file)
@@ -23,7 +23,7 @@ public sealed class SpiderChargeSystem : EntitySystem
     {
         base.Initialize();
 
-        SubscribeLocalEvent<SpiderChargeComponent, BeforeRangedInteractEvent>(BeforePlant);
+        SubscribeLocalEvent<SpiderChargeComponent, AttemptEntityStickEvent>(OnAttemptStick);
         SubscribeLocalEvent<SpiderChargeComponent, EntityStuckEvent>(OnStuck);
         SubscribeLocalEvent<SpiderChargeComponent, TriggerEvent>(OnExplode);
     }
@@ -31,14 +31,17 @@ public sealed class SpiderChargeSystem : EntitySystem
     /// <summary>
     /// Require that the planter is a ninja and the charge is near the target warp point.
     /// </summary>
-    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<NinjaRoleComponent>(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;
         }
     }
 
index b9244364890ea46ee5f2316642786005ba161d7a..7857fad7d5757cd1967bfc805bbe564e3c2e3555 100644 (file)
@@ -1,5 +1,28 @@
 namespace Content.Server.Sticky.Events;
 
+/// <summary>
+///     Risen on sticky entity to see if it can stick to another entity.
+/// </summary>
+[ByRefEvent]
+public record struct AttemptEntityStickEvent(EntityUid Target, EntityUid User)
+{
+    public readonly EntityUid Target = Target;
+    public readonly EntityUid User = User;
+    public bool Cancelled = false;
+}
+
+/// <summary>
+///     Risen on sticky entity to see if it can unstick from another entity.
+/// </summary>
+[ByRefEvent]
+public record struct AttemptEntityUnstickEvent(EntityUid Target, EntityUid User)
+{
+    public readonly EntityUid Target = Target;
+    public readonly EntityUid User = User;
+    public bool Cancelled = false;
+}
+
+
 /// <summary>
 ///     Risen on sticky entity when it was stuck to other entity.
 /// </summary>
index 330b878c05a8364f759d339a1dca2217d9ab1803..bcc1be39a92595de1c6d575cf34138f2a6128447 100644 (file)
@@ -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<Container>(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);
     }
 }