Refactors machine linking to use generics (#2462)

Co-authored-by: Paul <ritter.paul1+git@googlemail.com>
This commit is contained in:
Paul Ritter
2020-10-30 19:46:19 +01:00
committed by GitHub
parent 95590833ce
commit f36cba8e21
7 changed files with 31 additions and 35 deletions

View File

@@ -1,14 +1,9 @@
namespace Content.Server.GameObjects.Components.MachineLinking using System;
{
public interface ISignalReceiver
{
void TriggerSignal(SignalState state);
}
public enum SignalState namespace Content.Server.GameObjects.Components.MachineLinking
{
public interface ISignalReceiver<in T>
{ {
On, void TriggerSignal(T signal);
Off,
Toggle
} }
} }

View File

@@ -1,4 +1,5 @@
using Content.Shared.Interfaces; using Content.Server.GameObjects.Components.MachineLinking.Signals;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components; using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
@@ -29,7 +30,7 @@ namespace Content.Server.GameObjects.Components.MachineLinking
return; return;
} }
if (transmitter.TransmitSignal(user, SignalState.Toggle)) if (transmitter.TransmitSignal(user, new ToggleSignal()))
{ {
// Since the button doesn't have an animation, I'm going to use a popup message // Since the button doesn't have an animation, I'm going to use a popup message
Owner.PopupMessage(user, Loc.GetString("Click.")); Owner.PopupMessage(user, Loc.GetString("Click."));

View File

@@ -26,9 +26,9 @@ namespace Content.Server.GameObjects.Components.MachineLinking
_transmitters = new List<SignalTransmitterComponent>(); _transmitters = new List<SignalTransmitterComponent>();
} }
public void DistributeSignal(SignalState state) public void DistributeSignal<T>(T state)
{ {
foreach (var comp in Owner.GetAllComponents<ISignalReceiver>()) foreach (var comp in Owner.GetAllComponents<ISignalReceiver<T>>())
{ {
comp.TriggerSignal(state); comp.TriggerSignal(state);
} }

View File

@@ -52,7 +52,7 @@ namespace Content.Server.GameObjects.Components.MachineLinking
return; return;
} }
transmitter.TransmitSignal(user, _on ? SignalState.On : SignalState.Off); transmitter.TransmitSignal(user, _on);
} }
private void UpdateSprite() private void UpdateSprite()

View File

@@ -87,7 +87,7 @@ namespace Content.Server.GameObjects.Components.MachineLinking
} }
} }
public bool TransmitSignal(IEntity user, SignalState state) public bool TransmitSignal<T>(IEntity user, T signal)
{ {
if (_receivers.Count == 0) if (_receivers.Count == 0)
{ {
@@ -102,7 +102,7 @@ namespace Content.Server.GameObjects.Components.MachineLinking
continue; continue;
} }
receiver.DistributeSignal(state); receiver.DistributeSignal(signal);
} }
return true; return true;
} }

View File

@@ -0,0 +1,4 @@
namespace Content.Server.GameObjects.Components.MachineLinking.Signals
{
public class ToggleSignal {}
}

View File

@@ -3,6 +3,7 @@ using System.Threading.Tasks;
using Content.Server.GameObjects.Components.GUI; using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Items.Storage; using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.GameObjects.Components.MachineLinking; using Content.Server.GameObjects.Components.MachineLinking;
using Content.Server.GameObjects.Components.MachineLinking.Signals;
using Content.Server.GameObjects.Components.Mobs; using Content.Server.GameObjects.Components.Mobs;
using Content.Shared.Damage; using Content.Shared.Damage;
using Content.Shared.GameObjects.Components.Damage; using Content.Shared.GameObjects.Components.Damage;
@@ -28,7 +29,7 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
/// Component that represents a wall light. It has a light bulb that can be replaced when broken. /// Component that represents a wall light. It has a light bulb that can be replaced when broken.
/// </summary> /// </summary>
[RegisterComponent] [RegisterComponent]
public class PoweredLightComponent : Component, IInteractHand, IInteractUsing, IMapInit, ISignalReceiver public class PoweredLightComponent : Component, IInteractHand, IInteractUsing, IMapInit, ISignalReceiver<bool>, ISignalReceiver<ToggleSignal>
{ {
[Dependency] private IGameTiming _gameTiming = default!; [Dependency] private IGameTiming _gameTiming = default!;
@@ -62,23 +63,6 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
return InsertBulb(eventArgs.Using); return InsertBulb(eventArgs.Using);
} }
public void TriggerSignal(SignalState state)
{
switch (state)
{
case SignalState.On:
_on = true;
break;
case SignalState.Off:
_on = false;
break;
case SignalState.Toggle:
_on = !_on;
break;
}
UpdateLight();
}
public bool InteractHand(InteractHandEventArgs eventArgs) public bool InteractHand(InteractHandEventArgs eventArgs)
{ {
if (!eventArgs.User.TryGetComponent(out IDamageableComponent damageableComponent)) if (!eventArgs.User.TryGetComponent(out IDamageableComponent damageableComponent))
@@ -252,5 +236,17 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents.PowerRece
var entity = Owner.EntityManager.SpawnEntity(prototype, Owner.Transform.Coordinates); var entity = Owner.EntityManager.SpawnEntity(prototype, Owner.Transform.Coordinates);
_lightBulbContainer.Insert(entity); _lightBulbContainer.Insert(entity);
} }
public void TriggerSignal(bool signal)
{
_on = signal;
UpdateLight();
}
public void TriggerSignal(ToggleSignal signal)
{
_on = !_on;
UpdateLight();
}
} }
} }