Vending visualizer update (#9962)

This commit is contained in:
Leon Friedrich
2022-07-26 12:35:36 +12:00
committed by GitHub
parent a21bcb4363
commit 5449e7875e
9 changed files with 113 additions and 78 deletions

View File

@@ -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<VendingMachineInventoryPrototype>))]
public string PackPrototypeId = string.Empty;
public TimeSpan AnimationDuration = TimeSpan.Zero;
[ViewVariables] public List<VendingMachineInventoryEntry> Inventory = new();
[ViewVariables] public List<VendingMachineInventoryEntry> EmaggedInventory = new();
[ViewVariables] public List<VendingMachineInventoryEntry> ContrabandInventory = new();
@@ -32,6 +37,7 @@ namespace Content.Shared.VendingMachines
public enum VendingMachineVisuals
{
VisualState,
Inventory,
}
[Serializable, NetSerializable]

View File

@@ -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<SharedVendingMachineComponent, ComponentInit>(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<string, uint>? entries,
InventoryType type,
SharedVendingMachineComponent? component = null)
{
if (!Resolve(uid, ref component) || entries == null)
{
return;
}
var inventory = new List<VendingMachineInventoryEntry>();
foreach (var (id, amount) in entries)
{
if (_prototypeManager.HasIndex<EntityPrototype>(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;
}
}
}

View File

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