machine linking refactor to ecs (#4323)
* started work * some more work, ui working (somewhat) * stuff * reorganization * some more reorg * conveyors * conveyors working * finalized (dis)connection added linkattempt added feedback text work on conveyors * removed command add rangecheck * fixed inrange check * handling * ui no longer kanser, ship it * adresses reviews * reformats file * reformats file Co-authored-by: Paul <ritter.paul1+git@googlemail.com>
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
namespace Content.Server.MachineLinking.Components
|
||||
{
|
||||
public interface ISignalReceiver<in T>
|
||||
{
|
||||
void TriggerSignal(T signal);
|
||||
}
|
||||
}
|
||||
@@ -1,45 +1,10 @@
|
||||
using Content.Server.MachineLinking.Signals;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
|
||||
namespace Content.Server.MachineLinking.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class SignalButtonComponent : Component, IActivate, IInteractHand
|
||||
public class SignalButtonComponent : Component
|
||||
{
|
||||
public override string Name => "SignalButton";
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
TransmitSignal(eventArgs.User);
|
||||
}
|
||||
|
||||
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
|
||||
{
|
||||
TransmitSignal(eventArgs.User);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void TransmitSignal(IEntity user)
|
||||
{
|
||||
if (!Owner.TryGetComponent<SignalTransmitterComponent>(out var transmitter))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (transmitter.TransmitSignal(new ToggleSignal()))
|
||||
{
|
||||
// Since the button doesn't have an animation, I'm going to use a popup message
|
||||
Owner.PopupMessage(user, Loc.GetString("signal-button-component-transmit-signal-success-message"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Owner.PopupMessage(user, Loc.GetString("signal-button-component-transmit-signal-no-receivers-message"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,13 +9,6 @@ namespace Content.Server.MachineLinking.Components
|
||||
public override string Name => "SignalLinker";
|
||||
|
||||
[ViewVariables]
|
||||
public SignalTransmitterComponent? Link { get; set; }
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
Link = null;
|
||||
}
|
||||
public (SignalTransmitterComponent transmitter, string port)? Port;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,125 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Tools.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Content.Shared.Tool;
|
||||
using Content.Server.MachineLinking.Models;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.MachineLinking.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class SignalReceiverComponent : Component, IInteractUsing
|
||||
public class SignalReceiverComponent : Component
|
||||
{
|
||||
public override string Name => "SignalReceiver";
|
||||
|
||||
private readonly List<SignalTransmitterComponent> _transmitters = new();
|
||||
[DataField("inputs")]
|
||||
private List<SignalPort> _inputs = new();
|
||||
|
||||
[DataField("maxTransmitters")]
|
||||
private int? _maxTransmitters = default;
|
||||
|
||||
public void DistributeSignal<T>(T state)
|
||||
{
|
||||
foreach (var comp in Owner.GetAllComponents<ISignalReceiver<T>>())
|
||||
{
|
||||
comp.TriggerSignal(state);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Subscribe(SignalTransmitterComponent transmitter)
|
||||
{
|
||||
if (_transmitters.Contains(transmitter))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_transmitters.Count >= _maxTransmitters) return false;
|
||||
|
||||
transmitter.Subscribe(this);
|
||||
_transmitters.Add(transmitter);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Unsubscribe(SignalTransmitterComponent transmitter)
|
||||
{
|
||||
transmitter.Unsubscribe(this);
|
||||
_transmitters.Remove(transmitter);
|
||||
}
|
||||
|
||||
public void UnsubscribeAll()
|
||||
{
|
||||
for (var i = _transmitters.Count-1; i >= 0; i--)
|
||||
{
|
||||
var transmitter = _transmitters[i];
|
||||
if (transmitter.Deleted)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
transmitter.Unsubscribe(this);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subscribes/Unsubscribes a transmitter to this component. Returns whether it was successful.
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="transmitter"></param>
|
||||
/// <returns></returns>
|
||||
public bool Interact(IEntity user, SignalTransmitterComponent? transmitter)
|
||||
{
|
||||
if (transmitter == null)
|
||||
{
|
||||
user.PopupMessage(Loc.GetString("signal-receiver-component-interact-no-transmitter-message"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_transmitters.Contains(transmitter))
|
||||
{
|
||||
Unsubscribe(transmitter);
|
||||
Owner.PopupMessage(user, Loc.GetString("signal-receiver-component-interact-unlinked"));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (transmitter.Range > 0 && !Owner.Transform.Coordinates.InRange(Owner.EntityManager, transmitter.Owner.Transform.Coordinates, transmitter.Range))
|
||||
{
|
||||
Owner.PopupMessage(user, Loc.GetString("signal-receiver-component-interact-out-of-range"));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!Subscribe(transmitter))
|
||||
{
|
||||
Owner.PopupMessage(user, Loc.GetString("signal-receiver-component-interact-max-transmitters-limit"));
|
||||
return false;
|
||||
}
|
||||
Owner.PopupMessage(user, Loc.GetString("signal-receiver-component-interact-success"));
|
||||
return true;
|
||||
}
|
||||
|
||||
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.Using.TryGetComponent<ToolComponent>(out var tool))
|
||||
return false;
|
||||
|
||||
if (tool.HasQuality(ToolQuality.Multitool)
|
||||
&& eventArgs.Using.TryGetComponent<SignalLinkerComponent>(out var linker))
|
||||
{
|
||||
return Interact(eventArgs.User, linker.Link);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
|
||||
UnsubscribeAll();
|
||||
|
||||
_transmitters.Clear();
|
||||
}
|
||||
[ViewVariables]
|
||||
public IReadOnlyList<SignalPort> Inputs => _inputs;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,86 +1,12 @@
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Events;
|
||||
using Content.Shared.MachineLinking;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.MachineLinking.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class SignalSwitchComponent : Component, IInteractHand, IActivate
|
||||
public class SignalSwitchComponent : Component
|
||||
{
|
||||
public override string Name => "SignalSwitch";
|
||||
|
||||
[DataField("on")]
|
||||
private bool _on;
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
UpdateSprite();
|
||||
}
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
TransmitSignal(eventArgs.User);
|
||||
}
|
||||
|
||||
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
|
||||
{
|
||||
TransmitSignal(eventArgs.User);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void TransmitSignal(IEntity user)
|
||||
{
|
||||
_on = !_on;
|
||||
|
||||
UpdateSprite();
|
||||
|
||||
if (!Owner.TryGetComponent<SignalTransmitterComponent>(out var transmitter))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!transmitter.TransmitSignal(_on))
|
||||
{
|
||||
Owner.PopupMessage(user, Loc.GetString("signal-switch-component-transmit-no-receivers-connected"));
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateSprite()
|
||||
{
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(SignalSwitchVisuals.On, _on);
|
||||
}
|
||||
}
|
||||
|
||||
[Verb]
|
||||
private sealed class ToggleSwitchVerb : Verb<SignalSwitchComponent>
|
||||
{
|
||||
protected override void Activate(IEntity user, SignalSwitchComponent component)
|
||||
{
|
||||
component.TransmitSignal(user);
|
||||
}
|
||||
|
||||
protected override void GetData(IEntity user, SignalSwitchComponent component, VerbData data)
|
||||
{
|
||||
if (!EntitySystem.Get<ActionBlockerSystem>().CanInteract(user))
|
||||
{
|
||||
data.Visibility = VerbVisibility.Invisible;
|
||||
return;
|
||||
}
|
||||
|
||||
data.Text = Loc.GetString("toggle-switch-verb-get-data-text");
|
||||
data.Visibility = VerbVisibility.Visible;
|
||||
}
|
||||
}
|
||||
public bool State;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,159 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Tools.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Content.Shared.Tool;
|
||||
using Content.Server.MachineLinking.Models;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.MachineLinking.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class SignalTransmitterComponent : Component, IInteractUsing, ISerializationHooks
|
||||
public class SignalTransmitterComponent : Component
|
||||
{
|
||||
public override string Name => "SignalTransmitter";
|
||||
|
||||
private List<SignalReceiverComponent>? _unresolvedReceivers = new();
|
||||
private List<SignalReceiverComponent> _receivers = new();
|
||||
[DataField("outputs")]
|
||||
private List<SignalPort> _outputs = new();
|
||||
|
||||
/// <summary>
|
||||
/// 0 is unlimited range
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("range")]
|
||||
public float Range { get; private set; } = 10;
|
||||
|
||||
[DataField("signalReceivers")] private List<EntityUid> _receiverIds = new();
|
||||
|
||||
void ISerializationHooks.BeforeSerialization()
|
||||
{
|
||||
var entityList = new List<EntityUid>();
|
||||
|
||||
foreach (var receiver in _receivers)
|
||||
{
|
||||
if (receiver.Deleted)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
entityList.Add(receiver.Owner.Uid);
|
||||
}
|
||||
|
||||
_receiverIds = entityList;
|
||||
}
|
||||
|
||||
void ISerializationHooks.AfterDeserialization()
|
||||
{
|
||||
_unresolvedReceivers = new List<SignalReceiverComponent>();
|
||||
|
||||
foreach (var id in _receiverIds)
|
||||
{
|
||||
if (!Owner.EntityManager.TryGetEntity(id, out var entity) ||
|
||||
!entity.TryGetComponent<SignalReceiverComponent>(out var receiver))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_unresolvedReceivers.Add(receiver);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_receivers = new List<SignalReceiverComponent>();
|
||||
|
||||
if (_unresolvedReceivers != null)
|
||||
{
|
||||
foreach (var receiver in _unresolvedReceivers)
|
||||
{
|
||||
receiver.Subscribe(this);
|
||||
}
|
||||
|
||||
_unresolvedReceivers = null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool TransmitSignal<T>(T signal)
|
||||
{
|
||||
if (_receivers.Count == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var receiver in _receivers)
|
||||
{
|
||||
if (Range > 0 && !Owner.Transform.Coordinates.InRange(Owner.EntityManager, receiver.Owner.Transform.Coordinates, Range))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
receiver.DistributeSignal(signal);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Subscribe(SignalReceiverComponent receiver)
|
||||
{
|
||||
if (_receivers.Contains(receiver))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_receivers.Add(receiver);
|
||||
}
|
||||
|
||||
public void Unsubscribe(SignalReceiverComponent receiver)
|
||||
{
|
||||
_receivers.Remove(receiver);
|
||||
}
|
||||
|
||||
public SignalTransmitterComponent GetSignal(IEntity? user)
|
||||
{
|
||||
if (user != null)
|
||||
{
|
||||
Owner.PopupMessage(user, Loc.GetString("signal-transmitter-component-get-signal-success"));
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.Using.TryGetComponent<ToolComponent>(out var tool))
|
||||
return false;
|
||||
|
||||
if (tool.HasQuality(ToolQuality.Multitool)
|
||||
&& eventArgs.Using.TryGetComponent<SignalLinkerComponent>(out var linker))
|
||||
{
|
||||
linker.Link = GetSignal(eventArgs.User);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
|
||||
for (var i = _receivers.Count-1; i >= 0; i++)
|
||||
{
|
||||
var receiver = _receivers[i];
|
||||
if (receiver.Deleted)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
receiver.Unsubscribe(this);
|
||||
}
|
||||
|
||||
_receivers.Clear();
|
||||
}
|
||||
public IReadOnlyList<SignalPort> Outputs => _outputs;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.MachineLinking;
|
||||
using Content.Shared.Notification;
|
||||
using Content.Shared.Notification.Managers;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Localization;
|
||||
|
||||
namespace Content.Server.MachineLinking.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class SignalTwoWayLeverComponent : SignalTransmitterComponent, IInteractHand, IActivate
|
||||
{
|
||||
public override string Name => "TwoWayLever";
|
||||
|
||||
private TwoWayLeverSignal _state = TwoWayLeverSignal.Middle;
|
||||
|
||||
private bool _nextForward = true;
|
||||
|
||||
public TwoWayLeverSignal State
|
||||
{
|
||||
get => _state;
|
||||
private set
|
||||
{
|
||||
_state = value;
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(TwoWayLeverVisuals.State, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void NextState(IEntity user)
|
||||
{
|
||||
State = State switch
|
||||
{
|
||||
TwoWayLeverSignal.Left => TwoWayLeverSignal.Middle,
|
||||
TwoWayLeverSignal.Middle => _nextForward ? TwoWayLeverSignal.Right : TwoWayLeverSignal.Left,
|
||||
TwoWayLeverSignal.Right => TwoWayLeverSignal.Middle,
|
||||
_ => TwoWayLeverSignal.Middle
|
||||
};
|
||||
|
||||
if (State == TwoWayLeverSignal.Left || State == TwoWayLeverSignal.Right) _nextForward = !_nextForward;
|
||||
|
||||
if (!TransmitSignal(State))
|
||||
{
|
||||
Owner.PopupMessage(user, Loc.GetString("signal-two-way-lever-component-next-state-no-receivers-connected"));
|
||||
}
|
||||
}
|
||||
|
||||
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
|
||||
{
|
||||
NextState(eventArgs.User);
|
||||
return true;
|
||||
}
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
NextState(eventArgs.User);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using Content.Shared.MachineLinking;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.MachineLinking.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class TwoWayLeverComponent : Component
|
||||
{
|
||||
public override string Name => "TwoWayLever";
|
||||
|
||||
public TwoWayLeverSignal State;
|
||||
|
||||
public bool NextSignalLeft;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user