Add tether gun (#16430)

This commit is contained in:
metalgearsloth
2023-05-18 11:36:06 +10:00
committed by GitHub
parent b386764639
commit 775258be52
28 changed files with 697 additions and 413 deletions

View File

@@ -157,6 +157,18 @@ public sealed partial class PowerCellSystem : SharedPowerCellSystem
return false;
}
/// <summary>
/// Whether the power cell has any power at all for the draw rate.
/// </summary>
public bool HasDrawCharge(EntityUid uid, PowerCellDrawComponent? battery = null,
PowerCellSlotComponent? cell = null, EntityUid? user = null)
{
if (!Resolve(uid, ref battery, ref cell, false))
return true;
return HasCharge(uid, float.MinValue, cell, user);
}
#endregion
public void SetPowerCellDrawEnabled(EntityUid uid, bool enabled, PowerCellDrawComponent? component = null)

View File

@@ -0,0 +1,46 @@
using Content.Server.PowerCell;
using Content.Shared.PowerCell.Components;
using Content.Shared.Weapons.Misc;
using Robust.Shared.Physics.Components;
namespace Content.Server.Weapons.Misc;
public sealed class TetherGunSystem : SharedTetherGunSystem
{
[Dependency] private readonly PowerCellSystem _cell = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<TetherGunComponent, PowerCellSlotEmptyEvent>(OnGunEmpty);
}
private void OnGunEmpty(EntityUid uid, TetherGunComponent component, ref PowerCellSlotEmptyEvent args)
{
StopTether(uid, component);
}
protected override bool CanTether(EntityUid uid, TetherGunComponent component, EntityUid target, EntityUid? user)
{
if (!base.CanTether(uid, component, target, user))
return false;
if (!_cell.HasDrawCharge(uid, user: user))
return false;
return true;
}
protected override void StartTether(EntityUid gunUid, TetherGunComponent component, EntityUid target, EntityUid? user,
PhysicsComponent? targetPhysics = null, TransformComponent? targetXform = null)
{
base.StartTether(gunUid, component, target, user, targetPhysics, targetXform);
_cell.SetPowerCellDrawEnabled(gunUid, true);
}
protected override void StopTether(EntityUid gunUid, TetherGunComponent component, bool transfer = false)
{
base.StopTether(gunUid, component, transfer);
_cell.SetPowerCellDrawEnabled(gunUid, false);
}
}

View File

