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

@@ -8,112 +8,79 @@ namespace Content.Shared.VendingMachines
[NetworkedComponent()]
public abstract class SharedVendingMachineComponent : Component
{
/// <summary>
/// PrototypeID for the vending machine's inventory, see <see cref="VendingMachineInventoryPrototype"/>
/// </summary>
[DataField("pack", customTypeSerializer: typeof(PrototypeIdSerializer<VendingMachineInventoryPrototype>))]
public string PackPrototypeId = string.Empty;
public TimeSpan AnimationDuration = TimeSpan.Zero;
/// <summary>
/// Used by the server to determine how long the vending machine stays in the "Deny" state.
/// Used by the client to determine how long the deny animation should be played.
/// </summary>
[DataField("denyDelay")]
public float DenyDelay = 2.0f;
[ViewVariables] public List<VendingMachineInventoryEntry> Inventory = new();
[ViewVariables] public List<VendingMachineInventoryEntry> EmaggedInventory = new();
[ViewVariables] public List<VendingMachineInventoryEntry> ContrabandInventory = new();
/// <summary>
/// Used by the server to determine how long the vending machine stays in the "Eject" state.
/// The selected item is dispensed afer this delay.
/// Used by the client to determine how long the deny animation should be played.
/// </summary>
[DataField("ejectDelay")]
public float EjectDelay = 1.2f;
public List<VendingMachineInventoryEntry> AllInventory
{
get
{
var inventory = new List<VendingMachineInventoryEntry>(Inventory);
[ViewVariables]
public Dictionary<string, VendingMachineInventoryEntry> Inventory = new();
if (Emagged) inventory.AddRange(EmaggedInventory);
if (Contraband) inventory.AddRange(ContrabandInventory);
[ViewVariables]
public Dictionary<string, VendingMachineInventoryEntry> EmaggedInventory = new();
return inventory;
}
}
[ViewVariables]
public Dictionary<string, VendingMachineInventoryEntry> ContrabandInventory = new();
public bool Emagged;
public bool Contraband;
}
[Serializable, NetSerializable]
public enum VendingMachineVisuals
[Serializable, NetSerializable]
public sealed class VendingMachineInventoryEntry
{
[ViewVariables(VVAccess.ReadWrite)]
public InventoryType Type;
[ViewVariables(VVAccess.ReadWrite)]
public string ID;
[ViewVariables(VVAccess.ReadWrite)]
public uint Amount;
public VendingMachineInventoryEntry(InventoryType type, string id, uint amount)
{
VisualState,
Inventory,
Type = type;
ID = id;
Amount = amount;
}
}
[Serializable, NetSerializable]
public enum VendingMachineVisualState
{
Normal,
Off,
Broken,
Eject,
Deny,
}
[Serializable, NetSerializable]
public enum InventoryType : byte
{
Regular,
Emagged,
Contraband
}
[Serializable, NetSerializable]
public sealed class VendingMachineEjectMessage : BoundUserInterfaceMessage
{
public readonly InventoryType Type;
public readonly string ID;
public VendingMachineEjectMessage(InventoryType type, string id)
{
Type = type;
ID = id;
}
}
[Serializable, NetSerializable]
public enum VendingMachineVisuals
{
VisualState
}
[Serializable, NetSerializable]
public enum VendingMachineUiKey
{
Key,
}
[Serializable, NetSerializable]
public sealed class InventorySyncRequestMessage : BoundUserInterfaceMessage
{
}
[Serializable, NetSerializable]
public sealed class VendingMachineInventoryMessage : BoundUserInterfaceMessage
{
public readonly List<VendingMachineInventoryEntry> Inventory;
public VendingMachineInventoryMessage(List<VendingMachineInventoryEntry> inventory)
{
Inventory = inventory;
}
}
[Serializable, NetSerializable]
public sealed class VendingMachineInventoryEntry
{
[ViewVariables(VVAccess.ReadWrite)] public InventoryType Type;
[ViewVariables(VVAccess.ReadWrite)]
public string ID;
[ViewVariables(VVAccess.ReadWrite)]
public uint Amount;
public VendingMachineInventoryEntry(InventoryType type, string id, uint amount)
{
Type = type;
ID = id;
Amount = amount;
}
}
[Serializable, NetSerializable]
public enum VendingMachineWireStatus : byte
{
Power,
Access,
Advertisement,
Limiter
}
[Serializable, NetSerializable]
public enum InventoryType : byte
{
Regular,
Emagged,
Contraband
}
[Serializable, NetSerializable]
public enum VendingMachineVisualState
{
Normal,
Off,
Broken,
Eject,
Deny,
}
[Serializable, NetSerializable]

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

View File

@@ -0,0 +1,33 @@
using Robust.Shared.Serialization;
namespace Content.Shared.VendingMachines
{
[NetSerializable, Serializable]
public sealed class VendingMachineInterfaceState : BoundUserInterfaceState
{
public List<VendingMachineInventoryEntry> Inventory;
public VendingMachineInterfaceState(List<VendingMachineInventoryEntry> inventory)
{
Inventory = inventory;
}
}
[Serializable, NetSerializable]
public sealed class VendingMachineEjectMessage : BoundUserInterfaceMessage
{
public readonly InventoryType Type;
public readonly string ID;
public VendingMachineEjectMessage(InventoryType type, string id)
{
Type = type;
ID = id;
}
}
[Serializable, NetSerializable]
public enum VendingMachineUiKey
{
Key,
}
}

View File

@@ -7,19 +7,9 @@ namespace Content.Shared.VendingMachines
public sealed class VendingMachineInventoryPrototype : IPrototype
{
[ViewVariables]
[IdDataFieldAttribute]
[IdDataField]
public string ID { get; } = default!;
[DataField("name")]
public string Name { get; } = string.Empty;
[DataField("animationDuration")]
public double AnimationDuration { get; }
// TODO make this a proper sprite specifier for yaml linting.
[DataField("spriteName")]
public string SpriteName { get; } = string.Empty;
[DataField("startingInventory")]
public Dictionary<string, uint> StartingInventory { get; } = new();