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,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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user