Replaces AnchoredChanged C# event with ComponentMessage (#2905)

* Replaces AnchoredChanged C# event with ComponentMessage

* Removes unneeded fields

Co-authored-by: py01 <pyronetics01@gmail.com>
This commit is contained in:
py01
2021-01-04 22:32:59 -06:00
committed by GitHub
parent 350f7313be
commit 1032576a20
11 changed files with 126 additions and 87 deletions

View File

@@ -1,4 +1,4 @@
#nullable enable #nullable enable
using System; using System;
using Content.Server.Atmos; using Content.Server.Atmos;
using Content.Server.GameObjects.Components.Atmos.Piping; using Content.Server.GameObjects.Components.Atmos.Piping;
@@ -92,7 +92,6 @@ namespace Content.Server.GameObjects.Components.Atmos
if (Owner.TryGetComponent<IPhysicsComponent>(out var physics)) if (Owner.TryGetComponent<IPhysicsComponent>(out var physics))
{ {
AnchorUpdate(); AnchorUpdate();
physics.AnchoredChanged += AnchorUpdate;
} }
if (UserInterface != null) if (UserInterface != null)
{ {
@@ -107,15 +106,22 @@ namespace Content.Server.GameObjects.Components.Atmos
UpdateAppearance(); UpdateAppearance();
} }
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case AnchoredChangedMessage:
AnchorUpdate();
break;
}
}
#region Connector port methods #region Connector port methods
public override void OnRemove() public override void OnRemove()
{ {
base.OnRemove(); base.OnRemove();
if (Owner.TryGetComponent<IPhysicsComponent>(out var physics))
{
physics.AnchoredChanged -= AnchorUpdate;
}
if (UserInterface != null) if (UserInterface != null)
{ {
UserInterface.OnReceiveMessage -= OnUiReceiveMessage; UserInterface.OnReceiveMessage -= OnUiReceiveMessage;

View File

@@ -614,22 +614,12 @@ namespace Content.Server.GameObjects.Components.Disposal
Logger.WarningS("VitalComponentMissing", $"Disposal unit {Owner.Uid} is missing an anchorable component"); Logger.WarningS("VitalComponentMissing", $"Disposal unit {Owner.Uid} is missing an anchorable component");
} }
if (Owner.TryGetComponent(out IPhysicsComponent? physics))
{
physics.AnchoredChanged += UpdateVisualState;
}
UpdateTargetList(); UpdateTargetList();
UpdateVisualState(); UpdateVisualState();
} }
public override void OnRemove() public override void OnRemove()
{ {
if (Owner.TryGetComponent(out IPhysicsComponent? physics))
{
physics.AnchoredChanged -= UpdateVisualState;
}
if (_container != null) if (_container != null)
{ {
foreach (var entity in _container.ContainedEntities.ToArray()) foreach (var entity in _container.ContainedEntities.ToArray())
@@ -671,6 +661,10 @@ namespace Content.Server.GameObjects.Components.Disposal
Remove(msg.Entity); Remove(msg.Entity);
break; break;
case AnchoredChangedMessage:
UpdateVisualState();
break;
case PowerChangedMessage powerChanged: case PowerChangedMessage powerChanged:
PowerStateChanged(powerChanged); PowerStateChanged(powerChanged);
break; break;

View File

@@ -1,4 +1,4 @@
#nullable enable #nullable enable
using System; using System;
using System.Linq; using System.Linq;
using Content.Shared.GameObjects.Components.Disposal; using Content.Shared.GameObjects.Components.Disposal;
@@ -231,10 +231,6 @@ namespace Content.Server.GameObjects.Components.Disposal
Contents = ContainerManagerComponent.Ensure<Container>(Name, Owner); Contents = ContainerManagerComponent.Ensure<Container>(Name, Owner);
Owner.EnsureComponent<AnchorableComponent>(); Owner.EnsureComponent<AnchorableComponent>();
var physics = Owner.EnsureComponent<PhysicsComponent>();
physics.AnchoredChanged += AnchoredChanged;
} }
protected override void Startup() protected override void Startup()
@@ -254,9 +250,6 @@ namespace Content.Server.GameObjects.Components.Disposal
{ {
base.OnRemove(); base.OnRemove();
var physics = Owner.EnsureComponent<PhysicsComponent>();
physics.AnchoredChanged -= AnchoredChanged;
Disconnect(); Disconnect();
} }
@@ -275,6 +268,10 @@ namespace Content.Server.GameObjects.Components.Disposal
_lastClang = _gameTiming.CurTime; _lastClang = _gameTiming.CurTime;
EntitySystem.Get<AudioSystem>().PlayAtCoords(_clangSound, Owner.Transform.Coordinates); EntitySystem.Get<AudioSystem>().PlayAtCoords(_clangSound, Owner.Transform.Coordinates);
break; break;
case AnchoredChangedMessage:
AnchoredChanged();
break;
} }
} }

