From b478d5326b797407945742b43e2d2c6f129a6d59 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Wed, 26 Jul 2023 22:37:52 +1000 Subject: [PATCH] Remove component.Initialize calls (#18230) --- .../Animations/AnimationsTestComponent.cs | 51 ------------------- .../Spawners/ClientEntitySpawnerComponent.cs | 50 ------------------ Content.Server/Entry/IgnoredComponents.cs | 2 - .../Components/BaseNetConnectorComponent.cs | 13 +---- .../EntitySystems/PowerNetConnectorSystem.cs | 49 ++++++++++++++++++ .../Power/EntitySystems/PowerNetSystem.cs | 3 ++ .../Components/TimedSpawnerComponent.cs | 29 ----------- .../Spawners/EntitySystems/SpawnerSystem.cs | 38 ++++++++++++++ .../Entities/Effects/animations_test.yml | 9 ---- 9 files changed, 91 insertions(+), 153 deletions(-) delete mode 100644 Content.Client/Animations/AnimationsTestComponent.cs delete mode 100644 Content.Client/Spawners/ClientEntitySpawnerComponent.cs create mode 100644 Content.Server/Power/EntitySystems/PowerNetConnectorSystem.cs create mode 100644 Content.Server/Spawners/EntitySystems/SpawnerSystem.cs delete mode 100644 Resources/Prototypes/Entities/Effects/animations_test.yml diff --git a/Content.Client/Animations/AnimationsTestComponent.cs b/Content.Client/Animations/AnimationsTestComponent.cs deleted file mode 100644 index f951295ed5..0000000000 --- a/Content.Client/Animations/AnimationsTestComponent.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using Robust.Client.Animations; -using Robust.Client.GameObjects; -using Robust.Shared.Animations; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Maths; - -namespace Content.Client.Animations -{ - [RegisterComponent] - public sealed class AnimationsTestComponent : Component - { - protected override void Initialize() - { - base.Initialize(); - - var animations = IoCManager.Resolve().GetComponent(Owner); - animations.Play(new Animation - { - Length = TimeSpan.FromSeconds(20), - AnimationTracks = - { - new AnimationTrackComponentProperty - { - ComponentType = typeof(TransformComponent), - Property = nameof(TransformComponent.LocalRotation), - InterpolationMode = AnimationInterpolationMode.Linear, - KeyFrames = - { - new AnimationTrackProperty.KeyFrame(Angle.Zero, 0), - new AnimationTrackProperty.KeyFrame(Angle.FromDegrees(1440), 20) - } - }, - new AnimationTrackComponentProperty - { - ComponentType = typeof(SpriteComponent), - Property = "layer/0/texture", - KeyFrames = - { - new AnimationTrackProperty.KeyFrame("Objects/toolbox_r.png", 0), - new AnimationTrackProperty.KeyFrame("Objects/Toolbox_b.png", 5), - new AnimationTrackProperty.KeyFrame("Objects/Toolbox_y.png", 5), - new AnimationTrackProperty.KeyFrame("Objects/toolbox_r.png", 5), - } - } - } - }, "yes"); - } - } -} diff --git a/Content.Client/Spawners/ClientEntitySpawnerComponent.cs b/Content.Client/Spawners/ClientEntitySpawnerComponent.cs deleted file mode 100644 index c9a32c1a2c..0000000000 --- a/Content.Client/Spawners/ClientEntitySpawnerComponent.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System.Collections.Generic; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Serialization.Manager.Attributes; - -namespace Content.Client.Spawners -{ - /// - /// Spawns a set of entities on the client only, and removes them when this component is removed. - /// - [RegisterComponent] - [ComponentProtoName("ClientEntitySpawner")] - public sealed class ClientEntitySpawnerComponent : Component - { - [Dependency] private readonly IEntityManager _entMan = default!; - - [DataField("prototypes")] private List _prototypes = new() { "HVDummyWire" }; - - private readonly List _entity = new(); - - protected override void Initialize() - { - base.Initialize(); - SpawnEntities(); - } - - protected override void OnRemove() - { - RemoveEntities(); - base.OnRemove(); - } - - private void SpawnEntities() - { - foreach (var proto in _prototypes) - { - var entity = _entMan.SpawnEntity(proto, _entMan.GetComponent(Owner).Coordinates); - _entity.Add(entity); - } - } - - private void RemoveEntities() - { - foreach (var entity in _entity) - { - _entMan.DeleteEntity(entity); - } - } - } -} diff --git a/Content.Server/Entry/IgnoredComponents.cs b/Content.Server/Entry/IgnoredComponents.cs index 2f21a42e96..3294b43030 100644 --- a/Content.Server/Entry/IgnoredComponents.cs +++ b/Content.Server/Entry/IgnoredComponents.cs @@ -7,14 +7,12 @@ namespace Content.Server.Entry "ConstructionGhost", "IconSmooth", "InteractionOutline", - "AnimationsTest", "ItemStatus", "Marker", "GuidebookControlsTest", "GuideHelp", "Clickable", "Icon", - "ClientEntitySpawner", "HandheldGPS", "CableVisualizer", "UIFragment", diff --git a/Content.Server/Power/Components/BaseNetConnectorComponent.cs b/Content.Server/Power/Components/BaseNetConnectorComponent.cs index ae85c43557..e452474a2d 100644 --- a/Content.Server/Power/Components/BaseNetConnectorComponent.cs +++ b/Content.Server/Power/Components/BaseNetConnectorComponent.cs @@ -25,21 +25,10 @@ namespace Content.Server.Power.Components public TNetType? Net { get => _net; set => SetNet(value); } private TNetType? _net; - [ViewVariables] - private bool _needsNet => _net != null; + [ViewVariables] public bool NeedsNet => _net != null; [DataField("node")] public string? NodeId { get; set; } - protected override void Initialize() - { - base.Initialize(); - - if (_needsNet) - { - TryFindAndSetNet(); - } - } - protected override void OnRemove() { ClearNet(); diff --git a/Content.Server/Power/EntitySystems/PowerNetConnectorSystem.cs b/Content.Server/Power/EntitySystems/PowerNetConnectorSystem.cs new file mode 100644 index 0000000000..cb8593adc1 --- /dev/null +++ b/Content.Server/Power/EntitySystems/PowerNetConnectorSystem.cs @@ -0,0 +1,49 @@ +using Content.Server.Power.Components; +using Content.Server.Power.NodeGroups; + +namespace Content.Server.Power.EntitySystems; + +public sealed class PowerNetConnectorSystem : EntitySystem +{ + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent(OnApcInit); + SubscribeLocalEvent(OnApcPowerProviderInit); + SubscribeLocalEvent(OnBatteryChargerInit); + SubscribeLocalEvent(OnBatteryDischargerInit); + } + + private void OnPowerSupplierInit(EntityUid uid, PowerSupplierComponent component, ComponentInit args) + { + BaseNetConnectorInit(component); + } + + private void OnBatteryDischargerInit(EntityUid uid, BatteryDischargerComponent component, ComponentInit args) + { + BaseNetConnectorInit(component); + } + + private void OnBatteryChargerInit(EntityUid uid, BatteryChargerComponent component, ComponentInit args) + { + BaseNetConnectorInit(component); + } + + private void OnApcPowerProviderInit(EntityUid uid, ApcPowerProviderComponent component, ComponentInit args) + { + BaseNetConnectorInit(component); + } + + private void OnApcInit(EntityUid uid, ApcComponent component, ComponentInit args) + { + BaseNetConnectorInit(component); + } + + public void BaseNetConnectorInit(BaseNetConnectorComponent component) + { + if (component.NeedsNet) + { + component.TryFindAndSetNet(); + } + } +} diff --git a/Content.Server/Power/EntitySystems/PowerNetSystem.cs b/Content.Server/Power/EntitySystems/PowerNetSystem.cs index 6f66e7ba53..b457a321a1 100644 --- a/Content.Server/Power/EntitySystems/PowerNetSystem.cs +++ b/Content.Server/Power/EntitySystems/PowerNetSystem.cs @@ -17,6 +17,7 @@ namespace Content.Server.Power.EntitySystems public sealed class PowerNetSystem : EntitySystem { [Dependency] private readonly AppearanceSystem _appearance = default!; + [Dependency] private readonly PowerNetConnectorSystem _powerNetConnector = default!; [Dependency] private readonly IParallelManager _parMan = default!; private readonly PowerState _powerState = new(); @@ -101,6 +102,7 @@ namespace Content.Server.Power.EntitySystems private void PowerConsumerInit(EntityUid uid, PowerConsumerComponent component, ComponentInit args) { + _powerNetConnector.BaseNetConnectorInit(component); AllocLoad(component.NetworkLoad); } @@ -121,6 +123,7 @@ namespace Content.Server.Power.EntitySystems private void PowerSupplierInit(EntityUid uid, PowerSupplierComponent component, ComponentInit args) { + _powerNetConnector.BaseNetConnectorInit(component); AllocSupply(component.NetworkSupply); } diff --git a/Content.Server/Spawners/Components/TimedSpawnerComponent.cs b/Content.Server/Spawners/Components/TimedSpawnerComponent.cs index e314a348c4..a79444d88f 100644 --- a/Content.Server/Spawners/Components/TimedSpawnerComponent.cs +++ b/Content.Server/Spawners/Components/TimedSpawnerComponent.cs @@ -9,8 +9,6 @@ namespace Content.Server.Spawners.Components [RegisterComponent] public sealed class TimedSpawnerComponent : Component, ISerializationHooks { - [Dependency] private readonly IRobustRandom _robustRandom = default!; - [ViewVariables(VVAccess.ReadWrite)] [DataField("prototypes", customTypeSerializer:typeof(PrototypeIdListSerializer))] public List Prototypes { get; set; } = new(); @@ -38,32 +36,5 @@ namespace Content.Server.Spawners.Components if (MinimumEntitiesSpawned > MaximumEntitiesSpawned) throw new ArgumentException("MaximumEntitiesSpawned can't be lower than MinimumEntitiesSpawned!"); } - - protected override void Initialize() - { - base.Initialize(); - SetupTimer(); - } - - private void SetupTimer() - { - TokenSource?.Cancel(); - TokenSource = new CancellationTokenSource(); - Owner.SpawnRepeatingTimer(TimeSpan.FromSeconds(IntervalSeconds), OnTimerFired, TokenSource.Token); - } - - private void OnTimerFired() - { - if (!_robustRandom.Prob(Chance)) - return; - - var number = _robustRandom.Next(MinimumEntitiesSpawned, MaximumEntitiesSpawned); - - for (int i = 0; i < number; i++) - { - var entity = _robustRandom.Pick(Prototypes); - IoCManager.Resolve().SpawnEntity(entity, IoCManager.Resolve().GetComponent(Owner).Coordinates); - } - } } } diff --git a/Content.Server/Spawners/EntitySystems/SpawnerSystem.cs b/Content.Server/Spawners/EntitySystems/SpawnerSystem.cs new file mode 100644 index 0000000000..1d0e4c397c --- /dev/null +++ b/Content.Server/Spawners/EntitySystems/SpawnerSystem.cs @@ -0,0 +1,38 @@ +using System.Threading; +using Content.Server.Spawners.Components; +using Robust.Shared.Random; + +namespace Content.Server.Spawners.EntitySystems; + +public sealed class SpawnerSystem : EntitySystem +{ + [Dependency] private readonly IRobustRandom _random = default!; + + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent(OnSpawnerInit); + } + + private void OnSpawnerInit(EntityUid uid, TimedSpawnerComponent component, ComponentInit args) + { + component.TokenSource?.Cancel(); + component.TokenSource = new CancellationTokenSource(); + uid.SpawnRepeatingTimer(TimeSpan.FromSeconds(component.IntervalSeconds), () => OnTimerFired(uid, component), component.TokenSource.Token); + } + + private void OnTimerFired(EntityUid uid, TimedSpawnerComponent component) + { + if (!_random.Prob(component.Chance)) + return; + + var number = _random.Next(component.MinimumEntitiesSpawned, component.MaximumEntitiesSpawned); + var coordinates = Transform(uid).Coordinates; + + for (var i = 0; i < number; i++) + { + var entity = _random.Pick(component.Prototypes); + Spawn(entity, coordinates); + } + } +} diff --git a/Resources/Prototypes/Entities/Effects/animations_test.yml b/Resources/Prototypes/Entities/Effects/animations_test.yml deleted file mode 100644 index ae7b67b2d5..0000000000 --- a/Resources/Prototypes/Entities/Effects/animations_test.yml +++ /dev/null @@ -1,9 +0,0 @@ -#- type: entity -# id: AnimationsTest -# components: -# - type: Sprite -# texture: Objects/Toolbox_b.png -# offset: 2, 0 -# noRot: false -# - type: AnimationPlayer -# - type: AnimationsTest