Add tether gun (#16430)
This commit is contained in:
52
Content.Client/Weapons/Misc/TetherGunOverlay.cs
Normal file
52
Content.Client/Weapons/Misc/TetherGunOverlay.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Content.Shared.Weapons.Misc;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Shared.Enums;
|
||||
|
||||
namespace Content.Client.Weapons.Misc;
|
||||
|
||||
public sealed class TetherGunOverlay : Overlay
|
||||
{
|
||||
public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowFOV;
|
||||
|
||||
private IEntityManager _entManager;
|
||||
|
||||
public TetherGunOverlay(IEntityManager entManager)
|
||||
{
|
||||
_entManager = entManager;
|
||||
}
|
||||
|
||||
protected override void Draw(in OverlayDrawArgs args)
|
||||
{
|
||||
var query = _entManager.EntityQueryEnumerator<TetheredComponent>();
|
||||
var xformQuery = _entManager.GetEntityQuery<TransformComponent>();
|
||||
var worldHandle = args.WorldHandle;
|
||||
var xformSystem = _entManager.System<SharedTransformSystem>();
|
||||
|
||||
while (query.MoveNext(out var uid, out var tethered))
|
||||
{
|
||||
var gun = tethered.Tetherer;
|
||||
|
||||
if (!xformQuery.TryGetComponent(gun, out var gunXform) ||
|
||||
!xformQuery.TryGetComponent(uid, out var xform))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (xform.MapID != gunXform.MapID)
|
||||
continue;
|
||||
|
||||
var worldPos = xformSystem.GetWorldPosition(xform, xformQuery);
|
||||
var gunWorldPos = xformSystem.GetWorldPosition(gunXform, xformQuery);
|
||||
var diff = worldPos - gunWorldPos;
|
||||
var angle = diff.ToWorldAngle();
|
||||
var length = diff.Length / 2f;
|
||||
var midPoint = gunWorldPos + diff / 2;
|
||||
const float Width = 0.05f;
|
||||
|
||||
var box = new Box2(-Width, -length, Width, length);
|
||||
var rotated = new Box2Rotated(box.Translated(midPoint), angle, midPoint);
|
||||
|
||||
worldHandle.DrawRect(rotated, Color.Orange.WithAlpha(0.3f));
|
||||
}
|
||||
}
|
||||
}
|
||||
103
Content.Client/Weapons/Misc/TetherGunSystem.cs
Normal file
103
Content.Client/Weapons/Misc/TetherGunSystem.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using Content.Shared.Weapons.Misc;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.Player;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Weapons.Misc;
|
||||
|
||||
public sealed class TetherGunSystem : SharedTetherGunSystem
|
||||
{
|
||||
[Dependency] private readonly IEyeManager _eyeManager = default!;
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
[Dependency] private readonly IInputManager _input = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly IOverlayManager _overlay = default!;
|
||||
[Dependency] private readonly IPlayerManager _player = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<TetheredComponent, ComponentStartup>(OnTetheredStartup);
|
||||
SubscribeLocalEvent<TetheredComponent, ComponentShutdown>(OnTetheredShutdown);
|
||||
_overlay.AddOverlay(new TetherGunOverlay(EntityManager));
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
_overlay.RemoveOverlay<TetherGunOverlay>();
|
||||
}
|
||||
|
||||
protected override bool CanTether(EntityUid uid, TetherGunComponent component, EntityUid target, EntityUid? user)
|
||||
{
|
||||
// Need powercells predicted sadly :<
|
||||
return false;
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
if (!_timing.IsFirstTimePredicted)
|
||||
return;
|
||||
|
||||
var player = _player.LocalPlayer?.ControlledEntity;
|
||||
|
||||
if (player == null ||
|
||||
!TryGetTetherGun(player.Value, out var gunUid, out var gun) ||
|
||||
gun.TetherEntity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var mousePos = _input.MouseScreenPosition;
|
||||
var mouseWorldPos = _eyeManager.ScreenToMap(mousePos);
|
||||
|
||||
if (mouseWorldPos.MapId == MapId.Nullspace)
|
||||
return;
|
||||
|
||||
EntityCoordinates coords;
|
||||
|
||||
if (_mapManager.TryFindGridAt(mouseWorldPos, out var grid))
|
||||
{
|
||||
coords = EntityCoordinates.FromMap(grid.Owner, mouseWorldPos, TransformSystem);
|
||||
}
|
||||
else
|
||||
{
|
||||
coords = EntityCoordinates.FromMap(_mapManager.GetMapEntityId(mouseWorldPos.MapId), mouseWorldPos, TransformSystem);
|
||||
}
|
||||
|
||||
const float BufferDistance = 0.1f;
|
||||
|
||||
if (TryComp<TransformComponent>(gun.TetherEntity, out var tetherXform) &&
|
||||
tetherXform.Coordinates.TryDistance(EntityManager, TransformSystem, coords, out var distance) &&
|
||||
distance < BufferDistance)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RaisePredictiveEvent(new RequestTetherMoveEvent()
|
||||
{
|
||||
Coordinates = coords
|
||||
});
|
||||
}
|
||||
|
||||
private void OnTetheredStartup(EntityUid uid, TetheredComponent component, ComponentStartup args)
|
||||
{
|
||||
if (!TryComp<SpriteComponent>(uid, out var sprite))
|
||||
return;
|
||||
|
||||
sprite.Color = Color.Orange;
|
||||
}
|
||||
|
||||
private void OnTetheredShutdown(EntityUid uid, TetheredComponent component, ComponentShutdown args)
|
||||
{
|
||||
if (!TryComp<SpriteComponent>(uid, out var sprite))
|
||||
return;
|
||||
|
||||
sprite.Color = Color.White;
|
||||
}
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
using Content.Client.Gameplay;
|
||||
using Content.Shared.Weapons.Ranged.Systems;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Input;
|
||||
using Robust.Client.Physics;
|
||||
using Robust.Client.State;
|
||||
using Robust.Shared.Input;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Physics.Components;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Client.Weapons.Ranged.Systems;
|
||||
|
||||
public sealed class TetherGunSystem : SharedTetherGunSystem
|
||||
{
|
||||
[Dependency] private readonly IEyeManager _eyeManager = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly IInputManager _inputManager = default!;
|
||||
[Dependency] private readonly InputSystem _inputSystem = default!;
|
||||
[Dependency] private readonly PhysicsSystem _physics = default!;
|
||||
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The entity being dragged around.
|
||||
/// </summary>
|
||||
private EntityUid? _dragging;
|
||||
private EntityUid? _tether;
|
||||
|
||||
private MapCoordinates? _lastMousePosition;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeNetworkEvent<PredictTetherEvent>(OnPredictTether);
|
||||
SubscribeNetworkEvent<TetherGunToggleMessage>(OnTetherGun);
|
||||
SubscribeLocalEvent<UpdateIsPredictedEvent>(OnUpdatePrediction);
|
||||
}
|
||||
|
||||
private void OnUpdatePrediction(ref UpdateIsPredictedEvent ev)
|
||||
{
|
||||
if (ev.Uid == _dragging || ev.Uid == _tether)
|
||||
ev.IsPredicted = true;
|
||||
}
|
||||
|
||||
private void OnTetherGun(TetherGunToggleMessage ev)
|
||||
{
|
||||
Enabled = ev.Enabled;
|
||||
}
|
||||
|
||||
private void OnPredictTether(PredictTetherEvent ev)
|
||||
{
|
||||
if (_dragging != ev.Entity || _tether == ev.Entity)
|
||||
return;
|
||||
|
||||
var oldTether = _tether;
|
||||
_tether = ev.Entity;
|
||||
_physics.UpdateIsPredicted(oldTether);
|
||||
_physics.UpdateIsPredicted(_tether);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
if (!Enabled || !_gameTiming.IsFirstTimePredicted) return;
|
||||
|
||||
var state = _inputSystem.CmdStates.GetState(EngineKeyFunctions.Use);
|
||||
|
||||
if (state != BoundKeyState.Down)
|
||||
{
|
||||
StopDragging();
|
||||
return;
|
||||
}
|
||||
|
||||
var mouseScreenPos = _inputManager.MouseScreenPosition;
|
||||
var mousePos = _eyeManager.ScreenToMap(mouseScreenPos);
|
||||
|
||||
if (_dragging == null)
|
||||
{
|
||||
var gameState = IoCManager.Resolve<IStateManager>().CurrentState;
|
||||
|
||||
if (gameState is GameplayState game)
|
||||
{
|
||||
var uid = game.GetClickedEntity(mousePos);
|
||||
|
||||
if (uid != null)
|
||||
StartDragging(uid.Value, mousePos);
|
||||
}
|
||||
|
||||
if (_dragging == null)
|
||||
return;
|
||||
}
|
||||
|
||||
if (!TryComp<TransformComponent>(_dragging!.Value, out var xform) ||
|
||||
_lastMousePosition!.Value.MapId != xform.MapID ||
|
||||
!TryComp<PhysicsComponent>(_dragging, out var body))
|
||||
{
|
||||
StopDragging();
|
||||
return;
|
||||
}
|
||||
|
||||
if (_lastMousePosition.Value.Position.EqualsApprox(mousePos.Position)) return;
|
||||
|
||||
_lastMousePosition = mousePos;
|
||||
|
||||
RaiseNetworkEvent(new TetherMoveEvent()
|
||||
{
|
||||
Coordinates = _lastMousePosition!.Value,
|
||||
});
|
||||
}
|
||||
|
||||
private void StopDragging()
|
||||
{
|
||||
if (_dragging == null) return;
|
||||
|
||||
var oldDrag = _dragging;
|
||||
var oldTether = _tether;
|
||||
RaiseNetworkEvent(new StopTetherEvent());
|
||||
_dragging = null;
|
||||
_lastMousePosition = null;
|
||||
_tether = null;
|
||||
|
||||
_physics.UpdateIsPredicted(oldDrag);
|
||||
_physics.UpdateIsPredicted(oldTether);
|
||||
}
|
||||
|
||||
private void StartDragging(EntityUid uid, MapCoordinates coordinates)
|
||||
{
|
||||
_dragging = uid;
|
||||
_lastMousePosition = coordinates;
|
||||
RaiseNetworkEvent(new StartTetherEvent()
|
||||
{
|
||||
Entity = _dragging!.Value,
|
||||
Coordinates = coordinates,
|
||||
});
|
||||
|
||||
_physics.UpdateIsPredicted(uid);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user