From 8813ad355517f8c92134d63e7299c7aee219758b Mon Sep 17 00:00:00 2001 From: slarticodefast <161409025+slarticodefast@users.noreply.github.com> Date: Mon, 23 Sep 2024 17:46:01 +0200 Subject: [PATCH] gun pushback --- Content.Shared/CCVar/CCVars.cs | 2 +- .../Weapons/Ranged/Components/GunComponent.cs | 7 ++++ .../Weapons/Ranged/Systems/SharedGunSystem.cs | 34 ++++++++++++++++--- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Content.Shared/CCVar/CCVars.cs b/Content.Shared/CCVar/CCVars.cs index 1638dc848b9fdf..0cf7881dff0c63 100644 --- a/Content.Shared/CCVar/CCVars.cs +++ b/Content.Shared/CCVar/CCVars.cs @@ -1493,7 +1493,7 @@ public static readonly CVarDef /// /// Impulse multiplier for player interactions that move grids (other than shuttle thrusters, gyroscopes and grid collisons). - /// At the moment this only affects the pushback in SpraySystem. + /// At the moment this affects the pushback in SpraySystem and from shooting guns. /// A higher value means grids have a lower effective mass and therefore will get pushed stronger. /// A value of 0 will disable pushback. /// The default has been chosen such that a one tile grid roughly equals 2/3 Urist masses. diff --git a/Content.Shared/Weapons/Ranged/Components/GunComponent.cs b/Content.Shared/Weapons/Ranged/Components/GunComponent.cs index b404221abfc731..2d89f051fb6223 100644 --- a/Content.Shared/Weapons/Ranged/Components/GunComponent.cs +++ b/Content.Shared/Weapons/Ranged/Components/GunComponent.cs @@ -232,6 +232,13 @@ public sealed partial class GunComponent : Component /// [DataField] public bool ClumsyProof = false; + + /// + /// Pushback impulse applied to the gun's holder or the grid they are standing on when fired. + /// In kg*m/s + /// + [DataField] + public float ImpulseStrength = 25f; } [Flags] diff --git a/Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs b/Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs index 794237b145a052..59675f9f9330ef 100644 --- a/Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs +++ b/Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs @@ -4,6 +4,7 @@ using Content.Shared.Actions; using Content.Shared.Administration.Logs; using Content.Shared.Audio; +using Content.Shared.CCVar; using Content.Shared.CombatMode; using Content.Shared.Containers.ItemSlots; using Content.Shared.Damage; @@ -24,6 +25,7 @@ using Content.Shared.Whitelist; using Robust.Shared.Audio; using Robust.Shared.Audio.Systems; +using Robust.Shared.Configuration; using Robust.Shared.Containers; using Robust.Shared.Map; using Robust.Shared.Network; @@ -65,7 +67,9 @@ public abstract partial class SharedGunSystem : EntitySystem [Dependency] protected readonly ThrowingSystem ThrowingSystem = default!; [Dependency] private readonly UseDelaySystem _useDelay = default!; [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!; + [Dependency] private readonly IConfigurationManager _cfg = default!; + private float _gridImpulseMultiplier; private const float InteractNextFire = 0.3f; private const double SafetyNextFire = 0.5; private const float EjectOffset = 0.4f; @@ -97,6 +101,12 @@ public override void Initialize() SubscribeLocalEvent(OnCycleMode); SubscribeLocalEvent(OnGunSelected); SubscribeLocalEvent(OnMapInit); + Subs.CVar(_cfg, CCVars.GridImpulseMultiplier, UpdateGridMassMultiplier, true); + } + + private void UpdateGridMassMultiplier(float value) + { + _gridImpulseMultiplier = value; } private void OnMapInit(Entity gun, ref MapInitEvent args) @@ -355,7 +365,7 @@ private void AttemptShoot(EntityUid user, EntityUid gunUid, GunComponent gun) if (userImpulse && TryComp(user, out var userPhysics)) { if (_gravity.IsWeightless(user, userPhysics)) - CauseImpulse(fromCoordinates, toCoordinates.Value, user, userPhysics); + CauseImpulse(gun.ImpulseStrength, fromCoordinates, toCoordinates.Value, user, userPhysics); } Dirty(gunUid, gun); @@ -479,15 +489,29 @@ protected void MuzzleFlash(EntityUid gun, AmmoComponent component, Angle worldAn CreateEffect(gun, ev, gun); } - public void CauseImpulse(EntityCoordinates fromCoordinates, EntityCoordinates toCoordinates, EntityUid user, PhysicsComponent userPhysics) + public void CauseImpulse(float impulseStrength, EntityCoordinates fromCoordinates, EntityCoordinates toCoordinates, EntityUid user, PhysicsComponent userPhysics) { var fromMap = fromCoordinates.ToMapPos(EntityManager, TransformSystem); var toMap = toCoordinates.ToMapPos(EntityManager, TransformSystem); var shotDirection = (toMap - fromMap).Normalized(); - const float impulseStrength = 25.0f; - var impulseVector = shotDirection * impulseStrength; - Physics.ApplyLinearImpulse(user, -impulseVector, body: userPhysics); + var impulseVector = shotDirection * impulseStrength; + if (_gravity.IsWeightless(user, userPhysics)) + { + // push back the player + Physics.ApplyLinearImpulse(user, -impulseVector, body: userPhysics); + } + else + { + // push back the grid the player is standing on + var userTransform = Transform(user); + if (userTransform.GridUid != null) + { + // apply both linear and angular momentum depending on the player position + // multiply by a cvar because grid mass is currently extremely small compared to all other masses + Physics.ApplyLinearImpulse(userTransform.GridUid.Value, -impulseVector * _gridImpulseMultiplier, userTransform.LocalPosition); + } + } } public void RefreshModifiers(Entity gun)