@@ -1,199 +0,0 @@
using Content.Server.Ghost.Components;
using Content.Shared.Administration;
using Content.Shared.Weapons.Ranged.Systems;
using Robust.Server.Console;
using Robust.Server.Player;
using Robust.Shared.Containers;
using Robust.Shared.Map;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Dynamics.Joints;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Players;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Server.Weapons.Ranged.Systems;
public sealed class TetherGunSystem : SharedTetherGunSystem
{
[Dependency] private readonly IConGroupController _admin = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
[Dependency] private readonly SharedJointSystem _joints = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
private readonly Dictionary<ICommonSession, (EntityUid Entity, EntityUid Tether, Joint Joint)> _tethered = new();
private readonly HashSet<ICommonSession> _draggers = new();
private const string JointId = "tether-joint";
public override void Initialize()
{
base.Initialize();
SubscribeNetworkEvent<StartTetherEvent>(OnStartTether);
SubscribeNetworkEvent<StopTetherEvent>(OnStopTether);
SubscribeNetworkEvent<TetherMoveEvent>(OnMoveTether);
_playerManager.PlayerStatusChanged += OnStatusChange;
}
private void OnStatusChange(object? sender, SessionStatusEventArgs e)
{
StopTether(e.Session);
}
public override void Shutdown()
{
base.Shutdown();
_playerManager.PlayerStatusChanged -= OnStatusChange;
}
public void Toggle(ICommonSession? session)
{
if (session == null)
return;
if (_draggers.Add(session))
{
RaiseNetworkEvent(new TetherGunToggleMessage()
{
Enabled = true,
}, session.ConnectedClient);
return;
}
_draggers.Remove(session);
RaiseNetworkEvent(new TetherGunToggleMessage()
{
Enabled = false,
}, session.ConnectedClient);
}
public bool IsEnabled(ICommonSession? session)
{
if (session == null)
return false;
return _draggers.Contains(session);
}
private void OnStartTether(StartTetherEvent msg, EntitySessionEventArgs args)
{
if (args.SenderSession is not IPlayerSession playerSession ||
!_admin.CanCommand(playerSession, CommandName) ||
!Exists(msg.Entity) ||
Deleted(msg.Entity) ||
msg.Coordinates == MapCoordinates.Nullspace ||
_tethered.ContainsKey(args.SenderSession)) return;
var tether = Spawn("TetherEntity", msg.Coordinates);
if (!TryComp<PhysicsComponent>(tether, out var bodyA) ||
!TryComp<PhysicsComponent>(msg.Entity, out var bodyB))
{
Del(tether);
return;
}
EnsureComp<AdminFrozenComponent>(msg.Entity);
if (TryComp<TransformComponent>(msg.Entity, out var xform))
{
xform.Anchored = false;
}
if (_container.IsEntityInContainer(msg.Entity))
{
xform?.AttachToGridOrMap();
}
if (TryComp<PhysicsComponent>(msg.Entity, out var body))
{
_physics.SetBodyStatus(body, BodyStatus.InAir);
}
_physics.WakeBody(tether, body: bodyA);
_physics.WakeBody(msg.Entity, body: bodyB);
var joint = _joints.CreateMouseJoint(tether, msg.Entity, id: JointId);
SharedJointSystem.LinearStiffness(5f, 0.7f, bodyA.Mass, bodyB.Mass, out var stiffness, out var damping);
joint.Stiffness = stiffness;
joint.Damping = damping;
joint.MaxForce = 10000f * bodyB.Mass;
_tethered.Add(playerSession, (msg.Entity, tether, joint));
RaiseNetworkEvent(new PredictTetherEvent()
{
Entity = msg.Entity
}, args.SenderSession.ConnectedClient);
}
private void OnStopTether(StopTetherEvent msg, EntitySessionEventArgs args)
{
StopTether(args.SenderSession);
}
private void StopTether(ICommonSession session)
{
if (!_tethered.TryGetValue(session, out var weh))
return;
RemComp<AdminFrozenComponent>(weh.Entity);
if (TryComp<PhysicsComponent>(weh.Entity, out var body) &&
!HasComp<GhostComponent>(weh.Entity))
{
Timer.Spawn(1000, () =>
{
if (Deleted(weh.Entity)) return;
_physics.SetBodyStatus(body, BodyStatus.OnGround);
});
}
_joints.RemoveJoint(weh.Joint);
Del(weh.Tether);
_tethered.Remove(session);
}
private void OnMoveTether(TetherMoveEvent msg, EntitySessionEventArgs args)
{
if (!_tethered.TryGetValue(args.SenderSession, out var tether) ||
!TryComp<TransformComponent>(tether.Tether, out var xform) ||
xform.MapID != msg.Coordinates.MapId) return;
xform.WorldPosition = msg.Coordinates.Position;
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var toRemove = new RemQueue<ICommonSession>();
var bodyQuery = GetEntityQuery<PhysicsComponent>();
foreach (var (session, entity) in _tethered)
{
if (Deleted(entity.Entity) ||
Deleted(entity.Tether) ||
!entity.Joint.Enabled)
{
toRemove.Add(session);
continue;
}
// Force it awake, always
if (bodyQuery.TryGetComponent(entity.Entity, out var body))
{
_physics.WakeBody(entity.Entity, body: body);
}
}
foreach (var session in toRemove)
{
StopTether(session);
}
}
}

View File

@@ -1,25 +0,0 @@
using Content.Server.Administration;
using Content.Server.Weapons.Ranged.Systems;
using Content.Shared.Administration;
using Content.Shared.Weapons.Ranged.Systems;
using Robust.Shared.Console;
namespace Content.Server.Weapons;
[AdminCommand(AdminFlags.Fun)]
public sealed class TetherGunCommand : IConsoleCommand
{
public string Command => SharedTetherGunSystem.CommandName;
public string Description => "Allows you to drag mobs around with your mouse.";
public string Help => $"{Command}";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
var system = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<TetherGunSystem>();
system.Toggle(shell.Player);
if (system.IsEnabled(shell.Player))
shell.WriteLine("Tether gun toggled on");
else
shell.WriteLine("Tether gun toggled off");
}
}