Skip to content

Commit

Permalink
Make failing to fire a gun that requires wielding not delay the next …
Browse files Browse the repository at this point in the history
…shot (space-wizards#27973)

Make failing to fire a wield-only gun not delay the next shot
  • Loading branch information
DrSmugleaf authored May 14, 2024
1 parent 63c551b commit f22e540
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ namespace Content.Shared.Weapons.Ranged.Components;
/// <summary>
/// Indicates that this gun requires wielding to be useable.
/// </summary>
[RegisterComponent, NetworkedComponent, Access(typeof(WieldableSystem))]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(WieldableSystem))]
public sealed partial class GunRequiresWieldComponent : Component
{
[DataField, AutoNetworkedField]
public TimeSpan LastPopup;

[DataField, AutoNetworkedField]
public TimeSpan PopupCooldown = TimeSpan.FromSeconds(1);
}
4 changes: 3 additions & 1 deletion Content.Shared/Weapons/Ranged/Events/ShotAttemptedEvent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Content.Shared.Weapons.Ranged.Components;

namespace Content.Shared.Weapons.Ranged.Events;

/// <summary>
Expand All @@ -15,7 +17,7 @@ public record struct ShotAttemptedEvent
/// <summary>
/// The gun being shot.
/// </summary>
public EntityUid Used;
public Entity<GunComponent> Used;

public bool Cancelled { get; private set; }

Expand Down
2 changes: 1 addition & 1 deletion Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private void AttemptShoot(EntityUid user, EntityUid gunUid, GunComponent gun)
var prevention = new ShotAttemptedEvent
{
User = user,
Used = gunUid
Used = (gunUid, gun)
};
RaiseLocalEvent(gunUid, ref prevention);
if (prevention.Cancelled)
Expand Down
18 changes: 12 additions & 6 deletions Content.Shared/Wieldable/WieldableSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using Content.Shared.Weapons.Ranged.Systems;
using Content.Shared.Wieldable.Components;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Player;
using Robust.Shared.Timing;

namespace Content.Shared.Wieldable;

Expand All @@ -30,6 +30,7 @@ public sealed class WieldableSystem : EntitySystem
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly UseDelaySystem _delay = default!;
[Dependency] private readonly SharedGunSystem _gun = default!;
[Dependency] private readonly IGameTiming _timing = default!;

public override void Initialize()
{
Expand All @@ -42,7 +43,7 @@ public override void Initialize()
SubscribeLocalEvent<WieldableComponent, GetVerbsEvent<InteractionVerb>>(AddToggleWieldVerb);

SubscribeLocalEvent<MeleeRequiresWieldComponent, AttemptMeleeEvent>(OnMeleeAttempt);
SubscribeLocalEvent<GunRequiresWieldComponent, AttemptShootEvent>(OnShootAttempt);
SubscribeLocalEvent<GunRequiresWieldComponent, ShotAttemptedEvent>(OnShootAttempt);
SubscribeLocalEvent<GunWieldBonusComponent, ItemWieldedEvent>(OnGunWielded);
SubscribeLocalEvent<GunWieldBonusComponent, ItemUnwieldedEvent>(OnGunUnwielded);
SubscribeLocalEvent<GunWieldBonusComponent, GunRefreshModifiersEvent>(OnGunRefreshModifiers);
Expand All @@ -61,16 +62,21 @@ private void OnMeleeAttempt(EntityUid uid, MeleeRequiresWieldComponent component
}
}

private void OnShootAttempt(EntityUid uid, GunRequiresWieldComponent component, ref AttemptShootEvent args)
private void OnShootAttempt(EntityUid uid, GunRequiresWieldComponent component, ref ShotAttemptedEvent args)
{
if (TryComp<WieldableComponent>(uid, out var wieldable) &&
!wieldable.Wielded)
{
args.Cancelled = true;
args.Cancel();

if (!HasComp<MeleeWeaponComponent>(uid) && !HasComp<MeleeRequiresWieldComponent>(uid))
var time = _timing.CurTime;
if (time > component.LastPopup + component.PopupCooldown &&
!HasComp<MeleeWeaponComponent>(uid) &&
!HasComp<MeleeRequiresWieldComponent>(uid))
{
args.Message = Loc.GetString("wieldable-component-requires", ("item", uid));
component.LastPopup = time;
var message = Loc.GetString("wieldable-component-requires", ("item", uid));
_popupSystem.PopupClient(message, args.Used, args.User);
}
}
}
Expand Down

0 comments on commit f22e540

Please sign in to comment.