From 32a1f6ae933a0d3f1270d612e66ce596f9b9fcb7 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Thu, 3 Feb 2022 10:20:17 +1100 Subject: [PATCH] ECS computers (#6353) --- Content.Server/Computer/ComputerComponent.cs | 97 ------------------- .../Components/ComputerComponent.cs | 16 +++ .../ConstructionSystem.Computer.cs | 70 +++++++++++++ .../Construction/ConstructionSystem.cs | 3 +- .../Computer/SharedComputerComponent.cs | 6 +- 5 files changed, 89 insertions(+), 103 deletions(-) delete mode 100644 Content.Server/Computer/ComputerComponent.cs create mode 100644 Content.Server/Construction/Components/ComputerComponent.cs create mode 100644 Content.Server/Construction/ConstructionSystem.Computer.cs diff --git a/Content.Server/Computer/ComputerComponent.cs b/Content.Server/Computer/ComputerComponent.cs deleted file mode 100644 index 7cf59af18a..0000000000 --- a/Content.Server/Computer/ComputerComponent.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System; -using Content.Server.Construction; -using Content.Server.Construction.Components; -using Content.Server.Power.Components; -using Content.Shared.Computer; -using Robust.Shared.Containers; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Log; -using Robust.Shared.Prototypes; -using Robust.Shared.Serialization.Manager.Attributes; -using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; -using Robust.Shared.ViewVariables; - -namespace Content.Server.Computer -{ - [RegisterComponent] - public sealed class ComputerComponent : SharedComputerComponent, IMapInit - { - [Dependency] private readonly IEntityManager _entMan = default!; - - [ViewVariables] - [DataField("board", customTypeSerializer:typeof(PrototypeIdSerializer))] - private string? _boardPrototype; - - protected override void Initialize() - { - base.Initialize(); - - // Let's ensure the container manager and container are here. - Owner.EnsureContainer("board", out var _); - - if (_entMan.TryGetComponent(Owner, out ApcPowerReceiverComponent? powerReceiver) && - _entMan.TryGetComponent(Owner, out AppearanceComponent? appearance)) - { - appearance.SetData(ComputerVisuals.Powered, powerReceiver.Powered); - } - } - - [Obsolete("Component Messages are deprecated, use Entity Events instead.")] - public override void HandleMessage(ComponentMessage message, IComponent? component) - { -#pragma warning disable 618 - base.HandleMessage(message, component); -#pragma warning restore 618 - switch (message) - { - case PowerChangedMessage powerChanged: - PowerReceiverOnOnPowerStateChanged(powerChanged); - break; - } - } - - private void PowerReceiverOnOnPowerStateChanged(PowerChangedMessage e) - { - if (_entMan.TryGetComponent(Owner, out AppearanceComponent? appearance)) - { - appearance.SetData(ComputerVisuals.Powered, e.Powered); - } - } - - /// - /// Creates the corresponding computer board on the computer. - /// This exists so when you deconstruct computers that were serialized with the map, - /// you can retrieve the computer board. - /// - private void CreateComputerBoard() - { - // Ensure that the construction component is aware of the board container. - if (_entMan.TryGetComponent(Owner, out ConstructionComponent? construction)) - EntitySystem.Get().AddContainer(Owner, "board", construction); - - // We don't do anything if this is null or empty. - if (string.IsNullOrEmpty(_boardPrototype)) - return; - - var container = Owner.EnsureContainer("board", out var existed); - - if (existed) - { - // We already contain a board. Note: We don't check if it's the right one! - if (container.ContainedEntities.Count != 0) - return; - } - - var board = _entMan.SpawnEntity(_boardPrototype, _entMan.GetComponent(Owner).Coordinates); - - if(!container.Insert(board)) - Logger.Warning($"Couldn't insert board {board} to computer {Owner}!"); - } - - public void MapInit() - { - CreateComputerBoard(); - } - } -} diff --git a/Content.Server/Construction/Components/ComputerComponent.cs b/Content.Server/Construction/Components/ComputerComponent.cs new file mode 100644 index 0000000000..e76e957d84 --- /dev/null +++ b/Content.Server/Construction/Components/ComputerComponent.cs @@ -0,0 +1,16 @@ +using Robust.Shared.GameObjects; +using Robust.Shared.Prototypes; +using Robust.Shared.Serialization.Manager.Attributes; +using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; +using Robust.Shared.ViewVariables; + +namespace Content.Server.Construction.Components +{ + [RegisterComponent, ComponentProtoName("Computer")] + public sealed class ComputerComponent : Component + { + [ViewVariables] + [DataField("board", customTypeSerializer:typeof(PrototypeIdSerializer))] + public string? BoardPrototype; + } +} diff --git a/Content.Server/Construction/ConstructionSystem.Computer.cs b/Content.Server/Construction/ConstructionSystem.Computer.cs new file mode 100644 index 0000000000..2cf7e1b7be --- /dev/null +++ b/Content.Server/Construction/ConstructionSystem.Computer.cs @@ -0,0 +1,70 @@ +using Content.Server.Construction.Components; +using Content.Server.Power.Components; +using Content.Shared.Computer; +using Robust.Shared.Containers; +using Robust.Shared.GameObjects; +using Robust.Shared.Log; + +namespace Content.Server.Construction; + +public sealed partial class ConstructionSystem +{ + private void InitializeComputer() + { + SubscribeLocalEvent(OnCompInit); + SubscribeLocalEvent(OnCompMapInit); + SubscribeLocalEvent(OnCompPowerChange); + } + + private void OnCompInit(EntityUid uid, ComputerComponent component, ComponentInit args) + { + // Let's ensure the container manager and container are here. + _container.EnsureContainer(uid, "board"); + + if (TryComp(uid, out var powerReceiver) && + TryComp(uid, out var appearance)) + { + appearance.SetData(ComputerVisuals.Powered, powerReceiver.Powered); + } + } + + private void OnCompMapInit(EntityUid uid, ComputerComponent component, MapInitEvent args) + { + CreateComputerBoard(component); + } + + private void OnCompPowerChange(EntityUid uid, ComputerComponent component, PowerChangedEvent args) + { + if (TryComp(uid, out var appearance)) + { + appearance.SetData(ComputerVisuals.Powered, args.Powered); + } + } + + /// + /// Creates the corresponding computer board on the computer. + /// This exists so when you deconstruct computers that were serialized with the map, + /// you can retrieve the computer board. + /// + private void CreateComputerBoard(ComputerComponent component) + { + // Ensure that the construction component is aware of the board container. + if (TryComp(component.Owner, out var construction)) + AddContainer(component.Owner, "board", construction); + + // We don't do anything if this is null or empty. + if (string.IsNullOrEmpty(component.BoardPrototype)) + return; + + var container = _container.EnsureContainer(component.Owner, "board"); + + // We already contain a board. Note: We don't check if it's the right one! + if (container.ContainedEntities.Count != 0) + return; + + var board = EntityManager.SpawnEntity(component.BoardPrototype, Transform(component.Owner).Coordinates); + + if(!container.Insert(board)) + Logger.Warning($"Couldn't insert board {board} to computer {component.Owner}!"); + } +} diff --git a/Content.Server/Construction/ConstructionSystem.cs b/Content.Server/Construction/ConstructionSystem.cs index 96036b28fa..edd3aeee5f 100644 --- a/Content.Server/Construction/ConstructionSystem.cs +++ b/Content.Server/Construction/ConstructionSystem.cs @@ -27,9 +27,9 @@ namespace Content.Server.Construction [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IRobustRandom _robustRandom = default!; [Dependency] private readonly DoAfterSystem _doAfterSystem = default!; + [Dependency] private readonly SharedContainerSystem _container = default!; [Dependency] private readonly StackSystem _stackSystem = default!; [Dependency] private readonly ToolSystem _toolSystem = default!; - [Dependency] private readonly SharedContainerSystem _container = default!; private const string SawmillName = "Construction"; private ISawmill _sawmill = default!; @@ -40,6 +40,7 @@ namespace Content.Server.Construction _sawmill = _logManager.GetSawmill(SawmillName); + InitializeComputer(); InitializeGraphs(); InitializeGuided(); InitializeInteractions(); diff --git a/Content.Shared/Computer/SharedComputerComponent.cs b/Content.Shared/Computer/SharedComputerComponent.cs index 4de6e5f5fe..2950fde4d0 100644 --- a/Content.Shared/Computer/SharedComputerComponent.cs +++ b/Content.Shared/Computer/SharedComputerComponent.cs @@ -4,12 +4,8 @@ using Robust.Shared.Serialization; namespace Content.Shared.Computer { - public class SharedComputerComponent : Component - { - } - [Serializable, NetSerializable] - public enum ComputerVisuals + public enum ComputerVisuals : byte { // Bool Powered,