View File

@@ -569,21 +569,11 @@ namespace Content.Server.GameObjects.Components.Disposal
Logger.WarningS("VitalComponentMissing", $"Disposal unit {Owner.Uid} is missing an anchorable component"); Logger.WarningS("VitalComponentMissing", $"Disposal unit {Owner.Uid} is missing an anchorable component");
} }
if (Owner.TryGetComponent(out IPhysicsComponent? physics))
{
physics.AnchoredChanged += UpdateVisualState;
}
UpdateVisualState(); UpdateVisualState();
} }
public override void OnRemove() public override void OnRemove()
{ {
if (Owner.TryGetComponent(out IPhysicsComponent? physics))
{
physics.AnchoredChanged -= UpdateVisualState;
}
foreach (var entity in _container.ContainedEntities.ToArray()) foreach (var entity in _container.ContainedEntities.ToArray())
{ {
_container.ForceRemove(entity); _container.ForceRemove(entity);
@@ -617,6 +607,10 @@ namespace Content.Server.GameObjects.Components.Disposal
Remove(msg.Entity); Remove(msg.Entity);
break; break;
case AnchoredChangedMessage:
UpdateVisualState();
break;
case PowerChangedMessage powerChanged: case PowerChangedMessage powerChanged:
PowerStateChanged(powerChanged); PowerStateChanged(powerChanged);
break; break;

View File

@@ -1,8 +1,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups; using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Content.Server.GameObjects.Components.NodeContainer.Nodes; using Content.Server.GameObjects.Components.NodeContainer.Nodes;
using Content.Shared.GameObjects.EntitySystems; using Content.Shared.GameObjects.EntitySystems;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using Robust.Shared.Utility; using Robust.Shared.Utility;
@@ -48,6 +50,17 @@ namespace Content.Server.GameObjects.Components.NodeContainer
} }
} }
public override void HandleMessage(ComponentMessage message, IComponent component)
{
base.HandleMessage(message, component);
switch (message)
{
case AnchoredChangedMessage:
AnchorUpdate();
break;
}
}
public override void OnRemove() public override void OnRemove()
{ {
foreach (var node in _nodes) foreach (var node in _nodes)
@@ -57,6 +70,14 @@ namespace Content.Server.GameObjects.Components.NodeContainer
base.OnRemove(); base.OnRemove();
} }
private void AnchorUpdate()
{
foreach (var node in Nodes)
{
node.AnchorUpdate();
}
}
public void Examine(FormattedMessage message, bool inDetailsRange) public void Examine(FormattedMessage message, bool inDetailsRange)
{ {
if (!_examinable || !inDetailsRange) return; if (!_examinable || !inDetailsRange) return;

View File

@@ -1,4 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups; using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
@@ -66,17 +66,29 @@ namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
if (Owner.TryGetComponent<IPhysicsComponent>(out var physics)) if (Owner.TryGetComponent<IPhysicsComponent>(out var physics))
{ {
AnchorUpdate(); AnchorUpdate();
physics.AnchoredChanged += AnchorUpdate; }
}
public void AnchorUpdate()
{
if (Anchored)
{
if (_needsGroup)
{
TryAssignGroupIfNeeded();
CombineGroupWithReachable();
}
}
else
{
NodeGroup.RemoveNode(this);
ClearNodeGroup();
} }
} }
public void OnContainerRemove() public void OnContainerRemove()
{ {
_deleting = true; _deleting = true;
if (Owner.TryGetComponent<IPhysicsComponent>(out var physics))
{
physics.AnchoredChanged -= AnchorUpdate;
}
NodeGroup.RemoveNode(this); NodeGroup.RemoveNode(this);
} }
@@ -153,22 +165,5 @@ namespace Content.Server.GameObjects.Components.NodeContainer.Nodes
{ {
return _nodeGroupFactory.MakeNodeGroup(this); return _nodeGroupFactory.MakeNodeGroup(this);
} }
private void AnchorUpdate()
{
if (Anchored)
{
if (_needsGroup)
{
TryAssignGroupIfNeeded();
CombineGroupWithReachable();
}
}
else
{
NodeGroup.RemoveNode(this);
ClearNodeGroup();
}
}
} }
} }

