Adds grappling gun (#16662)

This commit is contained in:
metalgearsloth
2023-05-27 14:15:15 +10:00
committed by GitHub
parent 9eb4d4edb0
commit 552fbb0585
48 changed files with 753 additions and 35 deletions

View File

@@ -0,0 +1,9 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Weapons.Misc;
[RegisterComponent, NetworkedComponent]
public sealed class GrapplingProjectileComponent : Component
{
}

View File

@@ -0,0 +1,230 @@
using Content.Shared.CombatMode;
using Content.Shared.Hands;
using Content.Shared.Hands.Components;
using Content.Shared.Interaction;
using Content.Shared.Movement.Events;
using Content.Shared.Physics;
using Content.Shared.Projectiles;
using Content.Shared.Timing;
using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Systems;
using Robust.Shared.Network;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Dynamics.Joints;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Shared.Weapons.Misc;
public abstract class SharedGrapplingGunSystem : EntitySystem
{
[Dependency] protected readonly IGameTiming Timing = default!;
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedJointSystem _joints = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly UseDelaySystem _delay = default!;
public const string GrapplingJoint = "grappling";
public const float ReelRate = 2.5f;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GrapplingProjectileComponent, ProjectileEmbedEvent>(OnGrappleCollide);
SubscribeLocalEvent<CanWeightlessMoveEvent>(OnWeightlessMove);
SubscribeAllEvent<RequestGrapplingReelMessage>(OnGrapplingReel);
SubscribeLocalEvent<GrapplingGunComponent, GunShotEvent>(OnGrapplingShot);
SubscribeLocalEvent<GrapplingGunComponent, ActivateInWorldEvent>(OnGunActivate);
SubscribeLocalEvent<GrapplingGunComponent, HandDeselectedEvent>(OnGrapplingDeselected);
}
private void OnGrapplingShot(EntityUid uid, GrapplingGunComponent component, ref GunShotEvent args)
{
foreach (var (shotUid, _) in args.Ammo)
{
if (!HasComp<GrapplingProjectileComponent>(shotUid))
continue;
// At least show the visuals.
component.Projectile = shotUid.Value;
Dirty(component);
var visuals = EnsureComp<JointVisualsComponent>(shotUid.Value);
visuals.Sprite =
new SpriteSpecifier.Rsi(new ResPath("Objects/Weapons/Guns/Launchers/grappling_gun.rsi"), "rope");
visuals.OffsetA = new Vector2(0f, 0.5f);
visuals.Target = uid;
Dirty(visuals);
}
TryComp<AppearanceComponent>(uid, out var appearance);
_appearance.SetData(uid, SharedTetherGunSystem.TetherVisualsStatus.Key, false, appearance);
}
private void OnGrapplingDeselected(EntityUid uid, GrapplingGunComponent component, HandDeselectedEvent args)
{
SetReeling(uid, component, false, args.User);
}
private void OnGrapplingReel(RequestGrapplingReelMessage msg, EntitySessionEventArgs args)
{
var player = args.SenderSession.AttachedEntity;
if (!TryComp<HandsComponent>(player, out var hands) ||
!TryComp<GrapplingGunComponent>(hands.ActiveHandEntity, out var grappling))
{
return;
}
if (msg.Reeling &&
(!TryComp<CombatModeComponent>(player, out var combatMode) ||
!combatMode.IsInCombatMode))
{
return;
}
SetReeling(hands.ActiveHandEntity.Value, grappling, msg.Reeling, player.Value);
}
private void OnWeightlessMove(ref CanWeightlessMoveEvent ev)
{
if (ev.CanMove || !TryComp<JointRelayTargetComponent>(ev.Uid, out var relayComp))
return;
foreach (var relay in relayComp.Relayed)
{
if (TryComp<JointComponent>(relay, out var jointRelay) && jointRelay.GetJoints.ContainsKey(GrapplingJoint))
{
ev.CanMove = true;
return;
}
}
}
private void OnGunActivate(EntityUid uid, GrapplingGunComponent component, ActivateInWorldEvent args)
{
if (!Timing.IsFirstTimePredicted || _delay.ActiveDelay(uid))
return;
_delay.BeginDelay(uid);
_audio.PlayPredicted(component.CycleSound, uid, args.User);
TryComp<AppearanceComponent>(uid, out var appearance);
_appearance.SetData(uid, SharedTetherGunSystem.TetherVisualsStatus.Key, true, appearance);
SetReeling(uid, component, false, args.User);
if (!Deleted(component.Projectile))
{
if (_netManager.IsServer)
{
QueueDel(component.Projectile.Value);
}
component.Projectile = null;
Dirty(component);
}
}
private void SetReeling(EntityUid uid, GrapplingGunComponent component, bool value, EntityUid? user)
{
if (component.Reeling == value)
return;
if (value)
{
if (Timing.IsFirstTimePredicted)
component.Stream = _audio.PlayPredicted(component.ReelSound, uid, user);
}
else
{
if (Timing.IsFirstTimePredicted)
{
component.Stream?.Stop();
component.Stream = null;
}
}
component.Reeling = value;
Dirty(component);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<GrapplingGunComponent>();
while (query.MoveNext(out var uid, out var grappling))
{
if (!grappling.Reeling)
{
if (Timing.IsFirstTimePredicted)
{
// Just in case.
grappling.Stream?.Stop();
grappling.Stream = null;
}
continue;
}
if (!TryComp<JointComponent>(uid, out var jointComp) ||
!jointComp.GetJoints.TryGetValue(GrapplingJoint, out var joint) ||
joint is not DistanceJoint distance)
{
SetReeling(uid, grappling, false, null);
continue;
}
// TODO: This should be on engine.
distance.MaxLength = MathF.Max(distance.MinLength, distance.MaxLength - ReelRate * frameTime);
distance.Length = MathF.Min(distance.MaxLength, distance.Length);
_physics.WakeBody(joint.BodyAUid);
_physics.WakeBody(joint.BodyBUid);
if (jointComp.Relay != null)
{
_physics.WakeBody(jointComp.Relay.Value);
}
Dirty(jointComp);
if (distance.MaxLength.Equals(distance.MinLength))
{
SetReeling(uid, grappling, false, null);
}
}
}
private void OnGrappleCollide(EntityUid uid, GrapplingProjectileComponent component, ref ProjectileEmbedEvent args)
{
if (!Timing.IsFirstTimePredicted)
return;
var jointComp = EnsureComp<JointComponent>(uid);
var joint = _joints.CreateDistanceJoint(uid, args.Weapon, anchorA: new Vector2(0f, 0.5f), id: GrapplingJoint);
joint.MaxLength = joint.Length + 0.2f;
joint.Stiffness = 1f;
joint.MinLength = 0.35f;
// Setting velocity directly for mob movement fucks this so need to make them aware of it.
// joint.Breakpoint = 4000f;
Dirty(jointComp);
}
[Serializable, NetSerializable]
protected sealed class RequestGrapplingReelMessage : EntityEventArgs
{
public bool Reeling;
public RequestGrapplingReelMessage(bool reeling)
{
Reeling = reeling;
}
}
}

