Files
crystall-punk-14/Content.Client/Weapons/Ranged/Systems/TetherGunSystem.cs

143 lines
4.0 KiB
C#
Raw Normal View History

2022-12-19 06:41:04 +11:00
using Content.Client.Gameplay;
using Content.Shared.Weapons.Ranged.Systems;
2022-05-13 12:24:34 +10:00
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Input;
2023-05-13 02:02:50 +12:00
using Robust.Client.Physics;
2022-12-19 06:41:04 +11:00
using Robust.Client.State;
2022-05-13 12:24:34 +10:00
using Robust.Shared.Input;
using Robust.Shared.Map;
using Robust.Shared.Physics.Components;
2022-05-13 12:24:34 +10:00
using Robust.Shared.Timing;
namespace Content.Client.Weapons.Ranged.Systems;
2022-05-13 12:24:34 +10:00
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!;
2023-05-13 02:02:50 +12:00
[Dependency] private readonly PhysicsSystem _physics = default!;
2022-05-13 12:24:34 +10:00
public bool Enabled { get; set; }
/// <summary>
/// The entity being dragged around.
/// </summary>
private EntityUid? _dragging;
2022-05-13 18:59:12 +10:00
private EntityUid? _tether;
2022-05-13 12:24:34 +10:00
private MapCoordinates? _lastMousePosition;
2022-05-13 18:59:12 +10:00
public override void Initialize()
{
base.Initialize();
SubscribeNetworkEvent<PredictTetherEvent>(OnPredictTether);
2022-09-01 13:11:45 +10:00
SubscribeNetworkEvent<TetherGunToggleMessage>(OnTetherGun);
2023-05-13 02:02:50 +12:00
SubscribeLocalEvent<UpdateIsPredictedEvent>(OnUpdatePrediction);
}
private void OnUpdatePrediction(ref UpdateIsPredictedEvent ev)
{
if (ev.Uid == _dragging || ev.Uid == _tether)
ev.IsPredicted = true;
2022-09-01 13:11:45 +10:00
}
private void OnTetherGun(TetherGunToggleMessage ev)
{
Enabled = ev.Enabled;
2022-05-13 18:59:12 +10:00
}
private void OnPredictTether(PredictTetherEvent ev)
{
2023-05-13 02:02:50 +12:00
if (_dragging != ev.Entity || _tether == ev.Entity)
return;
2022-05-13 18:59:12 +10:00
2023-05-13 02:02:50 +12:00
var oldTether = _tether;
2022-05-13 18:59:12 +10:00
_tether = ev.Entity;
2023-05-13 02:02:50 +12:00
_physics.UpdateIsPredicted(oldTether);
_physics.UpdateIsPredicted(_tether);
2022-05-13 18:59:12 +10:00
}
2022-05-13 12:24:34 +10:00
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)
{
2022-12-19 06:41:04 +11:00
var gameState = IoCManager.Resolve<IStateManager>().CurrentState;
2022-05-13 12:24:34 +10:00
2022-12-19 06:41:04 +11:00
if (gameState is GameplayState game)
2022-05-13 12:24:34 +10:00
{
var uid = game.GetClickedEntity(mousePos);
2022-05-13 12:24:34 +10:00
2022-12-25 08:24:43 +11:00
if (uid != null)
StartDragging(uid.Value, mousePos);
2022-05-13 12:24:34 +10:00
}
2022-12-19 06:41:04 +11:00
if (_dragging == null)
return;
2022-05-13 12:24:34 +10:00
}
if (!TryComp<TransformComponent>(_dragging!.Value, out var xform) ||
2022-05-13 18:59:12 +10:00
_lastMousePosition!.Value.MapId != xform.MapID ||
!TryComp<PhysicsComponent>(_dragging, out var body))
2022-05-13 12:24:34 +10:00
{
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;
2023-05-13 02:02:50 +12:00
var oldDrag = _dragging;
var oldTether = _tether;
2022-05-13 12:24:34 +10:00
RaiseNetworkEvent(new StopTetherEvent());
_dragging = null;
_lastMousePosition = null;
2022-05-13 18:59:12 +10:00
_tether = null;
2023-05-13 02:02:50 +12:00
_physics.UpdateIsPredicted(oldDrag);
_physics.UpdateIsPredicted(oldTether);
2022-05-13 12:24:34 +10:00
}
private void StartDragging(EntityUid uid, MapCoordinates coordinates)
{
_dragging = uid;
_lastMousePosition = coordinates;
RaiseNetworkEvent(new StartTetherEvent()
{
Entity = _dragging!.Value,
Coordinates = coordinates,
});
2023-05-13 02:02:50 +12:00
_physics.UpdateIsPredicted(uid);
2022-05-13 12:24:34 +10:00
}
}