]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Throwing triggers (#39650)
authorslarticodefast <161409025+slarticodefast@users.noreply.github.com>
Fri, 15 Aug 2025 05:33:37 +0000 (07:33 +0200)
committerGitHub <noreply@github.com>
Fri, 15 Aug 2025 05:33:37 +0000 (22:33 -0700)
throw triggers

Content.Shared/Sound/Components/EmitSoundOnThrowComponent.cs
Content.Shared/Throwing/ThrowEvents.cs
Content.Shared/Throwing/ThrowingSystem.cs
Content.Shared/Throwing/ThrownEvent.cs [deleted file]
Content.Shared/Throwing/ThrownItemSystem.cs
Content.Shared/Trigger/Components/Triggers/TriggerOnThrowComponent.cs [new file with mode: 0644]
Content.Shared/Trigger/Components/Triggers/TriggerOnThrownComponent.cs [new file with mode: 0644]
Content.Shared/Trigger/Systems/TriggerSystem.Interaction.cs

index 76e9f080768e4f0c07dd61693d425a74b2291672..0498642c00d45a613d91d341c4b18c0513b0b4e0 100644 (file)
@@ -3,7 +3,7 @@ using Robust.Shared.GameStates;
 namespace Content.Shared.Sound.Components;
 
 /// <summary>
-/// Simple sound emitter that emits sound on ThrowEvent
+/// Simple sound emitter that emits sound on ThrownEvent
 /// </summary>
 [RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
 public sealed partial class EmitSoundOnThrowComponent : BaseEmitSoundComponent;
index fbda80b8ca9d35a7c5aea88002c91a9aec400939..8b60a7b4b1f59e5efcac8ac4ae64d890e1a656e5 100644 (file)
@@ -1,39 +1,25 @@
-namespace Content.Shared.Throwing
-{
-    /// <summary>
-    ///     Base class for all throw events.
-    /// </summary>
-    public abstract class ThrowEvent : HandledEntityEventArgs
-    {
-        public readonly EntityUid Thrown;
-        public readonly EntityUid Target;
-        public ThrownItemComponent Component;
+namespace Content.Shared.Throwing;
 
-        public ThrowEvent(EntityUid thrown, EntityUid target, ThrownItemComponent component)
-        {
-            Thrown = thrown;
-            Target = target;
-            Component = component;
-        }
-    }
+/// <summary>
+/// Raised on an entity after it has thrown something.
+/// </summary>
+[ByRefEvent]
+public readonly record struct ThrowEvent(EntityUid? User, EntityUid Thrown);
 
-    /// <summary>
-    ///     Raised directed on the target entity being hit by the thrown entity.
-    /// </summary>
-    public sealed class ThrowHitByEvent : ThrowEvent
-    {
-        public ThrowHitByEvent(EntityUid thrown, EntityUid target, ThrownItemComponent component) : base(thrown, target, component)
-        {
-        }
-    }
+/// <summary>
+/// Raised on an entity after it has been thrown.
+/// </summary>
+[ByRefEvent]
+public readonly record struct ThrownEvent(EntityUid? User, EntityUid Thrown);
 
-    /// <summary>
-    ///     Raised directed on the thrown entity that hits another.
-    /// </summary>
-    public sealed class ThrowDoHitEvent : ThrowEvent
-    {
-        public ThrowDoHitEvent(EntityUid thrown, EntityUid target, ThrownItemComponent component) : base(thrown, target, component)
-        {
-        }
-    }
-}
+/// <summary>
+/// Raised directed on the target entity being hit by the thrown entity.
+/// </summary>
+[ByRefEvent]
+public readonly record struct ThrowHitByEvent(EntityUid Thrown, EntityUid Target, ThrownItemComponent Component);
+
+/// <summary>
+/// Raised directed on the thrown entity that hits another.
+/// </summary>
+[ByRefEvent]
+public readonly record struct ThrowDoHitEvent(EntityUid Thrown, EntityUid Target, ThrownItemComponent Component);
index ceb9cf8bfb2b5405524cb1e584cad79c7182de56..4e44901c57f792a75b222b27efcd7d3e208320ec 100644 (file)
@@ -192,8 +192,6 @@ public sealed class ThrowingSystem : EntitySystem
             }
         }
 
-        var throwEvent = new ThrownEvent(user, uid);
-        RaiseLocalEvent(uid, ref throwEvent, true);
         if (user != null)
             _adminLogger.Add(LogType.Throw, LogImpact.Low, $"{ToPrettyString(user.Value):user} threw {ToPrettyString(uid):entity}");
 
@@ -206,6 +204,14 @@ public sealed class ThrowingSystem : EntitySystem
         var impulseVector = direction.Normalized() * throwSpeed * physics.Mass;
         _physics.ApplyLinearImpulse(uid, impulseVector, body: physics);
 
