2020-09-13 14:23:52 +02:00
|
|
|
|
using System.Collections.Generic;
|
2021-06-09 22:19:39 +02:00
|
|
|
|
using Content.Server.Interaction;
|
|
|
|
|
|
using Content.Server.MachineLinking.Components;
|
2020-08-29 03:33:42 -07:00
|
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
|
using Robust.Shared.Input;
|
|
|
|
|
|
using Robust.Shared.Input.Binding;
|
|
|
|
|
|
using Robust.Shared.Map;
|
|
|
|
|
|
using Robust.Shared.Network;
|
|
|
|
|
|
using Robust.Shared.Players;
|
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
|
namespace Content.Server.MachineLinking
|
2020-08-29 03:33:42 -07:00
|
|
|
|
{
|
|
|
|
|
|
public class SignalLinkerSystem : EntitySystem
|
|
|
|
|
|
{
|
2021-03-16 15:50:20 +01:00
|
|
|
|
private readonly Dictionary<NetUserId, SignalTransmitterComponent?> _transmitters = new();
|
2020-08-29 03:33:42 -07:00
|
|
|
|
|
2020-11-05 03:57:49 -08:00
|
|
|
|
public bool SignalLinkerKeybind(NetUserId id, bool? enable)
|
2020-08-29 03:33:42 -07:00
|
|
|
|
{
|
2021-02-16 20:14:21 +01:00
|
|
|
|
enable ??= !_transmitters.ContainsKey(id);
|
2020-08-29 03:33:42 -07:00
|
|
|
|
|
2021-02-16 20:14:21 +01:00
|
|
|
|
if (enable.Value)
|
2020-08-29 03:33:42 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (_transmitters.ContainsKey(id))
|
|
|
|
|
|
{
|
2020-11-05 03:57:49 -08:00
|
|
|
|
return true;
|
2020-08-29 03:33:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (_transmitters.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
CommandBinds.Builder
|
2021-03-16 15:50:20 +01:00
|
|
|
|
.BindBefore(EngineKeyFunctions.Use,
|
|
|
|
|
|
new PointerInputCmdHandler(HandleUse),
|
|
|
|
|
|
typeof(InteractionSystem))
|
2020-08-29 03:33:42 -07:00
|
|
|
|
.Register<SignalLinkerSystem>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_transmitters.Add(id, null);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2021-02-16 20:14:21 +01:00
|
|
|
|
else
|
2020-08-29 03:33:42 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (!_transmitters.ContainsKey(id))
|
|
|
|
|
|
{
|
2020-11-05 03:57:49 -08:00
|
|
|
|
return false;
|
2020-08-29 03:33:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_transmitters.Remove(id);
|
|
|
|
|
|
if (_transmitters.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
CommandBinds.Unregister<SignalLinkerSystem>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-02-16 20:14:21 +01:00
|
|
|
|
|
|
|
|
|
|
return enable.Value;
|
2020-08-29 03:33:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
|
private bool HandleUse(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
|
2020-08-29 03:33:42 -07:00
|
|
|
|
{
|
2021-03-16 15:50:20 +01:00
|
|
|
|
if (session?.AttachedEntity == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-29 14:26:00 +02:00
|
|
|
|
if (!_transmitters.TryGetValue(session.UserId, out var signalTransmitter))
|
2020-08-29 03:33:42 -07:00
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!EntityManager.TryGetEntity(uid, out var entity))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (entity.TryGetComponent<SignalReceiverComponent>(out var signalReceiver))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (signalReceiver.Interact(session.AttachedEntity, signalTransmitter))
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (entity.TryGetComponent<SignalTransmitterComponent>(out var transmitter))
|
|
|
|
|
|
{
|
2020-09-29 14:26:00 +02:00
|
|
|
|
_transmitters[session.UserId] = transmitter.GetSignal(session.AttachedEntity);
|
2020-08-29 03:33:42 -07:00
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|