From 5449e7875e47e648aaa34347529f6905fbe12e2a Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Tue, 26 Jul 2022 12:35:36 +1200 Subject: [PATCH] Vending visualizer update (#9962) --- Content.Client/Entry/EntryPoint.cs | 1 - .../UI/VendingMachineVisualizer.cs | 16 +++- .../VendingMachineComponent.cs | 10 +++ .../VendingMachines/VendingMachineSystem.cs | 7 ++ .../VendingMachineComponent.cs | 8 +- .../VendingMachines/VendingMachineSystem.cs | 73 ++----------------- .../SharedVendingMachineComponent.cs | 10 ++- .../SharedVendingMachineSystem.cs | 65 +++++++++++++++++ .../VendingMachineInventoryPrototype.cs | 1 + 9 files changed, 113 insertions(+), 78 deletions(-) create mode 100644 Content.Client/VendingMachines/VendingMachineComponent.cs create mode 100644 Content.Client/VendingMachines/VendingMachineSystem.cs create mode 100644 Content.Shared/VendingMachines/SharedVendingMachineSystem.cs diff --git a/Content.Client/Entry/EntryPoint.cs b/Content.Client/Entry/EntryPoint.cs index 3a37484c35..79a4f34aee 100644 --- a/Content.Client/Entry/EntryPoint.cs +++ b/Content.Client/Entry/EntryPoint.cs @@ -73,7 +73,6 @@ namespace Content.Client.Entry // Do not add to these, they are legacy. factory.RegisterClass(); factory.RegisterClass(); - factory.RegisterClass(); factory.RegisterClass(); factory.RegisterClass(); factory.RegisterClass(); diff --git a/Content.Client/VendingMachines/UI/VendingMachineVisualizer.cs b/Content.Client/VendingMachines/UI/VendingMachineVisualizer.cs index 70167300b4..755a244cca 100644 --- a/Content.Client/VendingMachines/UI/VendingMachineVisualizer.cs +++ b/Content.Client/VendingMachines/UI/VendingMachineVisualizer.cs @@ -1,10 +1,13 @@ using System; using System.Collections.Generic; +using Content.Shared.VendingMachines; using JetBrains.Annotations; using Robust.Client.Animations; using Robust.Client.GameObjects; +using Robust.Client.ResourceManagement; using Robust.Shared.GameObjects; using Robust.Shared.IoC; +using Robust.Shared.Prototypes; using Robust.Shared.Serialization; using Robust.Shared.Serialization.Manager.Attributes; using static Content.Shared.VendingMachines.SharedVendingMachineComponent; @@ -139,7 +142,18 @@ namespace Content.Client.VendingMachines.UI base.OnChangeData(component); var entMan = IoCManager.Resolve(); - var sprite = entMan.GetComponent(component.Owner); + var sprite = entMan.GetComponent(component.Owner); + + // TODO when moving to a system visualizer, re work how this is done + // Currently this only gets called during init, so unless it NEEEDS to be configurable, just make this party of the entity prototype. + if (component.TryGetData(VendingMachineVisuals.Inventory, out string? invId) && + IoCManager.Resolve().TryIndex(invId, out VendingMachineInventoryPrototype? prototype) && + IoCManager.Resolve().TryGetResource( + SharedSpriteComponent.TextureRoot / $"Structures/Machines/VendingMachines/{prototype.SpriteName}.rsi", out var res)) + { + sprite.BaseRSI = res.RSI; + } + var animPlayer = entMan.GetComponent(component.Owner); if (!component.TryGetData(VendingMachineVisuals.VisualState, out VendingMachineVisualState state)) { diff --git a/Content.Client/VendingMachines/VendingMachineComponent.cs b/Content.Client/VendingMachines/VendingMachineComponent.cs new file mode 100644 index 0000000000..f7c16584a3 --- /dev/null +++ b/Content.Client/VendingMachines/VendingMachineComponent.cs @@ -0,0 +1,10 @@ +using Content.Shared.VendingMachines; + +namespace Content.Client.VendingMachines; + +[RegisterComponent] +[ComponentReference(typeof(SharedVendingMachineComponent))] +public sealed class VendingMachineComponent : SharedVendingMachineComponent +{ + +} diff --git a/Content.Client/VendingMachines/VendingMachineSystem.cs b/Content.Client/VendingMachines/VendingMachineSystem.cs new file mode 100644 index 0000000000..c08f5e10d2 --- /dev/null +++ b/Content.Client/VendingMachines/VendingMachineSystem.cs @@ -0,0 +1,7 @@ +using Content.Shared.VendingMachines; + +namespace Content.Client.VendingMachines; + +public sealed class VendingMachineSystem : SharedVendingMachineSystem +{ +} diff --git a/Content.Server/VendingMachines/VendingMachineComponent.cs b/Content.Server/VendingMachines/VendingMachineComponent.cs index 2db45ad428..24f536a0a5 100644 --- a/Content.Server/VendingMachines/VendingMachineComponent.cs +++ b/Content.Server/VendingMachines/VendingMachineComponent.cs @@ -8,17 +8,11 @@ using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototy namespace Content.Server.VendingMachines { [RegisterComponent] + [ComponentReference(typeof(SharedVendingMachineComponent))] public sealed class VendingMachineComponent : SharedVendingMachineComponent { public bool Ejecting; - public TimeSpan AnimationDuration = TimeSpan.Zero; - - [ViewVariables] [DataField("pack", customTypeSerializer:typeof(PrototypeIdSerializer))] - public string PackPrototypeId = string.Empty; - - public string SpriteName = ""; - public bool Broken; /// diff --git a/Content.Server/VendingMachines/VendingMachineSystem.cs b/Content.Server/VendingMachines/VendingMachineSystem.cs index 90b1e51f41..3d7b108da7 100644 --- a/Content.Server/VendingMachines/VendingMachineSystem.cs +++ b/Content.Server/VendingMachines/VendingMachineSystem.cs @@ -20,7 +20,7 @@ using static Content.Shared.VendingMachines.SharedVendingMachineComponent; namespace Content.Server.VendingMachines { - public sealed class VendingMachineSystem : EntitySystem + public sealed class VendingMachineSystem : SharedVendingMachineSystem { [Dependency] private readonly IRobustRandom _random = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!; @@ -32,7 +32,7 @@ namespace Content.Server.VendingMachines public override void Initialize() { base.Initialize(); - SubscribeLocalEvent(OnComponentInit); + SubscribeLocalEvent(OnPowerChanged); SubscribeLocalEvent(OnInventoryRequestMessage); SubscribeLocalEvent(OnInventoryEjectMessage); @@ -43,9 +43,11 @@ namespace Content.Server.VendingMachines SubscribeLocalEvent(OnSelfDispense); } - private void OnComponentInit(EntityUid uid, VendingMachineComponent component, ComponentInit args) + protected override void OnComponentInit(EntityUid uid, SharedVendingMachineComponent sharedComponent, ComponentInit args) { - base.Initialize(); + base.OnComponentInit(uid, sharedComponent, args); + + var component = (VendingMachineComponent) sharedComponent; if (TryComp(component.Owner, out var receiver)) { @@ -57,8 +59,6 @@ namespace Content.Server.VendingMachines var action = new InstantAction(_prototypeManager.Index(component.Action)); _action.AddAction(uid, action, uid); } - - InitializeFromPrototype(uid, component); } private void OnInventoryRequestMessage(EntityUid uid, VendingMachineComponent component, InventorySyncRequestMessage args) @@ -123,67 +123,6 @@ namespace Content.Server.VendingMachines EjectRandom(uid, true, component); } - public void InitializeFromPrototype(EntityUid uid, VendingMachineComponent? vendComponent = null) - { - if (!Resolve(uid, ref vendComponent)) - return; - - if (string.IsNullOrEmpty(vendComponent.PackPrototypeId)) { return; } - - if (!_prototypeManager.TryIndex(vendComponent.PackPrototypeId, out VendingMachineInventoryPrototype? packPrototype)) - { - return; - } - - MetaData(uid).EntityName = packPrototype.Name; - vendComponent.AnimationDuration = TimeSpan.FromSeconds(packPrototype.AnimationDuration); - vendComponent.SpriteName = packPrototype.SpriteName; - if (!string.IsNullOrEmpty(vendComponent.SpriteName)) - { - if (TryComp(vendComponent.Owner, out var spriteComp)) { - const string vendingMachineRSIPath = "Structures/Machines/VendingMachines/{0}.rsi"; - spriteComp.BaseRSIPath = string.Format(vendingMachineRSIPath, vendComponent.SpriteName); - } - } - - AddInventoryFromPrototype(uid, packPrototype.StartingInventory, InventoryType.Regular, vendComponent); - AddInventoryFromPrototype(uid, packPrototype.EmaggedInventory, InventoryType.Emagged, vendComponent); - AddInventoryFromPrototype(uid, packPrototype.ContrabandInventory, InventoryType.Contraband, vendComponent); - } - - private void AddInventoryFromPrototype(EntityUid uid, Dictionary? entries, - InventoryType type, - VendingMachineComponent? component = null) - { - if (!Resolve(uid, ref component) || entries == null) - { - return; - } - - var inventory = new List(); - - foreach (var (id, amount) in entries) - { - if (_prototypeManager.HasIndex(id)) - { - inventory.Add(new VendingMachineInventoryEntry(type, id, amount)); - } - } - - switch (type) - { - case InventoryType.Regular: - component.Inventory.AddRange(inventory); - break; - case InventoryType.Emagged: - component.EmaggedInventory.AddRange(inventory); - break; - case InventoryType.Contraband: - component.ContrabandInventory.AddRange(inventory); - break; - } - } - public void Deny(EntityUid uid, VendingMachineComponent? vendComponent = null) { if (!Resolve(uid, ref vendComponent)) diff --git a/Content.Shared/VendingMachines/SharedVendingMachineComponent.cs b/Content.Shared/VendingMachines/SharedVendingMachineComponent.cs index 6084ec0f76..f5e6e82961 100644 --- a/Content.Shared/VendingMachines/SharedVendingMachineComponent.cs +++ b/Content.Shared/VendingMachines/SharedVendingMachineComponent.cs @@ -1,13 +1,18 @@ using Content.Shared.Actions; using Robust.Shared.GameStates; using Robust.Shared.Serialization; +using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; namespace Content.Shared.VendingMachines { - [Virtual] [NetworkedComponent()] - public class SharedVendingMachineComponent : Component + public abstract class SharedVendingMachineComponent : Component { + [DataField("pack", customTypeSerializer: typeof(PrototypeIdSerializer))] + public string PackPrototypeId = string.Empty; + + public TimeSpan AnimationDuration = TimeSpan.Zero; + [ViewVariables] public List Inventory = new(); [ViewVariables] public List EmaggedInventory = new(); [ViewVariables] public List ContrabandInventory = new(); @@ -32,6 +37,7 @@ namespace Content.Shared.VendingMachines public enum VendingMachineVisuals { VisualState, + Inventory, } [Serializable, NetSerializable] diff --git a/Content.Shared/VendingMachines/SharedVendingMachineSystem.cs b/Content.Shared/VendingMachines/SharedVendingMachineSystem.cs new file mode 100644 index 0000000000..98bd744413 --- /dev/null +++ b/Content.Shared/VendingMachines/SharedVendingMachineSystem.cs @@ -0,0 +1,65 @@ +using Robust.Shared.Prototypes; +using static Content.Shared.VendingMachines.SharedVendingMachineComponent; + +namespace Content.Shared.VendingMachines; + +public abstract class SharedVendingMachineSystem : EntitySystem +{ + [Dependency] private readonly IPrototypeManager _prototypeManager = default!; + + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent(OnComponentInit);; + } + + protected virtual void OnComponentInit(EntityUid uid, SharedVendingMachineComponent component, ComponentInit args) + { + if (!_prototypeManager.TryIndex(component.PackPrototypeId, out VendingMachineInventoryPrototype? packPrototype)) + return; + + MetaData(uid).EntityName = packPrototype.Name; + component.AnimationDuration = TimeSpan.FromSeconds(packPrototype.AnimationDuration); + + if (TryComp(component.Owner, out AppearanceComponent? appearance)) + appearance.SetData(VendingMachineVisuals.Inventory, component.PackPrototypeId); + + AddInventoryFromPrototype(uid, packPrototype.StartingInventory, InventoryType.Regular, component); + AddInventoryFromPrototype(uid, packPrototype.EmaggedInventory, InventoryType.Emagged, component); + AddInventoryFromPrototype(uid, packPrototype.ContrabandInventory, InventoryType.Contraband, component); + } + + private void AddInventoryFromPrototype(EntityUid uid, Dictionary? entries, + InventoryType type, + SharedVendingMachineComponent? component = null) + { + if (!Resolve(uid, ref component) || entries == null) + { + return; + } + + var inventory = new List(); + + foreach (var (id, amount) in entries) + { + if (_prototypeManager.HasIndex(id)) + { + inventory.Add(new VendingMachineInventoryEntry(type, id, amount)); + } + } + + switch (type) + { + case InventoryType.Regular: + component.Inventory.AddRange(inventory); + break; + case InventoryType.Emagged: + component.EmaggedInventory.AddRange(inventory); + break; + case InventoryType.Contraband: + component.ContrabandInventory.AddRange(inventory); + break; + } + } +} + diff --git a/Content.Shared/VendingMachines/VendingMachineInventoryPrototype.cs b/Content.Shared/VendingMachines/VendingMachineInventoryPrototype.cs index 331fee3adb..4e1ac78cc0 100644 --- a/Content.Shared/VendingMachines/VendingMachineInventoryPrototype.cs +++ b/Content.Shared/VendingMachines/VendingMachineInventoryPrototype.cs @@ -16,6 +16,7 @@ namespace Content.Shared.VendingMachines [DataField("animationDuration")] public double AnimationDuration { get; } + // TODO make this a proper sprite specifier for yaml linting. [DataField("spriteName")] public string SpriteName { get; } = string.Empty;