Skip to content

Commit

Permalink
gun pushback
Browse files Browse the repository at this point in the history
  • Loading branch information
slarticodefast committed Sep 23, 2024
1 parent 2566c95 commit 8813ad3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Content.Shared/CCVar/CCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ public static readonly CVarDef<int>

/// <summary>
/// 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.
Expand Down
7 changes: 7 additions & 0 deletions Content.Shared/Weapons/Ranged/Components/GunComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ public sealed partial class GunComponent : Component
/// </summary>
[DataField]
public bool ClumsyProof = false;

/// <summary>
/// Pushback impulse applied to the gun's holder or the grid they are standing on when fired.
/// In kg*m/s
/// </summary>
[DataField]
public float ImpulseStrength = 25f;
}

[Flags]
Expand Down
34 changes: 29 additions & 5 deletions Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -97,6 +101,12 @@ public override void Initialize()
SubscribeLocalEvent<GunComponent, CycleModeEvent>(OnCycleMode);
SubscribeLocalEvent<GunComponent, HandSelectedEvent>(OnGunSelected);
SubscribeLocalEvent<GunComponent, MapInitEvent>(OnMapInit);
Subs.CVar(_cfg, CCVars.GridImpulseMultiplier, UpdateGridMassMultiplier, true);
}

private void UpdateGridMassMultiplier(float value)
{
_gridImpulseMultiplier = value;
}

private void OnMapInit(Entity<GunComponent> gun, ref MapInitEvent args)
Expand Down Expand Up @@ -355,7 +365,7 @@ private void AttemptShoot(EntityUid user, EntityUid gunUid, GunComponent gun)
if (userImpulse && TryComp<PhysicsComponent>(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);
Expand Down Expand Up @@ -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<GunComponent?> gun)
Expand Down

0 comments on commit 8813ad3

Please sign in to comment.