View File

@@ -1,8 +1,9 @@
#nullable enable #nullable enable
using Content.Server.Utility; using Content.Server.Utility;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components; using Robust.Shared.GameObjects.Components;
using Robust.Shared.GameObjects.Components.Transform; using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Log; using Robust.Shared.Log;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
@@ -10,7 +11,6 @@ namespace Content.Server.GameObjects.Components.PA
{ {
public abstract class ParticleAcceleratorPartComponent : Component public abstract class ParticleAcceleratorPartComponent : Component
{ {
[ViewVariables] private PhysicsComponent? _collidableComponent;
[ViewVariables] public ParticleAcceleratorControlBoxComponent? Master; [ViewVariables] public ParticleAcceleratorControlBoxComponent? Master;
[ViewVariables] protected SnapGridComponent? SnapGrid; [ViewVariables] protected SnapGridComponent? SnapGrid;
@@ -18,14 +18,6 @@ namespace Content.Server.GameObjects.Components.PA
{ {
base.Initialize(); base.Initialize();
// FIXME: this has to be an entity system, full stop. // FIXME: this has to be an entity system, full stop.
if (!Owner.TryGetComponent(out _collidableComponent))
{
Logger.Error("ParticleAcceleratorPartComponent created with no CollidableComponent");
}
else
{
_collidableComponent.AnchoredChanged += OnAnchorChanged;
}
if (!Owner.TryGetComponent(out SnapGrid)) if (!Owner.TryGetComponent(out SnapGrid))
{ {
@@ -33,6 +25,17 @@ namespace Content.Server.GameObjects.Components.PA
} }
} }
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case AnchoredChangedMessage:
OnAnchorChanged();
break;
}
}
public void OnAnchorChanged() public void OnAnchorChanged()
{ {
RescanIfPossible(); RescanIfPossible();

View File

@@ -1,6 +1,5 @@
#nullable enable #nullable enable
using System; using System;
using Content.Server.GameObjects.Components.NodeContainer;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups; using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Content.Shared.GameObjects.Components.Power; using Content.Shared.GameObjects.Components.Power;
using Content.Shared.GameObjects.EntitySystems; using Content.Shared.GameObjects.EntitySystems;
@@ -9,6 +8,7 @@ using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies; using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.GameObjects.Components; using Robust.Shared.GameObjects.Components;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
@@ -99,16 +99,11 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
if (_physicsComponent != null) if (_physicsComponent != null)
{ {
AnchorUpdate(); AnchorUpdate();
_physicsComponent.AnchoredChanged += AnchorUpdate;
} }
} }
public override void OnRemove() public override void OnRemove()
{ {
if (_physicsComponent != null)
{
_physicsComponent.AnchoredChanged -= AnchorUpdate;
}
_provider.RemoveReceiver(this); _provider.RemoveReceiver(this);
base.OnRemove(); base.OnRemove();
} }
@@ -121,6 +116,17 @@ namespace Content.Server.GameObjects.Components.Power.ApcNetComponents
} }
} }
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case AnchoredChangedMessage:
AnchorUpdate();
break;
}
}
public void ApcPowerChanged() public void ApcPowerChanged()
{ {
var oldPowered = Powered; var oldPowered = Powered;

View File

@@ -9,6 +9,7 @@ using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components; using Robust.Shared.GameObjects.Components;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Timing; using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Localization; using Robust.Shared.Localization;
@@ -36,7 +37,17 @@ namespace Content.Server.GameObjects.Components.Power.PowerNetComponents
Logger.Error("RadiationCollectorComponent created with no CollidableComponent"); Logger.Error("RadiationCollectorComponent created with no CollidableComponent");
return; return;
} }
_collidableComponent.AnchoredChanged += OnAnchoredChanged; }
public override void HandleMessage(ComponentMessage message, IComponent component)
{
base.HandleMessage(message, component);
switch (message)
{
case AnchoredChangedMessage:
OnAnchoredChanged();
break;
}
} }
private void OnAnchoredChanged() private void OnAnchoredChanged()

