Clean up vending machines and port their visualizer (#10465)

This commit is contained in:
Andreas Kämper
2022-08-31 14:12:09 +02:00
committed by GitHub
parent 6b0e03e0d7
commit 42f3155c85
62 changed files with 873 additions and 850 deletions

View File

@@ -1,4 +1,5 @@
using Robust.Shared.Prototypes;
using System.Linq;
using static Content.Shared.VendingMachines.SharedVendingMachineComponent;
namespace Content.Shared.VendingMachines;
@@ -18,17 +19,43 @@ public abstract class SharedVendingMachineSystem : EntitySystem
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);
}
/// <summary>
/// Returns all of the vending machine's inventory. Only includes emagged and contraband inventories if
/// <see cref="SharedVendingMachineComponent.Emagged"/> and <see cref="SharedVendingMachineComponent.Contraband"/>
/// are <c>true</c> respectively.
/// </summary>
/// <param name="uid"></param>
/// <param name="component"></param>
/// <returns></returns>
public List<VendingMachineInventoryEntry> GetAllInventory(EntityUid uid, SharedVendingMachineComponent? component = null)
{
if (!Resolve(uid, ref component))
return new();
var inventory = new List<VendingMachineInventoryEntry>(component.Inventory.Values);
if (component.Emagged)
inventory.AddRange(component.EmaggedInventory.Values);
if (component.Contraband)
inventory.AddRange(component.ContrabandInventory.Values);
return inventory;
}
public List<VendingMachineInventoryEntry> GetAvailableInventory(EntityUid uid, SharedVendingMachineComponent? component = null)
{
if (!Resolve(uid, ref component))
return new();
return GetAllInventory(uid, component).Where(_ => _.Amount > 0).ToList();
}
private void AddInventoryFromPrototype(EntityUid uid, Dictionary<string, uint>? entries,
InventoryType type,
SharedVendingMachineComponent? component = null)
@@ -38,26 +65,26 @@ public abstract class SharedVendingMachineSystem : EntitySystem
return;
}
var inventory = new List<VendingMachineInventoryEntry>();
var inventory = new Dictionary<string, VendingMachineInventoryEntry>();
foreach (var (id, amount) in entries)
{
if (_prototypeManager.HasIndex<EntityPrototype>(id))
{
inventory.Add(new VendingMachineInventoryEntry(type, id, amount));
inventory.Add(id, new VendingMachineInventoryEntry(type, id, amount));
}
}
switch (type)
{
case InventoryType.Regular:
component.Inventory.AddRange(inventory);
component.Inventory = inventory;
break;
case InventoryType.Emagged:
component.EmaggedInventory.AddRange(inventory);
component.EmaggedInventory = inventory;
break;
case InventoryType.Contraband:
component.ContrabandInventory.AddRange(inventory);
component.ContrabandInventory = inventory;
break;
}
}