Files
tbd-station-14/Content.Server/GameObjects/Components/MachineLinking/SignalReceiverComponent.cs
DrSmugleaf 48b61f6bcc Replace every usage of GridCoordinates with EntityCoordinates (#2021)
* Update RobustToolbox

* Transition direct type usages

* More updates

* Fix invalid use of to map

* Update RobustToolbox

* Fix dropping items

* Rename name usages of "GridCoordinates" to "EntityCoordinates"

* Revert "Update RobustToolbox"

This reverts commit 9f334a17c5908ded0043a63158bb671e4aa3f346.

* Revert "Update RobustToolbox"

This reverts commit 3a9c8cfa3606fa501aa84407796d2ad920853a09.

# Conflicts:
#	RobustToolbox

* Fix cursed IMapGrid method usage.

* GridTileLookupTest now uses EntityCoordinates

Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com>
Co-authored-by: Víctor Aguilera Puerto <zddm@outlook.es>
2020-09-06 16:11:53 +02:00

119 lines
3.6 KiB
C#

using Content.Server.GameObjects.Components.Interactable;
using Content.Shared.GameObjects.Components.Interactable;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Content.Server.GameObjects.Components.MachineLinking
{
[RegisterComponent]
public class SignalReceiverComponent : Component, IInteractUsing
{
[Dependency] private readonly IMapManager _mapManager = default!;
public override string Name => "SignalReceiver";
private List<SignalTransmitterComponent> _transmitters;
public override void Initialize()
{
base.Initialize();
_transmitters = new List<SignalTransmitterComponent>();
}
public void DistributeSignal(SignalState state)
{
foreach (var comp in Owner.GetAllComponents<ISignalReceiver>())
{
comp.TriggerSignal(state);
}
}
public void Subscribe(SignalTransmitterComponent transmitter)
{
if (_transmitters.Contains(transmitter))
{
return;
}
transmitter.Subscribe(this);
_transmitters.Add(transmitter);
}
public void Unsubscribe(SignalTransmitterComponent transmitter)
{
transmitter.Unsubscribe(this);
_transmitters.Remove(transmitter);
}
/// <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 not set."));
return false;
}
if (_transmitters.Contains(transmitter))
{
Unsubscribe(transmitter);
Owner.PopupMessage(user, Loc.GetString("Unlinked."));
return true;
}
if (transmitter.Range > 0 && !Owner.Transform.Coordinates.InRange(Owner.EntityManager, transmitter.Owner.Transform.Coordinates, transmitter.Range))
{
Owner.PopupMessage(user, Loc.GetString("Out of range."));
return false;
}
Subscribe(transmitter);
Owner.PopupMessage(user, Loc.GetString("Linked!"));
return true;
}
public async Task<bool> 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();
foreach (var transmitter in _transmitters)
{
if (transmitter.Deleted)
{
continue;
}
transmitter.Unsubscribe(this);
}
_transmitters.Clear();
}
}
}