View File

@@ -43,7 +43,7 @@ public abstract partial class SharedTetherGunSystem
{
// Pickup
if (TryTether(uid, args.Target.Value, args.User, component))
TransformSystem.SetCoordinates(component.TetherEntity!.Value, new EntityCoordinates(uid, new Vector2(0.0f, -0.8f)));
TransformSystem.SetCoordinates(component.TetherEntity!.Value, new EntityCoordinates(uid, new Vector2(0f, 0f)));
}
}

View File

@@ -22,6 +22,8 @@ public sealed partial class BallisticAmmoProviderComponent : Component
[ViewVariables(VVAccess.ReadWrite), DataField("capacity")]
public int Capacity = 30;
public int Count => UnspawnedCount + Container.ContainedEntities.Count;
[ViewVariables(VVAccess.ReadWrite), DataField("unspawnedCount")]
[AutoNetworkedField]
public int UnspawnedCount;

View File

@@ -0,0 +1,28 @@
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
namespace Content.Shared.Weapons.Ranged.Components;
// I have tried to make this as generic as possible but "delete joint on cycle / right-click reels in" is very specific behavior.
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class GrapplingGunComponent : Component
{
[DataField("jointId"), AutoNetworkedField]
public string Joint = string.Empty;
[DataField("projectile")] public EntityUid? Projectile;
[ViewVariables(VVAccess.ReadWrite), DataField("reeling"), AutoNetworkedField]
public bool Reeling;
[ViewVariables(VVAccess.ReadWrite), DataField("reelSound"), AutoNetworkedField]
public SoundSpecifier? ReelSound = new SoundPathSpecifier("/Audio/Weapons/reel.ogg")
{
Params = AudioParams.Default.WithLoop(true)
};
[ViewVariables(VVAccess.ReadWrite), DataField("cycleSound"), AutoNetworkedField]
public SoundSpecifier? CycleSound = new SoundPathSpecifier("/Audio/Weapons/Guns/MagIn/kinetic_reload.ogg");
public IPlayingAudioStream? Stream;
}

View File

@@ -0,0 +1,12 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Weapons.Ranged.Components;
/// <summary>
/// Recharges ammo upon the gun being cycled.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class RechargeCycleAmmoComponent : Component
{
}

