From 189d49a51f02f2a1618f32a2c582594e7f12f2e5 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Mon, 8 Aug 2022 12:35:57 +1000 Subject: [PATCH] ItemMapper ECS (#9867) --- .../Storage/Systems/ItemMapperSystem.cs | 65 ++++++++++++++++ .../Storage/{ => Systems}/StorageSystem.cs | 6 +- .../Visualizers/MappedItemVisualizer.cs | 74 ------------------- .../Storage/EntitySystems/ItemMapperSystem.cs | 31 +------- Content.Shared/Blocking/BlockingUserSystem.cs | 3 - .../Storage/Components/ItemMapperComponent.cs | 13 +--- .../EntitySystems/SharedItemMapperSystem.cs | 31 +++++++- Content.Shared/Tag/TagComponent.cs | 2 +- .../Entities/Clothing/Belt/belts.yml | 30 ++------ .../Entities/Objects/Fun/crayons.yml | 3 - .../Entities/Objects/Misc/paper.yml | 2 - .../Objects/Specific/Janitorial/janitor.yml | 4 +- .../Entities/Objects/Vehicles/buckleable.yml | 4 +- 13 files changed, 112 insertions(+), 156 deletions(-) create mode 100644 Content.Client/Storage/Systems/ItemMapperSystem.cs rename Content.Client/Storage/{ => Systems}/StorageSystem.cs (91%) delete mode 100644 Content.Client/Storage/Visualizers/MappedItemVisualizer.cs diff --git a/Content.Client/Storage/Systems/ItemMapperSystem.cs b/Content.Client/Storage/Systems/ItemMapperSystem.cs new file mode 100644 index 0000000000..421713ee8e --- /dev/null +++ b/Content.Client/Storage/Systems/ItemMapperSystem.cs @@ -0,0 +1,65 @@ +using System.Linq; +using Content.Shared.Storage.Components; +using Content.Shared.Storage.EntitySystems; +using Robust.Client.GameObjects; +using Robust.Shared.Utility; + +namespace Content.Client.Storage.Systems; + +public sealed class ItemMapperSystem : SharedItemMapperSystem +{ + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent(OnStartup); + SubscribeLocalEvent(OnAppearance); + } + + private void OnStartup(EntityUid uid, ItemMapperComponent component, ComponentStartup args) + { + if (TryComp(uid, out var sprite)) + { + component.RSIPath ??= sprite.BaseRSI!.Path!; + } + } + + private void OnAppearance(EntityUid uid, ItemMapperComponent component, ref AppearanceChangeEvent args) + { + if (TryComp(component.Owner, out var spriteComponent)) + { + if (component.SpriteLayers.Count == 0) + { + InitLayers(component, spriteComponent, args.Component); + } + + EnableLayers(component, spriteComponent, args.Component); + } + } + + private void InitLayers(ItemMapperComponent component, SpriteComponent spriteComponent, AppearanceComponent appearance) + { + if (!appearance.TryGetData(StorageMapVisuals.InitLayers, out var wrapper)) + return; + + component.SpriteLayers.AddRange(wrapper.QueuedEntities); + + foreach (var sprite in component.SpriteLayers) + { + spriteComponent.LayerMapReserveBlank(sprite); + spriteComponent.LayerSetSprite(sprite, new SpriteSpecifier.Rsi(component.RSIPath!, sprite)); + spriteComponent.LayerSetVisible(sprite, false); + } + } + + private void EnableLayers(ItemMapperComponent component, SpriteComponent spriteComponent, AppearanceComponent appearance) + { + if (!appearance.TryGetData(StorageMapVisuals.LayerChanged, out var wrapper)) + return; + + foreach (var layerName in component.SpriteLayers) + { + var show = wrapper.QueuedEntities.Contains(layerName); + spriteComponent.LayerSetVisible(layerName, show); + } + } +} diff --git a/Content.Client/Storage/StorageSystem.cs b/Content.Client/Storage/Systems/StorageSystem.cs similarity index 91% rename from Content.Client/Storage/StorageSystem.cs rename to Content.Client/Storage/Systems/StorageSystem.cs index ed60a46188..37dc7a94ec 100644 --- a/Content.Client/Storage/StorageSystem.cs +++ b/Content.Client/Storage/Systems/StorageSystem.cs @@ -1,7 +1,7 @@ -using Content.Shared.Storage; -using Content.Client.Animations; +using Content.Client.Animations; +using Content.Shared.Storage; -namespace Content.Client.Storage; +namespace Content.Client.Storage.Systems; // TODO kill this is all horrid. public sealed class StorageSystem : EntitySystem diff --git a/Content.Client/Storage/Visualizers/MappedItemVisualizer.cs b/Content.Client/Storage/Visualizers/MappedItemVisualizer.cs deleted file mode 100644 index 63ac715005..0000000000 --- a/Content.Client/Storage/Visualizers/MappedItemVisualizer.cs +++ /dev/null @@ -1,74 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Content.Shared.Storage.Components; -using JetBrains.Annotations; -using Robust.Client.GameObjects; -using Robust.Shared.GameObjects; -using Robust.Shared.IoC; -using Robust.Shared.Serialization.Manager.Attributes; -using Robust.Shared.Utility; - -namespace Content.Client.Storage.Visualizers -{ - [UsedImplicitly] - public sealed class MappedItemVisualizer : AppearanceVisualizer - { - [DataField("sprite")] private ResourcePath? _rsiPath; - private List _spriteLayers = new(); - - public override void InitializeEntity(EntityUid entity) - { - base.InitializeEntity(entity); - - if (IoCManager.Resolve().TryGetComponent(entity, out var spriteComponent)) - { - _rsiPath ??= spriteComponent.BaseRSI!.Path!; - } - } - - - public override void OnChangeData(AppearanceComponent component) - { - base.OnChangeData(component); - - var entities = IoCManager.Resolve(); - if (entities.TryGetComponent(component.Owner, out ISpriteComponent? spriteComponent)) - { - if (_spriteLayers.Count == 0) - { - InitLayers(spriteComponent, component); - } - - EnableLayers(spriteComponent, component); - } - } - - private void InitLayers(ISpriteComponent spriteComponent, AppearanceComponent component) - { - if (!component.TryGetData(StorageMapVisuals.InitLayers, out var wrapper)) - return; - - _spriteLayers.AddRange(wrapper.QueuedEntities); - - foreach (var sprite in _spriteLayers) - { - spriteComponent.LayerMapReserveBlank(sprite); - spriteComponent.LayerSetSprite(sprite, new SpriteSpecifier.Rsi(_rsiPath!, sprite)); - spriteComponent.LayerSetVisible(sprite, false); - } - } - - private void EnableLayers(ISpriteComponent spriteComponent, AppearanceComponent component) - { - if (!component.TryGetData(StorageMapVisuals.LayerChanged, out var wrapper)) - return; - - - foreach (var layerName in _spriteLayers) - { - var show = wrapper.QueuedEntities.Contains(layerName); - spriteComponent.LayerSetVisible(layerName, show); - } - } - } -} diff --git a/Content.Server/Storage/EntitySystems/ItemMapperSystem.cs b/Content.Server/Storage/EntitySystems/ItemMapperSystem.cs index 6662213215..fb59289609 100644 --- a/Content.Server/Storage/EntitySystems/ItemMapperSystem.cs +++ b/Content.Server/Storage/EntitySystems/ItemMapperSystem.cs @@ -1,36 +1,9 @@ -using System.Linq; -using Content.Shared.Storage.Components; -using Content.Shared.Storage.EntitySystems; +using Content.Shared.Storage.EntitySystems; using JetBrains.Annotations; -using Robust.Shared.Containers; namespace Content.Server.Storage.EntitySystems { /// [UsedImplicitly] - public sealed class ItemMapperSystem : SharedItemMapperSystem - { - [Dependency] private readonly SharedContainerSystem _containerSystem = default!; - - protected override bool TryGetLayers(ContainerModifiedMessage msg, - ItemMapperComponent itemMapper, - out IReadOnlyList showLayers) - { - var containedLayers = _containerSystem.GetAllContainers(msg.Container.Owner) - .SelectMany(cont => cont.ContainedEntities).ToArray(); - - var list = new List(); - foreach (var mapLayerData in itemMapper.MapLayers.Values) - { - var count = containedLayers.Count(uid => mapLayerData.ServerWhitelist.IsValid(uid)); - if (count >= mapLayerData.MinCount && count <= mapLayerData.MaxCount) - { - list.Add(mapLayerData.Layer); - } - } - - showLayers = list; - return true; - } - } + public sealed class ItemMapperSystem : SharedItemMapperSystem {} } diff --git a/Content.Shared/Blocking/BlockingUserSystem.cs b/Content.Shared/Blocking/BlockingUserSystem.cs index 7cc6ad7025..6bfaa3bbbb 100644 --- a/Content.Shared/Blocking/BlockingUserSystem.cs +++ b/Content.Shared/Blocking/BlockingUserSystem.cs @@ -1,8 +1,6 @@ using Content.Shared.Audio; -using Content.Shared.Buckle.Components; using Content.Shared.Damage; using Content.Shared.Damage.Prototypes; -using Content.Shared.Hands.EntitySystems; using Robust.Shared.Audio; using Robust.Shared.Containers; using Robust.Shared.Player; @@ -12,7 +10,6 @@ namespace Content.Shared.Blocking; public sealed class BlockingUserSystem : EntitySystem { - [Dependency] private readonly SharedHandsSystem _handsSystem = default!; [Dependency] private readonly IPrototypeManager _proto = default!; [Dependency] private readonly BlockingSystem _blockingSystem = default!; diff --git a/Content.Shared/Storage/Components/ItemMapperComponent.cs b/Content.Shared/Storage/Components/ItemMapperComponent.cs index 459ff73a4a..86d1d879cc 100644 --- a/Content.Shared/Storage/Components/ItemMapperComponent.cs +++ b/Content.Shared/Storage/Components/ItemMapperComponent.cs @@ -1,5 +1,5 @@ using Content.Shared.Storage.EntitySystems; -using Robust.Shared.Serialization; +using Robust.Shared.Utility; namespace Content.Shared.Storage.Components { @@ -54,17 +54,12 @@ namespace Content.Shared.Storage.Components /// [RegisterComponent] [Access(typeof(SharedItemMapperSystem))] - public sealed class ItemMapperComponent : Component, ISerializationHooks + public sealed class ItemMapperComponent : Component { [DataField("mapLayers")] public readonly Dictionary MapLayers = new(); - void ISerializationHooks.AfterDeserialization() - { - foreach (var (layerName, val) in MapLayers) - { - val.Layer = layerName; - } - } + [DataField("sprite")] public ResourcePath? RSIPath; + public readonly List SpriteLayers = new(); } } diff --git a/Content.Shared/Storage/EntitySystems/SharedItemMapperSystem.cs b/Content.Shared/Storage/EntitySystems/SharedItemMapperSystem.cs index 55d8fe17c8..c653039254 100644 --- a/Content.Shared/Storage/EntitySystems/SharedItemMapperSystem.cs +++ b/Content.Shared/Storage/EntitySystems/SharedItemMapperSystem.cs @@ -1,4 +1,5 @@ -using Content.Shared.Storage.Components; +using System.Linq; +using Content.Shared.Storage.Components; using JetBrains.Annotations; using Robust.Shared.Containers; @@ -12,6 +13,8 @@ namespace Content.Shared.Storage.EntitySystems [UsedImplicitly] public abstract class SharedItemMapperSystem : EntitySystem { + [Dependency] private readonly SharedContainerSystem _container = default!; + /// public override void Initialize() { @@ -23,6 +26,11 @@ namespace Content.Shared.Storage.EntitySystems private void InitLayers(EntityUid uid, ItemMapperComponent component, ComponentInit args) { + foreach (var (layerName, val) in component.MapLayers) + { + val.Layer = layerName; + } + if (EntityManager.TryGetComponent(component.Owner, out AppearanceComponent? appearanceComponent)) { var list = new List(component.MapLayers.Keys); @@ -61,8 +69,25 @@ namespace Content.Shared.Storage.EntitySystems /// /// list of layers that should be visible /// false if msg.Container.Owner is not a storage, true otherwise. - protected abstract bool TryGetLayers(ContainerModifiedMessage msg, + private bool TryGetLayers(ContainerModifiedMessage msg, ItemMapperComponent itemMapper, - out IReadOnlyList containedLayers); + out IReadOnlyList showLayers) + { + var containedLayers = _container.GetAllContainers(msg.Container.Owner) + .SelectMany(cont => cont.ContainedEntities).ToArray(); + + var list = new List(); + foreach (var mapLayerData in itemMapper.MapLayers.Values) + { + var count = containedLayers.Count(uid => mapLayerData.ServerWhitelist.IsValid(uid)); + if (count >= mapLayerData.MinCount && count <= mapLayerData.MaxCount) + { + list.Add(mapLayerData.Layer); + } + } + + showLayers = list; + return true; + } } } diff --git a/Content.Shared/Tag/TagComponent.cs b/Content.Shared/Tag/TagComponent.cs index fc303c0039..2f34357de3 100644 --- a/Content.Shared/Tag/TagComponent.cs +++ b/Content.Shared/Tag/TagComponent.cs @@ -4,7 +4,7 @@ using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototy namespace Content.Shared.Tag { [RegisterComponent, Access(typeof(TagSystem))] - public sealed class TagComponent : Component, ISerializationHooks + public sealed class TagComponent : Component { [ViewVariables] [DataField("tags", customTypeSerializer: typeof(PrototypeIdHashSetSerializer))] diff --git a/Resources/Prototypes/Entities/Clothing/Belt/belts.yml b/Resources/Prototypes/Entities/Clothing/Belt/belts.yml index db1b8026fa..8db353aa63 100644 --- a/Resources/Prototypes/Entities/Clothing/Belt/belts.yml +++ b/Resources/Prototypes/Entities/Clothing/Belt/belts.yml @@ -57,10 +57,8 @@ whitelist: components: - SignalLinker + sprite: Clothing/Belt/belt_overlay.rsi - type: Appearance - visuals: - - type: MappedItemVisualizer - sprite: Clothing/Belt/belt_overlay.rsi - type: entity parent: ClothingBeltStorageBase @@ -132,10 +130,8 @@ whitelist: tags: - Wrench + sprite: Clothing/Belt/belt_overlay.rsi - type: Appearance - visuals: - - type: MappedItemVisualizer - sprite: Clothing/Belt/belt_overlay.rsi - type: entity parent: ClothingBeltStorageBase @@ -169,10 +165,8 @@ whitelist: components: - Stunbaton + sprite: Clothing/Belt/belt_overlay.rsi - type: Appearance - visuals: - - type: MappedItemVisualizer - sprite: Clothing/Belt/belt_overlay.rsi - type: entity parent: ClothingBeltStorageBase @@ -210,10 +204,8 @@ whitelist: tags: - Wrench + sprite: Clothing/Belt/belt_overlay.rsi - type: Appearance - visuals: - - type: MappedItemVisualizer - sprite: Clothing/Belt/belt_overlay.rsi - type: entity parent: ClothingBeltStorageBase @@ -272,10 +264,8 @@ whitelist: tags: - Wrench + sprite: Clothing/Belt/belt_overlay.rsi - type: Appearance - visuals: - - type: MappedItemVisualizer - sprite: Clothing/Belt/belt_overlay.rsi - type: entity parent: ClothingBeltStorageBase @@ -329,10 +319,8 @@ whitelist: tags: - Bottle + sprite: Clothing/Belt/belt_overlay.rsi - type: Appearance - visuals: - - type: MappedItemVisualizer - sprite: Clothing/Belt/belt_overlay.rsi - type: entity parent: ClothingBeltStorageBase @@ -364,10 +352,8 @@ whitelist: components: - Stunbaton + sprite: Clothing/Belt/belt_overlay.rsi - type: Appearance - visuals: - - type: MappedItemVisualizer - sprite: Clothing/Belt/belt_overlay.rsi - type: entity parent: ClothingBeltStorageBase @@ -392,8 +378,6 @@ tags: - CaptainSabre - type: Appearance - visuals: - - type: MappedItemVisualizer # Belts without visualizers diff --git a/Resources/Prototypes/Entities/Objects/Fun/crayons.yml b/Resources/Prototypes/Entities/Objects/Fun/crayons.yml index f7c2f2eaa2..0085148ed7 100644 --- a/Resources/Prototypes/Entities/Objects/Fun/crayons.yml +++ b/Resources/Prototypes/Entities/Objects/Fun/crayons.yml @@ -313,7 +313,4 @@ whitelist: tags: - CrayonYellow - - type: Appearance - visuals: - - type: MappedItemVisualizer diff --git a/Resources/Prototypes/Entities/Objects/Misc/paper.yml b/Resources/Prototypes/Entities/Objects/Misc/paper.yml index 5b0d6ebe41..fed707f690 100644 --- a/Resources/Prototypes/Entities/Objects/Misc/paper.yml +++ b/Resources/Prototypes/Entities/Objects/Misc/paper.yml @@ -164,8 +164,6 @@ tags: - Document - type: Appearance - visuals: - - type: MappedItemVisualizer - type: Tag tags: - Folder diff --git a/Resources/Prototypes/Entities/Objects/Specific/Janitorial/janitor.yml b/Resources/Prototypes/Entities/Objects/Specific/Janitorial/janitor.yml index 7971baa74d..39a6eef8b1 100644 --- a/Resources/Prototypes/Entities/Objects/Specific/Janitorial/janitor.yml +++ b/Resources/Prototypes/Entities/Objects/Specific/Janitorial/janitor.yml @@ -207,10 +207,8 @@ whitelist: tags: - WetFloorSign + sprite: Objects/Specific/Janitorial/janitorial_cart.rsi - type: Appearance - visuals: - - type: MappedItemVisualizer - sprite: Objects/Specific/Janitorial/janitorial_cart.rsi - type: SolutionContainerVisuals maxFillLevels: 3 fillBaseName: cart_water- diff --git a/Resources/Prototypes/Entities/Objects/Vehicles/buckleable.yml b/Resources/Prototypes/Entities/Objects/Vehicles/buckleable.yml index 14f63590c0..dd69e6dcef 100644 --- a/Resources/Prototypes/Entities/Objects/Vehicles/buckleable.yml +++ b/Resources/Prototypes/Entities/Objects/Vehicles/buckleable.yml @@ -148,10 +148,8 @@ whitelist: tags: - TrashBag + sprite: Objects/Vehicles/pussywagon.rsi - type: Appearance - visuals: - - type: MappedItemVisualizer - sprite: Objects/Vehicles/pussywagon.rsi - type: entity id: VehiclePussyWagonDestroyed