Predict vending machine UI (#33412)
This commit is contained in:
@@ -1,19 +1,12 @@
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using Content.Server.Advertise;
|
||||
using Content.Server.Advertise.Components;
|
||||
using Content.Server.Cargo.Systems;
|
||||
using Content.Server.Emp;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Power.EntitySystems;
|
||||
using Content.Shared.Access.Components;
|
||||
using Content.Shared.Access.Systems;
|
||||
using Content.Shared.Actions;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Destructible;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.Emag.Components;
|
||||
using Content.Shared.Emag.Systems;
|
||||
using Content.Shared.Emp;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Power;
|
||||
@@ -21,7 +14,6 @@ using Content.Shared.Throwing;
|
||||
using Content.Shared.UserInterface;
|
||||
using Content.Shared.VendingMachines;
|
||||
using Content.Shared.Wall;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
@@ -32,14 +24,9 @@ namespace Content.Server.VendingMachines
|
||||
public sealed class VendingMachineSystem : SharedVendingMachineSystem
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly AccessReaderSystem _accessReader = default!;
|
||||
[Dependency] private readonly AppearanceSystem _appearanceSystem = default!;
|
||||
[Dependency] private readonly PricingSystem _pricing = default!;
|
||||
[Dependency] private readonly ThrowingSystem _throwingSystem = default!;
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
[Dependency] private readonly SpeakOnUIClosedSystem _speakOnUIClosed = default!;
|
||||
[Dependency] private readonly SharedPointLightSystem _light = default!;
|
||||
[Dependency] private readonly EmagSystem _emag = default!;
|
||||
|
||||
private const float WallVendEjectDistanceFromWall = 1f;
|
||||
|
||||
@@ -55,11 +42,6 @@ namespace Content.Server.VendingMachines
|
||||
|
||||
SubscribeLocalEvent<VendingMachineComponent, ActivatableUIOpenAttemptEvent>(OnActivatableUIOpenAttempt);
|
||||
|
||||
Subs.BuiEvents<VendingMachineComponent>(VendingMachineUiKey.Key, subs =>
|
||||
{
|
||||
subs.Event<VendingMachineEjectMessage>(OnInventoryEjectMessage);
|
||||
});
|
||||
|
||||
SubscribeLocalEvent<VendingMachineComponent, VendingMachineSelfDispenseEvent>(OnSelfDispense);
|
||||
|
||||
SubscribeLocalEvent<VendingMachineComponent, RestockDoAfterEvent>(OnDoAfter);
|
||||
@@ -91,7 +73,7 @@ namespace Content.Server.VendingMachines
|
||||
|
||||
if (HasComp<ApcPowerReceiverComponent>(uid))
|
||||
{
|
||||
TryUpdateVisualState(uid, component);
|
||||
TryUpdateVisualState((uid, component));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,26 +83,15 @@ namespace Content.Server.VendingMachines
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void OnInventoryEjectMessage(EntityUid uid, VendingMachineComponent component, VendingMachineEjectMessage args)
|
||||
{
|
||||
if (!this.IsPowered(uid, EntityManager))
|
||||
return;
|
||||
|
||||
if (args.Actor is not { Valid: true } entity || Deleted(entity))
|
||||
return;
|
||||
|
||||
AuthorizedVend(uid, entity, args.Type, args.ID, component);
|
||||
}
|
||||
|
||||
private void OnPowerChanged(EntityUid uid, VendingMachineComponent component, ref PowerChangedEvent args)
|
||||
{
|
||||
TryUpdateVisualState(uid, component);
|
||||
TryUpdateVisualState((uid, component));
|
||||
}
|
||||
|
||||
private void OnBreak(EntityUid uid, VendingMachineComponent vendComponent, BreakageEventArgs eventArgs)
|
||||
{
|
||||
vendComponent.Broken = true;
|
||||
TryUpdateVisualState(uid, vendComponent);
|
||||
TryUpdateVisualState((uid, vendComponent));
|
||||
}
|
||||
|
||||
private void OnDamageChanged(EntityUid uid, VendingMachineComponent component, DamageChangedEvent args)
|
||||
@@ -128,7 +99,7 @@ namespace Content.Server.VendingMachines
|
||||
if (!args.DamageIncreased && component.Broken)
|
||||
{
|
||||
component.Broken = false;
|
||||
TryUpdateVisualState(uid, component);
|
||||
TryUpdateVisualState((uid, component));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -139,8 +110,11 @@ namespace Content.Server.VendingMachines
|
||||
if (args.DamageIncreased && args.DamageDelta.GetTotal() >= component.DispenseOnHitThreshold &&
|
||||
_random.Prob(component.DispenseOnHitChance.Value))
|
||||
{
|
||||
if (component.DispenseOnHitCooldown > 0f)
|
||||
component.DispenseOnHitCoolingDown = true;
|
||||
if (component.DispenseOnHitCooldown != null)
|
||||
{
|
||||
component.DispenseOnHitEnd = Timing.CurTime + component.DispenseOnHitCooldown.Value;
|
||||
}
|
||||
|
||||
EjectRandom(uid, throwItem: true, forceEject: true, component);
|
||||
}
|
||||
}
|
||||
@@ -199,145 +173,6 @@ namespace Content.Server.VendingMachines
|
||||
Dirty(uid, component);
|
||||
}
|
||||
|
||||
public void Deny(EntityUid uid, VendingMachineComponent? vendComponent = null)
|
||||
{
|
||||
if (!Resolve(uid, ref vendComponent))
|
||||
return;
|
||||
|
||||
if (vendComponent.Denying)
|
||||
return;
|
||||
|
||||
vendComponent.Denying = true;
|
||||
Audio.PlayPvs(vendComponent.SoundDeny, uid, AudioParams.Default.WithVolume(-2f));
|
||||
TryUpdateVisualState(uid, vendComponent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the user is authorized to use this vending machine
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="sender">Entity trying to use the vending machine</param>
|
||||
/// <param name="vendComponent"></param>
|
||||
public bool IsAuthorized(EntityUid uid, EntityUid sender, VendingMachineComponent? vendComponent = null)
|
||||
{
|
||||
if (!Resolve(uid, ref vendComponent))
|
||||
return false;
|
||||
|
||||
if (!TryComp<AccessReaderComponent>(uid, out var accessReader))
|
||||
return true;
|
||||
|
||||
if (_accessReader.IsAllowed(sender, uid, accessReader))
|
||||
return true;
|
||||
|
||||
Popup.PopupEntity(Loc.GetString("vending-machine-component-try-eject-access-denied"), uid);
|
||||
Deny(uid, vendComponent);
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to eject the provided item. Will do nothing if the vending machine is incapable of ejecting, already ejecting
|
||||
/// or the item doesn't exist in its inventory.
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="type">The type of inventory the item is from</param>
|
||||
/// <param name="itemId">The prototype ID of the item</param>
|
||||
/// <param name="throwItem">Whether the item should be thrown in a random direction after ejection</param>
|
||||
/// <param name="vendComponent"></param>
|
||||
public void TryEjectVendorItem(EntityUid uid, InventoryType type, string itemId, bool throwItem, VendingMachineComponent? vendComponent = null)
|
||||
{
|
||||
if (!Resolve(uid, ref vendComponent))
|
||||
return;
|
||||
|
||||
if (vendComponent.Ejecting || vendComponent.Broken || !this.IsPowered(uid, EntityManager))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var entry = GetEntry(uid, itemId, type, vendComponent);
|
||||
|
||||
if (entry == null)
|
||||
{
|
||||
Popup.PopupEntity(Loc.GetString("vending-machine-component-try-eject-invalid-item"), uid);
|
||||
Deny(uid, vendComponent);
|
||||
return;
|
||||
}
|
||||
|
||||
if (entry.Amount <= 0)
|
||||
{
|
||||
Popup.PopupEntity(Loc.GetString("vending-machine-component-try-eject-out-of-stock"), uid);
|
||||
Deny(uid, vendComponent);
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(entry.ID))
|
||||
return;
|
||||
|
||||
|
||||
// Start Ejecting, and prevent users from ordering while anim playing
|
||||
vendComponent.Ejecting = true;
|
||||
vendComponent.NextItemToEject = entry.ID;
|
||||
vendComponent.ThrowNextItem = throwItem;
|
||||
|
||||
if (TryComp(uid, out SpeakOnUIClosedComponent? speakComponent))
|
||||
_speakOnUIClosed.TrySetFlag((uid, speakComponent));
|
||||
|
||||
entry.Amount--;
|
||||
Dirty(uid, vendComponent);
|
||||
TryUpdateVisualState(uid, vendComponent);
|
||||
Audio.PlayPvs(vendComponent.SoundVend, uid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether the user is authorized to use the vending machine, then ejects the provided item if true
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="sender">Entity that is trying to use the vending machine</param>
|
||||
/// <param name="type">The type of inventory the item is from</param>
|
||||
/// <param name="itemId">The prototype ID of the item</param>
|
||||
/// <param name="component"></param>
|
||||
public void AuthorizedVend(EntityUid uid, EntityUid sender, InventoryType type, string itemId, VendingMachineComponent component)
|
||||
{
|
||||
if (IsAuthorized(uid, sender, component))
|
||||
{
|
||||
TryEjectVendorItem(uid, type, itemId, component.CanShoot, component);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to update the visuals of the component based on its current state.
|
||||
/// </summary>
|
||||
public void TryUpdateVisualState(EntityUid uid, VendingMachineComponent? vendComponent = null)
|
||||
{
|
||||
if (!Resolve(uid, ref vendComponent))
|
||||
return;
|
||||
|
||||
var finalState = VendingMachineVisualState.Normal;
|
||||
if (vendComponent.Broken)
|
||||
{
|
||||
finalState = VendingMachineVisualState.Broken;
|
||||
}
|
||||
else if (vendComponent.Ejecting)
|
||||
{
|
||||
finalState = VendingMachineVisualState.Eject;
|
||||
}
|
||||
else if (vendComponent.Denying)
|
||||
{
|
||||
finalState = VendingMachineVisualState.Deny;
|
||||
}
|
||||
else if (!this.IsPowered(uid, EntityManager))
|
||||
{
|
||||
finalState = VendingMachineVisualState.Off;
|
||||
}
|
||||
|
||||
if (_light.TryGetLight(uid, out var pointlight))
|
||||
{
|
||||
var lightState = finalState != VendingMachineVisualState.Broken && finalState != VendingMachineVisualState.Off;
|
||||
_light.SetEnabled(uid, lightState, pointlight);
|
||||
}
|
||||
|
||||
_appearanceSystem.SetData(uid, VendingMachineVisuals.VisualState, finalState);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ejects a random item from the available stock. Will do nothing if the vending machine is empty.
|
||||
/// </summary>
|
||||
@@ -367,18 +202,18 @@ namespace Content.Server.VendingMachines
|
||||
}
|
||||
else
|
||||
{
|
||||
TryEjectVendorItem(uid, item.Type, item.ID, throwItem, vendComponent);
|
||||
TryEjectVendorItem(uid, item.Type, item.ID, throwItem, user: null, vendComponent: vendComponent);
|
||||
}
|
||||
}
|
||||
|
||||
private void EjectItem(EntityUid uid, VendingMachineComponent? vendComponent = null, bool forceEject = false)
|
||||
protected override void EjectItem(EntityUid uid, VendingMachineComponent? vendComponent = null, bool forceEject = false)
|
||||
{
|
||||
if (!Resolve(uid, ref vendComponent))
|
||||
return;
|
||||
|
||||
// No need to update the visual state because we never changed it during a forced eject
|
||||
if (!forceEject)
|
||||
TryUpdateVisualState(uid, vendComponent);
|
||||
TryUpdateVisualState((uid, vendComponent));
|
||||
|
||||
if (string.IsNullOrEmpty(vendComponent.NextItemToEject))
|
||||
{
|
||||
@@ -411,68 +246,17 @@ namespace Content.Server.VendingMachines
|
||||
vendComponent.ThrowNextItem = false;
|
||||
}
|
||||
|
||||
private VendingMachineInventoryEntry? GetEntry(EntityUid uid, string entryId, InventoryType type, VendingMachineComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return null;
|
||||
|
||||
if (type == InventoryType.Emagged && _emag.CheckFlag(uid, EmagType.Interaction))
|
||||
return component.EmaggedInventory.GetValueOrDefault(entryId);
|
||||
|
||||
if (type == InventoryType.Contraband && component.Contraband)
|
||||
return component.ContrabandInventory.GetValueOrDefault(entryId);
|
||||
|
||||
return component.Inventory.GetValueOrDefault(entryId);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
var query = EntityQueryEnumerator<VendingMachineComponent>();
|
||||
while (query.MoveNext(out var uid, out var comp))
|
||||
{
|
||||
if (comp.Ejecting)
|
||||
{
|
||||
comp.EjectAccumulator += frameTime;
|
||||
if (comp.EjectAccumulator >= comp.EjectDelay)
|
||||
{
|
||||
comp.EjectAccumulator = 0f;
|
||||
comp.Ejecting = false;
|
||||
|
||||
EjectItem(uid, comp);
|
||||
}
|
||||
}
|
||||
|
||||
if (comp.Denying)
|
||||
{
|
||||
comp.DenyAccumulator += frameTime;
|
||||
if (comp.DenyAccumulator >= comp.DenyDelay)
|
||||
{
|
||||
comp.DenyAccumulator = 0f;
|
||||
comp.Denying = false;
|
||||
|
||||
TryUpdateVisualState(uid, comp);
|
||||
}
|
||||
}
|
||||
|
||||
if (comp.DispenseOnHitCoolingDown)
|
||||
{
|
||||
comp.DispenseOnHitAccumulator += frameTime;
|
||||
if (comp.DispenseOnHitAccumulator >= comp.DispenseOnHitCooldown)
|
||||
{
|
||||
comp.DispenseOnHitAccumulator = 0f;
|
||||
comp.DispenseOnHitCoolingDown = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
var disabled = EntityQueryEnumerator<EmpDisabledComponent, VendingMachineComponent>();
|
||||
while (disabled.MoveNext(out var uid, out _, out var comp))
|
||||
{
|
||||
if (comp.NextEmpEject < _timing.CurTime)
|
||||
{
|
||||
EjectRandom(uid, true, false, comp);
|
||||
comp.NextEmpEject += TimeSpan.FromSeconds(5 * comp.EjectDelay);
|
||||
comp.NextEmpEject += (5 * comp.EjectDelay);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -485,7 +269,7 @@ namespace Content.Server.VendingMachines
|
||||
RestockInventoryFromPrototype(uid, vendComponent);
|
||||
|
||||
Dirty(uid, vendComponent);
|
||||
TryUpdateVisualState(uid, vendComponent);
|
||||
TryUpdateVisualState((uid, vendComponent));
|
||||
}
|
||||
|
||||
private void OnPriceCalculation(EntityUid uid, VendingMachineRestockComponent component, ref PriceCalculationEvent args)
|
||||
|
||||
Reference in New Issue
Block a user