+        var thrownEvent = new ThrownEvent(user, uid);
+        RaiseLocalEvent(uid, ref thrownEvent, true);
+        if (user != null)
+        {
+            var throwEvent = new ThrowEvent(user, uid);
+            RaiseLocalEvent(user.Value, ref throwEvent, true);
+        }
+
         if (comp.LandTime == null || comp.LandTime <= TimeSpan.Zero)
         {
             _thrownSystem.LandComponent(uid, comp, physics, playSound);
diff --git a/Content.Shared/Throwing/ThrownEvent.cs b/Content.Shared/Throwing/ThrownEvent.cs
deleted file mode 100644 (file)
index 70cb6ee..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-using JetBrains.Annotations;
-
-namespace Content.Shared.Throwing;
-
-/// <summary>
-///     Raised on thrown entity.
-/// </summary>
-[PublicAPI]
-[ByRefEvent]
-public readonly record struct ThrownEvent(EntityUid? User, EntityUid Thrown);
index 65c5a0f13ef6ddf2ab88a7d0c00472d72f1482da..5adad359e524dc0ff07d6b84386c61d2b5ab20d6 100644 (file)
@@ -140,8 +140,10 @@ namespace Content.Shared.Throwing
                 _adminLogger.Add(LogType.ThrowHit, LogImpact.Low,
                     $"{ToPrettyString(thrown):thrown} thrown by {ToPrettyString(component.Thrower.Value):thrower} hit {ToPrettyString(target):target}.");
 
-            RaiseLocalEvent(target, new ThrowHitByEvent(thrown, target, component), true);
-            RaiseLocalEvent(thrown, new ThrowDoHitEvent(thrown, target, component), true);
+            var hitByEv = new ThrowHitByEvent(thrown, target, component);
+            var doHitEv = new ThrowDoHitEvent(thrown, target, component);
+            RaiseLocalEvent(target, ref hitByEv, true);
+            RaiseLocalEvent(thrown, ref doHitEv, true);
         }
 
         public override void Update(float frameTime)
diff --git a/Content.Shared/Trigger/Components/Triggers/TriggerOnThrowComponent.cs b/Content.Shared/Trigger/Components/Triggers/TriggerOnThrowComponent.cs
new file mode 100644 (file)
index 0000000..e9249a8
--- /dev/null
@@ -0,0 +1,10 @@
+using Robust.Shared.GameStates;
+
+namespace Content.Shared.Trigger.Components.Triggers;
+
+/// <summary>
+/// Triggers when after an entity has thrown something.
+/// The user is the thrown item.
+/// </summary>
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class TriggerOnThrowComponent : BaseTriggerOnXComponent;
diff --git a/Content.Shared/Trigger/Components/Triggers/TriggerOnThrownComponent.cs b/Content.Shared/Trigger/Components/Triggers/TriggerOnThrownComponent.cs
new file mode 100644 (file)
index 0000000..e309261
--- /dev/null
@@ -0,0 +1,10 @@
+using Robust.Shared.GameStates;
+
+namespace Content.Shared.Trigger.Components.Triggers;
+
+/// <summary>
+/// Triggers when an entity was thrown.
+/// The user is the thrower.
+/// </summary>
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class TriggerOnThrownComponent : BaseTriggerOnXComponent;
index 230b628663e7b968fee244e99588863f230a5629..39ef4889de1c422f6b0df11b7541766e7749e18c 100644 (file)
@@ -2,6 +2,7 @@
 using Content.Shared.Interaction;
 using Content.Shared.Interaction.Events;
 using Content.Shared.Item.ItemToggle.Components;
+using Content.Shared.Throwing;
 using Content.Shared.Trigger.Components.Triggers;
 using Content.Shared.Trigger.Components.Effects;
 
@@ -12,10 +13,11 @@ public sealed partial class TriggerSystem
     private void InitializeInteraction()
     {
         SubscribeLocalEvent<TriggerOnExaminedComponent, ExaminedEvent>(OnExamined);
-
         SubscribeLocalEvent<TriggerOnActivateComponent, ActivateInWorldEvent>(OnActivate);
         SubscribeLocalEvent<TriggerOnUseComponent, UseInHandEvent>(OnUse);
         SubscribeLocalEvent<TriggerOnInteractHandComponent, InteractHandEvent>(OnInteractHand);
+        SubscribeLocalEvent<TriggerOnThrowComponent, ThrowEvent>(OnThrow);
+        SubscribeLocalEvent<TriggerOnThrownComponent, ThrownEvent>(OnThrown);
 
         SubscribeLocalEvent<ItemToggleOnTriggerComponent, TriggerEvent>(HandleItemToggleOnTrigger);
         SubscribeLocalEvent<AnchorOnTriggerComponent, TriggerEvent>(HandleAnchorOnTrigger);
@@ -57,6 +59,16 @@ public sealed partial class TriggerSystem
         args.Handled = true;
     }
 
+    private void OnThrow(Entity<TriggerOnThrowComponent> ent, ref ThrowEvent args)
+    {
+        Trigger(ent.Owner, args.Thrown, ent.Comp.KeyOut);
+    }
+
+    private void OnThrown(Entity<TriggerOnThrownComponent> ent, ref ThrownEvent args)
+    {
+        Trigger(ent.Owner, args.User, ent.Comp.KeyOut);
+    }
+
     private void HandleItemToggleOnTrigger(Entity<ItemToggleOnTriggerComponent> ent, ref TriggerEvent args)
     {
         if (args.Key != null && !ent.Comp.KeysIn.Contains(args.Key))