Reduce vendor restocking time + some code cleanup (#16025)

This commit is contained in:
Nemanja
2023-05-03 01:38:03 -04:00
committed by GitHub
parent b88a81e0b5
commit 7f3846b7c0
9 changed files with 252 additions and 242 deletions

View File

@@ -0,0 +1,92 @@
using Content.Shared.DoAfter;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Content.Shared.Wires;
using Robust.Shared.Audio;
namespace Content.Shared.VendingMachines;
public abstract partial class SharedVendingMachineSystem
{
public bool TryAccessMachine(EntityUid uid,
VendingMachineRestockComponent restock,
VendingMachineComponent machineComponent,
EntityUid user,
EntityUid target)
{
if (!TryComp<WiresPanelComponent>(target, out var panel) || !panel.Open)
{
if (_net.IsServer)
{
Popup.PopupCursor(Loc.GetString("vending-machine-restock-needs-panel-open",
("this", uid),
("user", user),
("target", target)),
user);
}
return false;
}
return true;
}
public bool TryMatchPackageToMachine(EntityUid uid,
VendingMachineRestockComponent component,
VendingMachineComponent machineComponent,
EntityUid user,
EntityUid target)
{
if (!component.CanRestock.Contains(machineComponent.PackPrototypeId))
{
if (_net.IsServer)
{
Popup.PopupCursor(Loc.GetString("vending-machine-restock-invalid-inventory", ("this", uid), ("user", user),
("target", target)), user);
}
return false;
}
return true;
}
private void OnAfterInteract(EntityUid uid, VendingMachineRestockComponent component, AfterInteractEvent args)
{
if (args.Target is not { } target || !args.CanReach || args.Handled)
return;
if (!TryComp<VendingMachineComponent>(args.Target, out var machineComponent))
return;
if (!TryMatchPackageToMachine(uid, component, machineComponent, args.User, target))
return;
if (!TryAccessMachine(uid, component, machineComponent, args.User, target))
return;
args.Handled = true;
var doAfterArgs = new DoAfterArgs(args.User, (float) component.RestockDelay.TotalSeconds, new RestockDoAfterEvent(), target,
target: target, used: uid)
{
BreakOnTargetMove = true,
BreakOnUserMove = true,
BreakOnDamage = true,
NeedHand = true
};
if (!_doAfter.TryStartDoAfter(doAfterArgs))
return;
if (_net.IsServer)
{
Popup.PopupEntity(Loc.GetString("vending-machine-restock-start", ("this", uid), ("user", args.User),
("target", target)),
args.User,
PopupType.Medium);
}
Audio.PlayPredicted(component.SoundRestockStart, uid, args.User);
}
}

View File

@@ -2,18 +2,25 @@ using Content.Shared.Emag.Components;
using Robust.Shared.Prototypes;
using System.Linq;
using Content.Shared.DoAfter;
using Robust.Shared.Serialization;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Robust.Shared.Network;
namespace Content.Shared.VendingMachines;
public abstract class SharedVendingMachineSystem : EntitySystem
public abstract partial class SharedVendingMachineSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly INetManager _net = default!;
[Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
[Dependency] protected readonly SharedAudioSystem Audio = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] protected readonly SharedPopupSystem Popup = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<VendingMachineComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<VendingMachineRestockComponent, AfterInteractEvent>(OnAfterInteract);
}
protected virtual void OnComponentInit(EntityUid uid, VendingMachineComponent component, ComponentInit args)
@@ -29,7 +36,7 @@ public abstract class SharedVendingMachineSystem : EntitySystem
return;
}
if (!_prototypeManager.TryIndex(component.PackPrototypeId, out VendingMachineInventoryPrototype? packPrototype))
if (!PrototypeManager.TryIndex(component.PackPrototypeId, out VendingMachineInventoryPrototype? packPrototype))
return;
AddInventoryFromPrototype(uid, packPrototype.StartingInventory, InventoryType.Regular, component);
@@ -96,9 +103,9 @@ public abstract class SharedVendingMachineSystem : EntitySystem
foreach (var (id, amount) in entries)
{
if (_prototypeManager.HasIndex<EntityPrototype>(id))
if (PrototypeManager.HasIndex<EntityPrototype>(id))
{
if (inventory.TryGetValue(id, out VendingMachineInventoryEntry? entry))
if (inventory.TryGetValue(id, out var entry))
// Prevent a machine's stock from going over three times
// the prototype's normal amount. This is an arbitrary
// number and meant to be a convenience for someone
@@ -112,8 +119,3 @@ public abstract class SharedVendingMachineSystem : EntitySystem
}
}
}
[Serializable, NetSerializable]
public sealed class RestockDoAfterEvent : SimpleDoAfterEvent
{
}

View File

@@ -86,7 +86,13 @@ namespace Content.Shared.VendingMachines
/// </summary>
[DataField("soundVend")]
// Grabbed from: https://github.com/discordia-space/CEV-Eris/blob/f702afa271136d093ddeb415423240a2ceb212f0/sound/machines/vending_drop.ogg
public SoundSpecifier SoundVend = new SoundPathSpecifier("/Audio/Machines/machine_vend.ogg");
public SoundSpecifier SoundVend = new SoundPathSpecifier("/Audio/Machines/machine_vend.ogg")
{
Params = new AudioParams
{
Volume = -2f
}
};
/// <summary>
/// Sound that plays when an item can't be ejected

View File

@@ -0,0 +1,52 @@
using Content.Shared.DoAfter;
using Robust.Shared.Audio;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.VendingMachines;
[RegisterComponent, NetworkedComponent, Access(typeof(SharedVendingMachineSystem))]
public sealed class VendingMachineRestockComponent : Component
{
/// <summary>
/// The time (in seconds) that it takes to restock a machine.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("restockDelay")]
public TimeSpan RestockDelay = TimeSpan.FromSeconds(5.0f);
/// <summary>
/// What sort of machine inventory does this restock?
/// This is checked against the VendingMachineComponent's pack value.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("canRestock", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<VendingMachineInventoryPrototype>))]
public HashSet<string> CanRestock = new();
/// <summary>
/// Sound that plays when starting to restock a machine.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("soundRestockStart")]
public SoundSpecifier SoundRestockStart = new SoundPathSpecifier("/Audio/Machines/vending_restock_start.ogg")
{
Params = new AudioParams
{
Volume = -2f,
Variation = 0.2f
}
};
/// <summary>
/// Sound that plays when finished restocking a machine.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("soundRestockDone")]
public SoundSpecifier SoundRestockDone = new SoundPathSpecifier("/Audio/Machines/vending_restock_done.ogg");
}
[Serializable, NetSerializable]
public sealed class RestockDoAfterEvent : SimpleDoAfterEvent
{
}