Give throwing a cooldown (#23311)

This commit is contained in:
Nemanja
2023-12-31 22:24:37 -05:00
committed by GitHub
parent e76d8390bd
commit ad5f7a5c76
2 changed files with 25 additions and 1 deletions

View File

@@ -17,12 +17,14 @@ using Robust.Shared.GameStates;
using Robust.Shared.Input.Binding; using Robust.Shared.Input.Binding;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Player; using Robust.Shared.Player;
using Robust.Shared.Timing;
using Robust.Shared.Utility; using Robust.Shared.Utility;
namespace Content.Server.Hands.Systems namespace Content.Server.Hands.Systems
{ {
public sealed class HandsSystem : SharedHandsSystem public sealed class HandsSystem : SharedHandsSystem
{ {
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly StackSystem _stackSystem = default!; [Dependency] private readonly StackSystem _stackSystem = default!;
[Dependency] private readonly HandVirtualItemSystem _virtualItemSystem = default!; [Dependency] private readonly HandVirtualItemSystem _virtualItemSystem = default!;
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!; [Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
@@ -43,6 +45,7 @@ namespace Content.Server.Hands.Systems
SubscribeLocalEvent<HandsComponent, BodyPartRemovedEvent>(HandleBodyPartRemoved); SubscribeLocalEvent<HandsComponent, BodyPartRemovedEvent>(HandleBodyPartRemoved);
SubscribeLocalEvent<HandsComponent, ComponentGetState>(GetComponentState); SubscribeLocalEvent<HandsComponent, ComponentGetState>(GetComponentState);
SubscribeLocalEvent<HandsComponent, EntityUnpausedEvent>(OnUnpaused);
SubscribeLocalEvent<HandsComponent, BeforeExplodeEvent>(OnExploded); SubscribeLocalEvent<HandsComponent, BeforeExplodeEvent>(OnExploded);
@@ -63,6 +66,11 @@ namespace Content.Server.Hands.Systems
args.State = new HandsComponentState(hands); args.State = new HandsComponentState(hands);
} }
private void OnUnpaused(Entity<HandsComponent> ent, ref EntityUnpausedEvent args)
{
ent.Comp.NextThrowTime += args.PausedTime;
}
private void OnExploded(Entity<HandsComponent> ent, ref BeforeExplodeEvent args) private void OnExploded(Entity<HandsComponent> ent, ref BeforeExplodeEvent args)
{ {
foreach (var hand in ent.Comp.Hands.Values) foreach (var hand in ent.Comp.Hands.Values)
@@ -172,6 +180,10 @@ namespace Content.Server.Hands.Systems
!_actionBlockerSystem.CanThrow(player, throwEnt)) !_actionBlockerSystem.CanThrow(player, throwEnt))
return false; return false;
if (_timing.CurTime < hands.NextThrowTime)
return false;
hands.NextThrowTime = _timing.CurTime + hands.ThrowCooldown;
if (EntityManager.TryGetComponent(throwEnt, out StackComponent? stack) && stack.Count > 1 && stack.ThrowIndividually) if (EntityManager.TryGetComponent(throwEnt, out StackComponent? stack) && stack.Count > 1 && stack.ThrowIndividually)
{ {
var splitStack = _stackSystem.Split(throwEnt, 1, EntityManager.GetComponent<TransformComponent>(player).Coordinates, stack); var splitStack = _stackSystem.Split(throwEnt, 1, EntityManager.GetComponent<TransformComponent>(player).Coordinates, stack);
@@ -201,7 +213,7 @@ namespace Content.Server.Hands.Systems
return true; return true;
// This can grief the above event so we raise it afterwards // This can grief the above event so we raise it afterwards
if (!TryDrop(player, throwEnt, handsComp: hands)) if (IsHolding(player, throwEnt, out _, hands) && !TryDrop(player, throwEnt, handsComp: hands))
return false; return false;
_throwingSystem.TryThrow(ev.ItemUid, ev.Direction, ev.ThrowStrength, ev.PlayerUid); _throwingSystem.TryThrow(ev.ItemUid, ev.Direction, ev.ThrowStrength, ev.PlayerUid);

View File

@@ -57,6 +57,18 @@ public sealed partial class HandsComponent : Component
/// Used by the client. /// Used by the client.
/// </summary> /// </summary>
public readonly Dictionary<HandLocation, HashSet<string>> RevealedLayers = new(); public readonly Dictionary<HandLocation, HashSet<string>> RevealedLayers = new();
/// <summary>
/// The time at which throws will be allowed again.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public TimeSpan NextThrowTime;
/// <summary>
/// The minimum time inbetween throws.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public TimeSpan ThrowCooldown = TimeSpan.FromSeconds(0.5f);
} }
[Serializable, NetSerializable] [Serializable, NetSerializable]