From 25763e4ace85779bc44c9a5c786df66391886714 Mon Sep 17 00:00:00 2001 From: deltanedas <39013340+deltanedas@users.noreply.github.com> Date: Thu, 8 Jun 2023 02:15:39 +0000 Subject: [PATCH] add an event to prevent gun shooting (#17104) Co-authored-by: deltanedas <@deltanedas:kde.org> --- .../Weapons/Melee/SharedMeleeWeaponSystem.cs | 8 +++++ .../Ranged/Events/ShotAttemptedEvent.cs | 32 +++++++++++++++++++ .../Weapons/Ranged/Systems/SharedGunSystem.cs | 9 ++++-- 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 Content.Shared/Weapons/Ranged/Events/ShotAttemptedEvent.cs diff --git a/Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs b/Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs index 37b11ada7e..ecfc85daca 100644 --- a/Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs +++ b/Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs @@ -16,6 +16,7 @@ using Content.Shared.Popups; using Content.Shared.Weapons.Melee.Components; using Content.Shared.Weapons.Melee.Events; using Content.Shared.Weapons.Ranged.Components; +using Content.Shared.Weapons.Ranged.Events; using Content.Shared.Weapons.Ranged.Systems; using Robust.Shared.Audio; using Robust.Shared.GameStates; @@ -71,6 +72,7 @@ public abstract class SharedMeleeWeaponSystem : EntitySystem SubscribeLocalEvent(OnHandleState); SubscribeLocalEvent(OnMeleeDropped); SubscribeLocalEvent(OnMeleeSelected); + SubscribeLocalEvent(OnMeleeShotAttempted); SubscribeLocalEvent(OnMeleeShot); SubscribeLocalEvent(OnGetBonusMeleeDamage); SubscribeLocalEvent(OnGetBonusHeavyDamageModifier); @@ -95,6 +97,12 @@ public abstract class SharedMeleeWeaponSystem : EntitySystem #endif } + private void OnMeleeShotAttempted(EntityUid uid, MeleeWeaponComponent comp, ref ShotAttemptedEvent args) + { + if (comp.NextAttack > Timing.CurTime) + args.Cancel(); + } + private void OnMeleeShot(EntityUid uid, MeleeWeaponComponent component, ref GunShotEvent args) { if (!TryComp(uid, out var gun)) diff --git a/Content.Shared/Weapons/Ranged/Events/ShotAttemptedEvent.cs b/Content.Shared/Weapons/Ranged/Events/ShotAttemptedEvent.cs new file mode 100644 index 0000000000..2b5628f751 --- /dev/null +++ b/Content.Shared/Weapons/Ranged/Events/ShotAttemptedEvent.cs @@ -0,0 +1,32 @@ +namespace Content.Shared.Weapons.Ranged.Events; + +/// +/// Raised on a gun when someone is attempting to shoot it. +/// Cancel this event to prevent it from shooting. +/// +[ByRefEvent] +public record struct ShotAttemptedEvent +{ + /// + /// The user attempting to shoot the gun. + /// + public EntityUid User; + + public bool Cancelled { get; private set; } + + /// + /// Prevent the gun from shooting + /// + public void Cancel() + { + Cancelled = true; + } + + /// + /// Allow the gun to shoot again, only use if you know what you are doing + /// + public void Uncancel() + { + Cancelled = false; + } +} diff --git a/Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs b/Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs index 22243015f1..b7002b1d99 100644 --- a/Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs +++ b/Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs @@ -221,8 +221,13 @@ public abstract partial class SharedGunSystem : EntitySystem var curTime = Timing.CurTime; - // Maybe Raise an event for this? CanAttack doesn't seem appropriate. - if (TryComp(gunUid, out var melee) && melee.NextAttack > curTime) + // check if anything wants to prevent shooting + var prevention = new ShotAttemptedEvent + { + User = user + }; + RaiseLocalEvent(gunUid, ref prevention); + if (prevention.Cancelled) return; // Need to do this to play the clicking sound for empty automatic weapons