View File

@@ -0,0 +1,7 @@
namespace Content.Shared.Weapons.Ranged.Events;
/// <summary>
/// Raised directed on a gun when it cycles.
/// </summary>
[ByRefEvent]
public readonly record struct GunCycledEvent;

View File

@@ -0,0 +1,31 @@
using Content.Shared.Interaction;
using Content.Shared.Weapons.Ranged.Components;
namespace Content.Shared.Weapons.Ranged.Systems;
/// <summary>
/// Recharges ammo whenever the gun is cycled.
/// </summary>
public sealed class RechargeCycleAmmoSystem : EntitySystem
{
[Dependency] private readonly SharedGunSystem _gun = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RechargeCycleAmmoComponent, ActivateInWorldEvent>(OnRechargeCycled);
}
private void OnRechargeCycled(EntityUid uid, RechargeCycleAmmoComponent component, ActivateInWorldEvent args)
{
if (!TryComp<BasicEntityAmmoProviderComponent>(uid, out var basic) || args.Handled)
return;
if (basic.Count >= basic.Capacity || basic.Count == null)
return;
_gun.UpdateBasicEntityAmmoCount(uid, basic.Count.Value + 1, basic);
Dirty(basic);
args.Handled = true;
}
}

View File

@@ -162,7 +162,7 @@ public abstract partial class SharedGunSystem
var shots = GetBallisticShots(component);
component.Cycled = true;
Cycle(component, coordinates);
Cycle(uid, component, coordinates);
var text = Loc.GetString(shots == 0 ? "gun-ballistic-cycled-empty" : "gun-ballistic-cycled");
@@ -171,7 +171,7 @@ public abstract partial class SharedGunSystem
UpdateAmmoCount(uid);
}
protected abstract void Cycle(BallisticAmmoProviderComponent component, MapCoordinates coordinates);
protected abstract void Cycle(EntityUid uid, BallisticAmmoProviderComponent component, MapCoordinates coordinates);
private void OnBallisticInit(EntityUid uid, BallisticAmmoProviderComponent component, ComponentInit args)
{

View File

@@ -313,15 +313,16 @@ public abstract partial class SharedGunSystem : EntitySystem
}
// Shoot confirmed - sounds also played here in case it's invalid (e.g. cartridge already spent).
Shoot(gunUid, gun, ev.Ammo, fromCoordinates, toCoordinates.Value, user, throwItems: attemptEv.ThrowItems);
var shotEv = new GunShotEvent(user);
Shoot(gunUid, gun, ev.Ammo, fromCoordinates, toCoordinates.Value, out var userImpulse, user, throwItems: attemptEv.ThrowItems);
var shotEv = new GunShotEvent(user, ev.Ammo);
RaiseLocalEvent(gunUid, ref shotEv);
// Projectiles cause impulses especially important in non gravity environments
if (TryComp<PhysicsComponent>(user, out var userPhysics))
if (userImpulse && TryComp<PhysicsComponent>(user, out var userPhysics))
{
if (_gravity.IsWeightless(user, userPhysics))
CauseImpulse(fromCoordinates, toCoordinates.Value, user, userPhysics);
}
Dirty(gun);
}
@@ -331,11 +332,12 @@ public abstract partial class SharedGunSystem : EntitySystem
EntityUid ammo,
EntityCoordinates fromCoordinates,
EntityCoordinates toCoordinates,
out bool userImpulse,
EntityUid? user = null,
bool throwItems = false)
{
var shootable = EnsureComp<AmmoComponent>(ammo);
Shoot(gunUid, gun, new List<(EntityUid? Entity, IShootable Shootable)>(1) { (ammo, shootable) }, fromCoordinates, toCoordinates, user, throwItems);
Shoot(gunUid, gun, new List<(EntityUid? Entity, IShootable Shootable)>(1) { (ammo, shootable) }, fromCoordinates, toCoordinates, out userImpulse, user, throwItems);
}
public abstract void Shoot(
@@ -344,6 +346,7 @@ public abstract partial class SharedGunSystem : EntitySystem
List<(EntityUid? Entity, IShootable Shootable)> ammo,
EntityCoordinates fromCoordinates,
EntityCoordinates toCoordinates,
out bool userImpulse,
EntityUid? user = null,
bool throwItems = false);
@@ -436,7 +439,7 @@ public record struct AttemptShootEvent(EntityUid User, string? Message, bool Can
/// </summary>
/// <param name="User">The user that fired this gun.</param>
[ByRefEvent]
public record struct GunShotEvent(EntityUid User);
public record struct GunShotEvent(EntityUid User, List<(EntityUid? Uid, IShootable Shootable)> Ammo);
public enum EffectLayers : byte
{