Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attack delay to weapon throwing #34903

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Content.Server/Damage/Systems/DamageOtherOnHitSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
using Content.Shared.Damage.Systems;
using Content.Shared.Database;
using Content.Shared.Effects;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Mobs.Components;
using Content.Shared.Throwing;
using Content.Shared.Weapons.Melee;
using Content.Shared.Weapons.Melee.Events;
using Content.Shared.Wires;
using Robust.Shared.Physics.Components;
using Robust.Shared.Player;
Expand All @@ -24,12 +28,14 @@ public sealed class DamageOtherOnHitSystem : EntitySystem
[Dependency] private readonly DamageExamineSystem _damageExamine = default!;
[Dependency] private readonly SharedCameraRecoilSystem _sharedCameraRecoil = default!;
[Dependency] private readonly SharedColorFlashEffectSystem _color = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;

public override void Initialize()
{
SubscribeLocalEvent<DamageOtherOnHitComponent, ThrowDoHitEvent>(OnDoHit);
SubscribeLocalEvent<DamageOtherOnHitComponent, DamageExamineEvent>(OnDamageExamine);
SubscribeLocalEvent<DamageOtherOnHitComponent, AttemptPacifiedThrowEvent>(OnAttemptPacifiedThrow);
SubscribeLocalEvent<DamageOtherOnHitComponent, MeleeHitEvent>(OnMeleeCollideHit);
}

private void OnDoHit(EntityUid uid, DamageOtherOnHitComponent component, ThrowDoHitEvent args)
Expand Down Expand Up @@ -68,5 +74,17 @@ private void OnAttemptPacifiedThrow(Entity<DamageOtherOnHitComponent> ent, ref A
{
args.Cancel("pacified-cannot-throw");
}

/// <summary>
/// Prevent immediately throwing this weapon after using it for a melee attack.
/// </summary>
private void OnMeleeCollideHit(Entity<DamageOtherOnHitComponent> ent, ref MeleeHitEvent args)
{
if (!TryComp<HandsComponent>(args.User, out var hands) || !TryComp<MeleeWeaponComponent>(ent.Owner, out var melee))
return;

if (hands.NextThrowTime < melee.NextAttack)
_hands.ResetThrowCooldown(args.User, hands, melee.NextAttack);
}
}
}
16 changes: 13 additions & 3 deletions Content.Server/Hands/Systems/HandsSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Numerics;
using Content.Server.Inventory;
using Content.Server.Popups;
using Content.Server.Stack;
using Content.Server.Stunnable;
using Content.Shared.ActionBlocker;
Expand Down Expand Up @@ -36,6 +37,7 @@ public sealed class HandsSystem : SharedHandsSystem
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
[Dependency] private readonly PullingSystem _pullingSystem = default!;
[Dependency] private readonly ThrowingSystem _throwingSystem = default!;
[Dependency] private readonly PopupSystem _popup = default!;

