add an event to prevent gun shooting (#17104)
Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
@@ -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<MeleeWeaponComponent, ComponentHandleState>(OnHandleState);
|
||||
SubscribeLocalEvent<MeleeWeaponComponent, HandDeselectedEvent>(OnMeleeDropped);
|
||||
SubscribeLocalEvent<MeleeWeaponComponent, HandSelectedEvent>(OnMeleeSelected);
|
||||
SubscribeLocalEvent<MeleeWeaponComponent, ShotAttemptedEvent>(OnMeleeShotAttempted);
|
||||
SubscribeLocalEvent<MeleeWeaponComponent, GunShotEvent>(OnMeleeShot);
|
||||
SubscribeLocalEvent<BonusMeleeDamageComponent, GetMeleeDamageEvent>(OnGetBonusMeleeDamage);
|
||||
SubscribeLocalEvent<BonusMeleeDamageComponent, GetHeavyDamageModifierEvent>(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<GunComponent>(uid, out var gun))
|
||||
|
||||
32
Content.Shared/Weapons/Ranged/Events/ShotAttemptedEvent.cs
Normal file
32
Content.Shared/Weapons/Ranged/Events/ShotAttemptedEvent.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
namespace Content.Shared.Weapons.Ranged.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Raised on a gun when someone is attempting to shoot it.
|
||||
/// Cancel this event to prevent it from shooting.
|
||||
/// </summary>
|
||||
[ByRefEvent]
|
||||
public record struct ShotAttemptedEvent
|
||||
{
|
||||
/// <summary>
|
||||
/// The user attempting to shoot the gun.
|
||||
/// </summary>
|
||||
public EntityUid User;
|
||||
|
||||
public bool Cancelled { get; private set; }
|
||||
|
||||
/// </summary>
|
||||
/// Prevent the gun from shooting
|
||||
/// </summary>
|
||||
public void Cancel()
|
||||
{
|
||||
Cancelled = true;
|
||||
}
|
||||
|
||||
/// </summary>
|
||||
/// Allow the gun to shoot again, only use if you know what you are doing
|
||||
/// </summary>
|
||||
public void Uncancel()
|
||||
{
|
||||
Cancelled = false;
|
||||
}
|
||||
}
|
||||
@@ -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<MeleeWeaponComponent>(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
|
||||
|
||||
Reference in New Issue
Block a user