View File

@@ -79,9 +79,18 @@ namespace Content.Server.GameObjects.Components.Singularity
Logger.Error("ContainmentFieldGeneratorComponent created with no CollidableComponent"); Logger.Error("ContainmentFieldGeneratorComponent created with no CollidableComponent");
return; return;
} }
_collidableComponent.AnchoredChanged += OnAnchoredChanged;
} }
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case AnchoredChangedMessage:
OnAnchoredChanged();
break;
}
}
private void OnAnchoredChanged() private void OnAnchoredChanged()
{ {

View File

@@ -16,6 +16,7 @@ using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.ComponentDependencies; using Robust.Shared.GameObjects.ComponentDependencies;
using Robust.Shared.GameObjects.Components; using Robust.Shared.GameObjects.Components;
using Robust.Shared.GameObjects.Systems; using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random; using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Localization; using Robust.Shared.Localization;
@@ -43,7 +44,6 @@ namespace Content.Server.GameObjects.Components.Singularity
private CancellationTokenSource? _timerCancel; private CancellationTokenSource? _timerCancel;
private PhysicsComponent _collidableComponent = default!;
private PowerConsumerComponent _powerConsumer = default!; private PowerConsumerComponent _powerConsumer = default!;
// whether the power switch is in "on" // whether the power switch is in "on"
@@ -79,19 +79,11 @@ namespace Content.Server.GameObjects.Components.Singularity
{ {
base.Initialize(); base.Initialize();
if (!Owner.TryGetComponent(out _collidableComponent!))
{
Logger.Error($"EmitterComponent {Owner} created with no CollidableComponent");
return;
}
if (!Owner.TryGetComponent(out _powerConsumer!)) if (!Owner.TryGetComponent(out _powerConsumer!))
{ {
Logger.Error($"EmitterComponent {Owner} created with no PowerConsumerComponent"); Logger.Error($"EmitterComponent {Owner} created with no PowerConsumerComponent");
return; return;
} }
_collidableComponent.AnchoredChanged += OnAnchoredChanged;
_powerConsumer.OnReceivedPowerChanged += OnReceivedPowerChanged; _powerConsumer.OnReceivedPowerChanged += OnReceivedPowerChanged;
} }
@@ -112,9 +104,20 @@ namespace Content.Server.GameObjects.Components.Singularity
} }
} }
private void OnAnchoredChanged() public override void HandleMessage(ComponentMessage message, IComponent? component)
{ {
if (_collidableComponent.Anchored) base.HandleMessage(message, component);
switch (message)
{
case AnchoredChangedMessage anchoredChanged:
OnAnchoredChanged(anchoredChanged);
break;
}
}
private void OnAnchoredChanged(AnchoredChangedMessage anchoredChanged)
{
if (anchoredChanged.Anchored)
Owner.SnapToGrid(); Owner.SnapToGrid();
} }