ECS computers (#6353)

This commit is contained in:
metalgearsloth
2022-02-03 10:20:17 +11:00
committed by GitHub
parent 796329cf17
commit 32a1f6ae93
5 changed files with 89 additions and 103 deletions

View File

@@ -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<EntityPrototype>))]
private string? _boardPrototype;
protected override void Initialize()
{
base.Initialize();
// Let's ensure the container manager and container are here.
Owner.EnsureContainer<Container>("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);
}
}
/// <summary>
/// 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.
/// </summary>
private void CreateComputerBoard()
{
// Ensure that the construction component is aware of the board container.
if (_entMan.TryGetComponent(Owner, out ConstructionComponent? construction))
EntitySystem.Get<ConstructionSystem>().AddContainer(Owner, "board", construction);
// We don't do anything if this is null or empty.
if (string.IsNullOrEmpty(_boardPrototype))
return;
var container = Owner.EnsureContainer<Container>("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<TransformComponent>(Owner).Coordinates);
if(!container.Insert(board))
Logger.Warning($"Couldn't insert board {board} to computer {Owner}!");
}
public void MapInit()
{
CreateComputerBoard();
}
}
}

View File

@@ -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<EntityPrototype>))]
public string? BoardPrototype;
}
}

View File

@@ -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<ComputerComponent, ComponentInit>(OnCompInit);
SubscribeLocalEvent<ComputerComponent, MapInitEvent>(OnCompMapInit);
SubscribeLocalEvent<ComputerComponent, PowerChangedEvent>(OnCompPowerChange);
}
private void OnCompInit(EntityUid uid, ComputerComponent component, ComponentInit args)
{
// Let's ensure the container manager and container are here.
_container.EnsureContainer<Container>(uid, "board");
if (TryComp<ApcPowerReceiverComponent>(uid, out var powerReceiver) &&
TryComp<AppearanceComponent>(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<AppearanceComponent>(uid, out var appearance))
{
appearance.SetData(ComputerVisuals.Powered, args.Powered);
}
}
/// <summary>
/// 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.
/// </summary>
private void CreateComputerBoard(ComputerComponent component)
{
// Ensure that the construction component is aware of the board container.
if (TryComp<ConstructionComponent>(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<Container>(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}!");
}
}

View File

@@ -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();

View File

@@ -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,