public override void Initialize()
{
Expand Down Expand Up @@ -93,7 +95,7 @@ private void OnDisarmed(EntityUid uid, HandsComponent component, DisarmedEvent a
_pullingSystem.TryStopPull(puller.Pulling.Value, pullable);

var offsetRandomCoordinates = _transformSystem.GetMoverCoordinates(args.Target).Offset(_random.NextVector2(1f, 1.5f));
if (!ThrowHeldItem(args.Target, offsetRandomCoordinates))
if (!ThrowHeldItem(args.Target, offsetRandomCoordinates, forcePlayerThrow: true))
return;

args.PopupPrefix = "disarm-action-";
Expand Down Expand Up @@ -179,16 +181,24 @@ private bool HandleThrowItem(ICommonSession? playerSession, EntityCoordinates co
/// <summary>
/// Throw the player's currently held item.
/// </summary>
public bool ThrowHeldItem(EntityUid player, EntityCoordinates coordinates, float minDistance = 0.1f)
/// <param name="player">The entity with hands who is doing the throwing.</param>
/// <param name="coordinates">Target coordinates for the throw.</param>
/// <param name="minDistance">Minimum distance a throw may result in.</param>
/// <param name="forcePlayerThrow">If the throw should bypass checks involving the player (e.g. throw cooldowns for disarming).</param>
/// <returns></returns>
public bool ThrowHeldItem(EntityUid player, EntityCoordinates coordinates, float minDistance = 0.1f, bool forcePlayerThrow = false)
{
if (ContainerSystem.IsEntityInContainer(player) ||
!TryComp(player, out HandsComponent? hands) ||
hands.ActiveHandEntity is not { } throwEnt ||
!_actionBlockerSystem.CanThrow(player, throwEnt))
return false;

if (_timing.CurTime < hands.NextThrowTime)
if (!forcePlayerThrow && _timing.CurTime < hands.NextThrowTime)
{
_popup.PopupEntity(Loc.GetString("hands-system-throwing-cooldown"), player, player);
return false;
}
hands.NextThrowTime = _timing.CurTime + hands.ThrowCooldown;

if (EntityManager.TryGetComponent(throwEnt, out StackComponent? stack) && stack.Count > 1 && stack.ThrowIndividually)
Expand Down
17 changes: 17 additions & 0 deletions Content.Shared/Damage/Systems/StaminaSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
using Content.Shared.Damage.Events;
using Content.Shared.Database;
using Content.Shared.Effects;
using Content.Shared.Hands.Components;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.IdentityManagement;
using Content.Shared.Popups;
using Content.Shared.Projectiles;
using Content.Shared.Rejuvenate;
using Content.Shared.Rounding;
using Content.Shared.Stunnable;
using Content.Shared.Throwing;
using Content.Shared.Weapons.Melee;
using Content.Shared.Weapons.Melee.Events;
using JetBrains.Annotations;
using Robust.Shared.Audio;
Expand All @@ -34,6 +37,7 @@ public sealed partial class StaminaSystem : EntitySystem
[Dependency] private readonly SharedColorFlashEffectSystem _color = default!;
[Dependency] private readonly SharedStunSystem _stunSystem = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;

/// <summary>
/// How much of a buffer is there between the stun duration and when stuns can be re-applied.
Expand All @@ -56,6 +60,7 @@ public override void Initialize()

SubscribeLocalEvent<StaminaDamageOnCollideComponent, ProjectileHitEvent>(OnProjectileHit);
SubscribeLocalEvent<StaminaDamageOnCollideComponent, ThrowDoHitEvent>(OnThrowHit);
SubscribeLocalEvent<StaminaDamageOnCollideComponent, MeleeHitEvent>(OnMeleeCollideHit);

SubscribeLocalEvent<StaminaDamageOnHitComponent, MeleeHitEvent>(OnMeleeHit);
}
Expand Down Expand Up @@ -190,6 +195,18 @@ private void OnThrowHit(EntityUid uid, StaminaDamageOnCollideComponent component
OnCollide(uid, component, args.Target);
}

/// <summary>
/// Prevent immediately throwing this weapon after using it for a melee attack.
/// </summary>
private void OnMeleeCollideHit(Entity<StaminaDamageOnCollideComponent> ent, ref MeleeHitEvent args)
{
if (!TryComp<HandsComponent>(args.User, out var hands) || !TryComp<MeleeWeaponComponent>(ent.Owner, out var melee))
return;

if (hands.NextThrowTime < melee.NextAttack)
_hands.ResetThrowCooldown(args.User, hands, melee.NextAttack);
}

private void OnCollide(EntityUid uid, StaminaDamageOnCollideComponent component, EntityUid target)
{
// you can't inflict stamina damage on things with no stamina component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System.Linq;
using Content.Shared.Damage.Components;
using Content.Shared.Examine;
using Content.Shared.Hands.Components;
using Content.Shared.IdentityManagement;
using Content.Shared.Input;
using Content.Shared.Interaction;
using Content.Shared.Inventory.VirtualItem;
using Content.Shared.Localizations;
using Content.Shared.Weapons.Melee;
using Content.Shared.Weapons.Melee.Events;
using Robust.Shared.Input.Binding;
using Robust.Shared.Map;
using Robust.Shared.Player;
Expand Down Expand Up @@ -213,4 +216,23 @@ private void HandleExamined(EntityUid examinedUid, HandsComponent handsComp, Exa
args.PushMarkup(Loc.GetString(locKey, locUser, locItems));
}
}

/// <summary>
/// Resets the throw cooldown to allow for throwing, and optionally sets a new throw tim if given.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Resets the throw cooldown to allow for throwing, and optionally sets a new throw tim if given.
/// Resets the throw cooldown to allow for throwing, and optionally sets a new throw time if given.

/// </summary>
/// <param name="customTime">If set, this will be the new NextThrowTime.</param>
public void ResetThrowCooldown(EntityUid uid, HandsComponent? handsComp, TimeSpan? customTime = null)
{
if (!Resolve(uid, ref handsComp))
return;

if (customTime == null || _timing.CurTime > customTime)
{
handsComp.NextThrowTime = _timing.CurTime;
return;
}

handsComp.NextThrowTime = (TimeSpan)customTime;

}
}
2 changes: 2 additions & 0 deletions Content.Shared/Hands/EntitySystems/SharedHandsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Content.Shared.Storage.EntitySystems;
using Robust.Shared.Containers;
using Robust.Shared.Input.Binding;
using Robust.Shared.Timing;

namespace Content.Shared.Hands.EntitySystems;

Expand All @@ -22,6 +23,7 @@ public abstract partial class SharedHandsSystem
[Dependency] private readonly SharedStorageSystem _storage = default!;
[Dependency] protected readonly SharedTransformSystem TransformSystem = default!;
[Dependency] private readonly SharedVirtualItemSystem _virtualSystem = default!;
[Dependency] private readonly IGameTiming _timing = default!;

protected event Action<Entity<HandsComponent>?>? OnHandSetActive;

Expand Down
2 changes: 1 addition & 1 deletion Content.Shared/Throwing/ThrowingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void TryThrow(EntityUid uid,

ThrowingAngleComponent? throwingAngle = null;

// Give it a l'il spin.
// Give it a li'l spin.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely vital change! 😄

if (doSpin)
{
if (physics.InvI > 0f && (!TryComp(uid, out throwingAngle) || throwingAngle.AngularVelocity))
Expand Down
2 changes: 2 additions & 0 deletions Resources/Locale/en-US/hands/hands-system.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ comp-hands-examine-empty = { CAPITALIZE(SUBJECT($user)) } { CONJUGATE-BE($user)
comp-hands-examine-wrapper = { INDEFINITE($item) } [color=paleturquoise]{$item}[/color]

hands-system-blocked-by = Blocked by

hands-system-throwing-cooldown = You are not ready to throw yet!
Loading